Template parsing
This commit is contained in:
parent
fb4dba31bc
commit
d8f76c01d0
38
main.go
38
main.go
@ -1,21 +1,35 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Joker/jade"
|
||||||
"github.com/aerojs/aero"
|
"github.com/aerojs/aero"
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
"github.com/buaazp/fasthttprouter"
|
"github.com/buaazp/fasthttprouter"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
"github.com/valyala/fasttemplate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
arn.Init()
|
arn.Init()
|
||||||
|
|
||||||
app := aero.New()
|
app := aero.New()
|
||||||
|
jade.PrettyOutput = false
|
||||||
|
code, _ := jade.ParseFile("pages/anime/anime.pug")
|
||||||
|
code = strings.TrimSpace(code)
|
||||||
|
code = strings.Replace(code, "{{ ", "{{", -1)
|
||||||
|
code = strings.Replace(code, " }}", "}}", -1)
|
||||||
|
fmt.Println(code)
|
||||||
|
t := fasttemplate.New(code, "{{", "}}")
|
||||||
|
|
||||||
app.Get("/", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
|
app.Get("/", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
|
||||||
ctx.Write(ctx.RequestURI())
|
ctx.Response.Header.Set("content-type", "text/html;charset=utf-8")
|
||||||
|
ctx.SetBodyString("Hello World")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/anime/:id", func(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
|
app.Get("/anime/:id", func(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
|
||||||
@ -27,8 +41,28 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.WriteString(anime.Description)
|
ctx.Response.Header.Set("content-type", "text/html;charset=utf-8")
|
||||||
|
|
||||||
|
writer := ctx.Response.BodyWriter()
|
||||||
|
t.ExecuteFunc(writer, func(w io.Writer, tag string) (int, error) {
|
||||||
|
val := reflect.ValueOf(*anime)
|
||||||
|
parts := strings.Split(tag, ".")
|
||||||
|
|
||||||
|
for _, part := range parts {
|
||||||
|
val = val.FieldByName(part)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val.Kind() {
|
||||||
|
case reflect.Int:
|
||||||
|
num := strconv.FormatInt(val.Int(), 10)
|
||||||
|
return w.Write([]byte(num))
|
||||||
|
default:
|
||||||
|
return w.Write([]byte(val.String()))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println("Starting server on http://localhost:5000/")
|
||||||
|
|
||||||
app.Run()
|
app.Run()
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
h1 {{.Title.Romaji}}
|
h1 {{ Title.Romaji }}
|
||||||
p {{.Description}}
|
p {{ Description }}
|
||||||
|
if ID
|
||||||
|
p {{ ID }}
|
||||||
|
else
|
||||||
|
p Undefined.
|
Loading…
Reference in New Issue
Block a user