Add include example

This commit is contained in:
Elara 2023-10-29 19:56:15 -07:00
parent e3770adc87
commit 1c87deb61b
8 changed files with 169 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/readme /readme
/replybot /replybot
/include

View File

@ -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)
}
}

View File

@ -0,0 +1,23 @@
<html>
#include("head.html")
<body>
#include("header.html")
<section class="hero is-fullheight-with-navbar">
<div class="hero-head">
<div class="container">
<p class="title has-text-centered mt-2">About Salix</p>
<p>
Salix (pronounced <i>say-lix</i>) is a Go templating engine inspired by <a href="https://github.com/vapor/leaf">Leaf</a>.
<br><br>
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! :)
</p>
</div>
</div>
</section>
#include("footer.html")
</body>
</html>

View File

@ -0,0 +1,7 @@
<section class="hero is-small is-dark">
<div class="hero-body">
<div class="container">
<p class="has-text-centered">Copyright &copy; #(now().Year()) Salix Contributors. Licensed under the GPLv3.</p>
</div>
</div>
</section>

View File

@ -0,0 +1,4 @@
<head>
<title>#(title)</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
</head>

View File

@ -0,0 +1,36 @@
<nav class="navbar is-dark">
<div class="navbar-brand">
<a class="navbar-item" href="/">Salix</a>
<a class="navbar-burger" id="navbarMenuIcon" onclick="toggleNavMenu()">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div class="navbar-menu" id="navbarMenu">
<div class="navbar-end">
<a class='navbar-item #if(title == "Home"):is-active#!if' href="/">
Home
</a>
<a class='navbar-item #if(title == "About"):is-active#!if' href="/about">
About
</a>
</div>
</div>
</nav>
<script>
function toggleNavMenu() {
let navbarMenuIcon = document.getElementById("navbarMenuIcon");
let navbarMenu = document.getElementById("navbarMenu");
if (navbarMenu.classList.contains('is-active')) {
navbarMenuIcon.classList.remove('is-active')
navbarMenu.classList.remove('is-active')
} else {
navbarMenuIcon.classList.add('is-active')
navbarMenu.classList.add('is-active')
}
}
</script>

View File

@ -0,0 +1,19 @@
<html>
#include("head.html")
<body>
#include("header.html")
<section class="hero is-fullheight-with-navbar">
<div class="hero-body">
<div class="container">
<p class="title">Hello, #(name)!</p>
<p class="subtitle">This is a demo of the Salix template engine.</p>
<a class="button is-link is-rounded" href="/about">About &rarr;</a>
</div>
</div>
</section>
#include("footer.html")
</body>
</html>

View File

@ -11,7 +11,7 @@ var (
ErrNoSuchTemplate = errors.New("no such template") 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{} type includeTag struct{}
func (it includeTag) Run(tc *TagContext, block, args []ast.Node) error { func (it includeTag) Run(tc *TagContext, block, args []ast.Node) error {