4
4
"fmt"
5
5
"strings"
6
6
"html"
7
- _"html/template"
7
+ "html/template"
8
+ "bytes"
8
9
9
10
a "github.com/julvo/htmlgo/attributes"
10
11
)
@@ -23,7 +24,7 @@ func insertAttributes(attrs []a.Attribute) string {
23
24
return s
24
25
}
25
26
26
- func insertChildren (children [] HTML ) string {
27
+ func insertChildren (children ... HTML ) string {
27
28
s := ""
28
29
for _ , c := range children {
29
30
s += string (c )
@@ -38,7 +39,7 @@ func indent(s, indentation string) string {
38
39
func Element (tag string , attrs []a.Attribute , children ... HTML ) HTML {
39
40
return HTML (
40
41
"\n <" + tag + insertAttributes (attrs ) + ">" +
41
- indent (insertChildren (children ), " " ) +
42
+ indent (insertChildren (children ... ), " " ) +
42
43
"\n </" + tag + ">" )
43
44
}
44
45
@@ -69,7 +70,23 @@ func Doctype(t string) HTML {
69
70
const DoctypeHtml5 HTML = "<!DOCTYPE HTML>"
70
71
71
72
func Script (attrs []a.Attribute , js JS ) HTML {
72
- return HTML ("" )
73
+ if js .data == nil {
74
+ return Element ("script" , attrs , HTML ("\n " + js .templ ))
75
+ }
76
+
77
+ // TODO set verbosity level to enable logging
78
+ t , err := template .New ("_" ).Parse (
79
+ "\n <script" + insertAttributes (attrs ) + ">" +
80
+ indent ("\n " + js .templ , " " ) + "\n </script>" )
81
+ if err != nil {
82
+ return Element ("script" , attrs )
83
+ }
84
+ buf := new (bytes.Buffer )
85
+ err = t .Execute (buf , js .data )
86
+ if err != nil {
87
+ return Element ("script" , attrs )
88
+ }
89
+ return HTML (buf .String ())
73
90
}
74
91
75
92
func Script_ (js JS ) HTML {
0 commit comments