Compare commits
1 Commits
adib/md-no
...
templCompM
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6191f1ccaa |
69
handlers/dev/xtemplmulti.handlers.go
Normal file
69
handlers/dev/xtemplmulti.handlers.go
Normal file
@@ -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: "<div style='background-color:red;'>Komponen 1<div>"}
|
||||
|
||||
newItem2 := models.XtemplModel{
|
||||
NameTempl: "Komponen 2",
|
||||
HtmlString: "<div style='background-color:yellow;'>Komponen 2<div>"}
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
6
models/xtempl.models.go
Normal file
6
models/xtempl.models.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type XtemplModel struct {
|
||||
NameTempl string `json:"nametempl"`
|
||||
HtmlString string `json:"htmlString"`
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
|
||||
35
views/dev/xtemplmulti/xtemplmulti.templ
Normal file
35
views/dev/xtemplmulti/xtemplmulti.templ
Normal file
@@ -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) {
|
||||
<div id="dataMhs" style="margin-bottom: 20px">
|
||||
<table>
|
||||
<tr>
|
||||
<td>Jarot</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id="dataGuru" style="margin-bottom: 20px">
|
||||
<table>
|
||||
<tr>
|
||||
<td>Desy</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
for _, v := range arrString {
|
||||
{ v.NameTempl }
|
||||
{ v.HtmlString }
|
||||
|
||||
@stringToTemplComp(v.HtmlString)
|
||||
}
|
||||
|
||||
}
|
||||
72
views/dev/xtemplmulti/xtemplmulti_templ.go
Normal file
72
views/dev/xtemplmulti/xtemplmulti_templ.go
Normal file
@@ -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("<div id=\"dataMhs\" style=\"margin-bottom: 20px\"><table><tr><td>Jarot</td></tr></table></div><div id=\"dataGuru\" style=\"margin-bottom: 20px\"><table><tr><td>Desy</td></tr></table></div>")
|
||||
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
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user