Add recursion example
This commit is contained in:
parent
236e178cea
commit
82e81ec7f4
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
/replybot
|
||||
/include
|
||||
/macro
|
||||
/recursion
|
6
examples/recursion/post.html
Normal file
6
examples/recursion/post.html
Normal file
@ -0,0 +1,6 @@
|
||||
<div class="post" data-id="#(post.ID)">
|
||||
<p>Message=#(post.Message)</p>
|
||||
#for(child in post.Children):
|
||||
#include("post.html", post = child)
|
||||
#!for
|
||||
</div>
|
80
examples/recursion/recursion.go
Normal file
80
examples/recursion/recursion.go
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Salix - Go templating engine
|
||||
* Copyright (C) 2023 Elara Musayelyan
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"go.elara.ws/salix"
|
||||
)
|
||||
|
||||
//go:embed post.html
|
||||
var tmpl string
|
||||
|
||||
type Post struct {
|
||||
ID int
|
||||
Message string
|
||||
Children []*Post
|
||||
}
|
||||
|
||||
var posts = Post{
|
||||
ID: 0,
|
||||
Message: "Hello",
|
||||
Children: []*Post{
|
||||
{
|
||||
ID: 12,
|
||||
Message: "Hi!",
|
||||
Children: []*Post{
|
||||
{
|
||||
ID: 13234,
|
||||
Message: "Test 2",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 13,
|
||||
Message: "Test",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
t, err := salix.New().ParseString("post.html", tmpl)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
err = t.WithVarMap(map[string]any{"post": posts}).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)
|
||||
}
|
||||
}
|
6
salix.go
6
salix.go
@ -368,6 +368,9 @@ func (t *Template) getField(fa ast.FieldAccess, local map[string]any) (any, erro
|
||||
return nil, err
|
||||
}
|
||||
rval := reflect.ValueOf(val)
|
||||
for rval.Kind() == reflect.Pointer {
|
||||
rval = rval.Elem()
|
||||
}
|
||||
field := rval.FieldByName(fa.Name.Value)
|
||||
if !field.IsValid() {
|
||||
return nil, t.posError(fa, "%w: %s", ErrNoSuchField, fa.Name.Value)
|
||||
@ -382,6 +385,9 @@ func (t *Template) execMethodCall(mc ast.MethodCall, local map[string]any) (any,
|
||||
return nil, err
|
||||
}
|
||||
rval := reflect.ValueOf(val)
|
||||
for rval.Kind() == reflect.Pointer {
|
||||
rval = rval.Elem()
|
||||
}
|
||||
mtd := rval.MethodByName(mc.Name.Value)
|
||||
if !mtd.IsValid() {
|
||||
return nil, t.posError(mc, "%w: %s", ErrNoSuchMethod, mc.Name.Value)
|
||||
|
Loading…
Reference in New Issue
Block a user