From 37f07715ff2cbffd003fed556ce40dd00710007e Mon Sep 17 00:00:00 2001 From: sindhu Date: Fri, 17 May 2024 13:30:02 +0700 Subject: [PATCH] view multi templ component --- handlers/dev/xtemplmulti.handlers.go | 69 +++++++++++++++++++++ handlers/routes.go | 2 + models/xtempl.models.go | 6 ++ utils/view.utils.go | 22 +++++++ views/dev/xtemplmulti/xtemplmulti.templ | 35 +++++++++++ views/dev/xtemplmulti/xtemplmulti_templ.go | 72 ++++++++++++++++++++++ 6 files changed, 206 insertions(+) create mode 100644 handlers/dev/xtemplmulti.handlers.go create mode 100644 models/xtempl.models.go create mode 100644 views/dev/xtemplmulti/xtemplmulti.templ create mode 100644 views/dev/xtemplmulti/xtemplmulti_templ.go diff --git a/handlers/dev/xtemplmulti.handlers.go b/handlers/dev/xtemplmulti.handlers.go new file mode 100644 index 0000000..91cae34 --- /dev/null +++ b/handlers/dev/xtemplmulti.handlers.go @@ -0,0 +1,69 @@ +package dev_handlers + +import ( + "cpone/models" + "cpone/utils" + "html/template" + "strings" + + xtemplmulti "cpone/views/dev/xtemplmulti" + + "github.com/a-h/templ" + "github.com/labstack/echo/v4" +) + +type XTemplMultiService interface { +} + +func NewXTemplMultiHandler(us XTemplMultiService) *XTemplMultiHandler { + return &XTemplMultiHandler{ + XTemplMultiService: us, + } +} + +type XTemplMultiHandler struct { + XTemplMultiService XTemplMultiService +} + +func (lh *MasterMenuUserGroupHandler) HandlerXTemplMulti(c echo.Context) error { + var arrTempl []models.XtemplModel + // toastComponent := customtoastv2.CustomToastV2Show("sukses", "halo", "warning") + + newItem := models.XtemplModel{ + NameTempl: "Komponen 1", + HtmlString: "
Komponen 1
"} + + newItem2 := models.XtemplModel{ + NameTempl: "Komponen 2", + HtmlString: "
Komponen 2
"} + + arrTempl = append(arrTempl, newItem, newItem2) + + var stringx string + + for _, item := range arrTempl { + stringx += item.HtmlString + } + + tpl := template.Must(template.New("index").Parse(stringx)) + + var stringBuilder strings.Builder + if err := tpl.Execute(&stringBuilder, arrTempl); err != nil { + panic(err) + } + + newItemFix := models.XtemplModel{ + NameTempl: "Template Gabungan", + HtmlString: stringBuilder.String(), + } + + arrTemplFix := []models.XtemplModel{newItemFix} + + var templx []templ.Component + si := xtemplmulti.MainXTemplMulti(arrTemplFix) + templx = append(templx, si) + templx = append(templx, si) + + // return utils.View(c, si) + return utils.ViewMulti(c, templx) +} diff --git a/handlers/routes.go b/handlers/routes.go index b90e9ce..4a6b965 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -204,6 +204,8 @@ func SetupRoutesDev(app *echo.Echo, appStore db.AppStore) { dev.GET("/hidetoast", devUGhandlers.HandlerHideToast) + dev.GET("/xtemplmulti", devUGhandlers.HandlerXTemplMulti) + // clientgroup.GET("/usergroup/edit", mastermenuusergroupHandler.ChangeFormEdit) // clientgroup.GET("/usergroup/pagination", mastermenuusergroupHandler.HandleChangePage) } diff --git a/models/xtempl.models.go b/models/xtempl.models.go new file mode 100644 index 0000000..afac72b --- /dev/null +++ b/models/xtempl.models.go @@ -0,0 +1,6 @@ +package models + +type XtemplModel struct { + NameTempl string `json:"nametempl"` + HtmlString string `json:"htmlString"` +} diff --git a/utils/view.utils.go b/utils/view.utils.go index ef4e5ac..d9213cf 100644 --- a/utils/view.utils.go +++ b/utils/view.utils.go @@ -1,10 +1,32 @@ package utils import ( + "bytes" + "net/http" + "github.com/a-h/templ" "github.com/labstack/echo/v4" ) +func ViewMulti(c echo.Context, cmp []templ.Component) error { + var stringHtml string = "" + buf := new(bytes.Buffer) + + var err error + for _, x := range cmp { + err = x.Render(c.Request().Context(), buf) + if err != nil { + return err + } + stringHtml += buf.String() + } + + c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML) + + return c.String(http.StatusOK, stringHtml) + +} + func View(c echo.Context, cmp templ.Component) error { c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML) diff --git a/views/dev/xtemplmulti/xtemplmulti.templ b/views/dev/xtemplmulti/xtemplmulti.templ new file mode 100644 index 0000000..693a56b --- /dev/null +++ b/views/dev/xtemplmulti/xtemplmulti.templ @@ -0,0 +1,35 @@ +package xtemplmulti + +import "cpone/models" + +func stringToTemplComp(textAll string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + _, err := io.WriteString(w, textAll) + return err + }) +} + +templ MainXTemplMulti(arrString []models.XtemplModel) { +
+ + + + +
Jarot
+
+ +
+ + + + +
Desy
+
+ for _, v := range arrString { + { v.NameTempl } + { v.HtmlString } + + @stringToTemplComp(v.HtmlString) + } + +} \ No newline at end of file diff --git a/views/dev/xtemplmulti/xtemplmulti_templ.go b/views/dev/xtemplmulti/xtemplmulti_templ.go new file mode 100644 index 0000000..f2cc920 --- /dev/null +++ b/views/dev/xtemplmulti/xtemplmulti_templ.go @@ -0,0 +1,72 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.663 +package xtemplmulti + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import "context" +import "io" +import "bytes" + +import "cpone/models" + +func stringToTemplComp(textAll string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + _, err := io.WriteString(w, textAll) + return err + }) +} + +func MainXTemplMulti(arrString []models.XtemplModel) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Jarot
Desy
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, v := range arrString { + var templ_7745c5c3_Var2 string + templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(v.NameTempl) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views\dev\xtemplmulti\xtemplmulti.templ`, Line: 29, Col: 21} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(v.HtmlString) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views\dev\xtemplmulti\xtemplmulti.templ`, Line: 30, Col: 22} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = stringToTemplComp(v.HtmlString).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +}