Templates
Neo uses Go html/template
package as template engine. For detail information about template syntax and feature please read documentation.
But let’s now focus on what you have to know from Neo’s side to use templating.
First what you have to do is to create templates. For each template you have to define name.
Let’s define three templates, header footer and index. We will include header and footer templates from index.
{{define "header"}}
<p>This is header</p>
{{end}}
{{define "footer"}}
<p>This is footer</p>
{{end}}
{{define "index"}}
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
{{template "header"}}
<div>
Site content
</div>
{{template "footer"}}
</body>
</html>
{{end}}
Next you have to compile above templates. You have to say to Neo where he can find templates which you will use from you application.
Usually you do that from main function.
app.Templates(
"/path/to/templates/*",
"/another/path/to/templates/template.tpl",
)
As you can see you can provide multiple paths where your templates are located.
Then in one of your route handlers you have to call template rendering in order to make HTML and return it to user.
app.Get("/", func(ctx *neo.Ctx) (int, error) {
return 200, ctx.Res.Tpl("index", nil)
})
Second parameter of Tpl
function is data which will be passed into template. So you can make instances of your structs and pass them as second argument. Something like this:
data := Person{"Some", "Person"}
ctx.Res.Tpl("index", data)
And that’s it basically about templates.
Improve this document