diff --git a/.gitignore b/.gitignore index 4301ce5..40fbc12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /readme -/replybot \ No newline at end of file +/replybot +/include \ No newline at end of file diff --git a/examples/include/include.go b/examples/include/include.go new file mode 100644 index 0000000..20927ae --- /dev/null +++ b/examples/include/include.go @@ -0,0 +1,77 @@ +package main + +import ( + "embed" + "io/fs" + "log" + "net/http" + "time" + + "go.elara.ws/salix" +) + +//go:embed tmpls +var tmpls embed.FS + +func main() { + tmplsFS, err := fs.Sub(tmpls, "tmpls") + if err != nil { + log.Fatalln(err) + } + + ns := salix.New().WithVarMap(map[string]any{"now": time.Now}) + + err = ns.ParseFSGlob(tmplsFS, "*.html") + if err != nil { + log.Fatalln(err) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + tmpl, ok := ns.GetTemplate("home.html") + if !ok { + w.WriteHeader(http.StatusInternalServerError) + return + } + + name := r.URL.Query().Get("name") + if name == "" { + name = "World" + } + + err = tmpl. + WithVarMap(map[string]any{ + "title": "Home", + "name": name, + }). + Execute(w) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + }) + + http.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) { + tmpl, ok := ns.GetTemplate("about.html") + if !ok { + w.WriteHeader(http.StatusInternalServerError) + return + } + + err = tmpl. + WithVarMap(map[string]any{"title": "About"}). + Execute(w) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + }) + + log.Println("Starting HTTP server on port 8080") + + err = http.ListenAndServe(":8080", nil) + if err != nil { + log.Fatalln(err) + } +} diff --git a/examples/include/tmpls/about.html b/examples/include/tmpls/about.html new file mode 100644 index 0000000..6858ae3 --- /dev/null +++ b/examples/include/tmpls/about.html @@ -0,0 +1,23 @@ + + #include("head.html") + + + #include("header.html") + +
+
+
+

About Salix

+ +

+ Salix (pronounced say-lix) is a Go templating engine inspired by Leaf. +

+ Salix's syntax is similar to Leaf and (in my opinion at least), it's much more fun to write than the Go template syntax. If you like this project, please star its repo. I hope you enjoy! :) +

+
+
+
+ + #include("footer.html") + + \ No newline at end of file diff --git a/examples/include/tmpls/footer.html b/examples/include/tmpls/footer.html new file mode 100644 index 0000000..f8d1af8 --- /dev/null +++ b/examples/include/tmpls/footer.html @@ -0,0 +1,7 @@ +
+
+
+

Copyright © #(now().Year()) Salix Contributors. Licensed under the GPLv3.

+
+
+
\ No newline at end of file diff --git a/examples/include/tmpls/head.html b/examples/include/tmpls/head.html new file mode 100644 index 0000000..9e064f5 --- /dev/null +++ b/examples/include/tmpls/head.html @@ -0,0 +1,4 @@ + + #(title) + + \ No newline at end of file diff --git a/examples/include/tmpls/header.html b/examples/include/tmpls/header.html new file mode 100644 index 0000000..fc1ff1e --- /dev/null +++ b/examples/include/tmpls/header.html @@ -0,0 +1,36 @@ + + + \ No newline at end of file diff --git a/examples/include/tmpls/home.html b/examples/include/tmpls/home.html new file mode 100644 index 0000000..7b100b3 --- /dev/null +++ b/examples/include/tmpls/home.html @@ -0,0 +1,19 @@ + + #include("head.html") + + + #include("header.html") + +
+
+
+

Hello, #(name)!

+

This is a demo of the Salix template engine.

+ About → +
+
+
+ + #include("footer.html") + + \ No newline at end of file diff --git a/include_tag.go b/include_tag.go index e439d14..c957b2a 100644 --- a/include_tag.go +++ b/include_tag.go @@ -11,7 +11,7 @@ var ( ErrNoSuchTemplate = errors.New("no such template") ) -// forTag represents an #include tag within a Salix template +// includeTag represents an #include tag within a Salix template type includeTag struct{} func (it includeTag) Run(tc *TagContext, block, args []ast.Node) error {