package htmlx import "github.com/d5/tengo/v2" import "log" var Module = map[string] tengo.Object{ "new_render" : &tengo.UserFunction{ Name: "new_render", Value: func(args ...tengo.Object) (tengo.Object, error){ ret := &HTML{} return ret, nil }, }, } type HTML struct{ tengo.ObjectImpl } func (html *HTML) Import(moduleName string) (any, error) { log.Println("import:", moduleName) switch moduleName { case "html": return html, nil } return nil, nil } func (html *HTML) TypeName() string { return "blablabla" } func (html *HTML) String() (string) { return "HTML{...}" } /* html.div({ id: "some-el-id", value: "shit value" }).body( html.raw("cock "), html.strong("something") ) */ func (html *HTML) IndexGet( index tengo.Object, ) (tengo.Object, error) { str, ok := tengo.ToString(index) if !ok { return nil, tengo.ErrInvalidIndexValueType } fn := func(args ...tengo.Object) (tengo.Object, error) { if len(args) > 1 { return nil, tengo.ErrWrongNumArguments } var arg tengo.Object if len(args) == 1 { arg = args[0] } if arg == nil { return &Element{ Tag: str, }, nil } if can := arg.CanIterate() ; !can { return nil, tengo.ErrInvalidArgumentType{ Name: "first", Expected: "iterable", Found: arg.TypeName(), } } attr := map[string] string{} iter := arg.Iterate() for iter.Next() { key, val := iter.Key(), iter.Value() skey, ok := tengo.ToString(key) if !ok { return nil, tengo.ErrInvalidArgumentType{ Name: "attribute(key)", Expected: "stringer", Found: key.TypeName(), } } sval, ok := tengo.ToString(val) if !ok { return nil, tengo.ErrInvalidArgumentType{ Name: "attribute(value)", Expected: "stringer", Found: val.TypeName(), } } attr[skey] = sval } return &Element{ Tag: str, Attr: attr, }, nil } return &tengo.UserFunction{ Name: str, Value: fn, }, nil }