Golang Accessing Template Variables From Included Templates [duplicate] - go

This question already has an answer here:
Variable in template's included template
(1 answer)
Closed 6 years ago.
In golang, I am working with three files: index.html, nav.html and main.go
nav.html contains the following:
{{ define "nav" }}
<nav class="nav-container">
<h1>{{ .path }}</h1>
</nav>
{{ end }}
index.html contains the following:
{{ define "index" }}
{{ template "nav" }} <!-- Includes the nav.html file -->
<h1>Welcome to my website. You are visiting {{ .path }}.</h1>
{{ end }}
I am using Golang's template package along with Martini which is not too important in this case.
My main.go file contains:
package main
import (
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
func main() {
m := martiniSetup()
m.Get("/", func(res http.ResponseWriter, req *http.Request, ren render.Render, params martini.Params) {
parse := make(map[string]interface{})
parse["path"] = req.URL.Path
ren.HTML(http.StatusOK, "index", parse)
})
m.Run()
}
My problem:
The .path variable being parsed into the index template is only accessable by the index template itself.
I include the nav template using {{ template "nav" }} inside index.html. The issue is, nav.html cannot access the .path variable. It is only accessable by the index template.
Is there any way to make the .path variable accessable to all included template files, in my case index.html and nav.html?

You can pass the data to the nested template as an argument like this: {{ template "nav" . }}
Now the dot will be accessible within the define "nav" block.

Related

Execute multiple templates in go

I have this variable and I wanted to know if I can execute multiple templates
and if I can't do it with this variable then what should I do ?
var (
templates = template.Must(template.ParseGlob("web/Pages/*"))
)
Thanks in advance
You can include templates in other templates by using the {{ template "name" }} action.
Say you have file a.html:
<p>this is A</p>
then you can include it in b.html like this:
{{ template "a.html" }}
<p>this is B</p>
and then if you run templates.ExecuteTemplate(os.Stdout, "b.html", nil) you'll get:
<p>this is A</p>
<p>this is B</p>
See the playground link for a more fleshed out example: https://play.golang.com/p/W0whRWlX119

Avoid to reset the variable's value inside loop in golang's view [duplicate]

I'm trying to use a variable declared outside a Go template range loop to see if the previous post occurred on the same day as the current post. Here's a simplified example.
Where .Posts is an array of post structs that each have a .Content and a .Date.
{{ $prevDate := "" }}
{{ range $post := .Posts }}
{{ if ne $prevDate $post.Date }}
<div class="post-date">Posts dated: {{ $post.Date }}</div>
{{ end }}
<div class="post-content">{{ $post.Content }}</div>
{{ $prevDate := $post.Date }}
{{ end }}
The problem is that $prevDate seems to be reset to "" at the start of each iteration of the loop.
Can anyone help me understand why the value of $prevDate is reset on each iteration and perhaps suggest a way to accomplish what I'm trying to do here?
Note: Go 1.11 will support modifying template variables via assignment. This will be valid code:
{{ $v := "init" }}
{{ if true }}
{{ $v = "changed" }}
{{ end }}
v: {{ $v }} {{/* "changed" */}}
Original answer pre-dating Go 1.11 follows:
Variables are not reset. Basically what happens is that you redeclare the $prevDate variable inside the loop. But it is only in scope after the redeclaration and before the closing {{end}} tag of the {{range}}. So when the next iteraiton of the loop comes, you only see the "outer" variable which you haven't changed (because you created a new).
You can't change the values of the template variables you create.
What you can do is for example use the following range form:
{{ range $index, $post := .Posts }}
And...
Solution #1: with a registered Function
And you can register a function for the template (see template.Funcs()) to which you can pass the $index and it would return the date field of the previous element (at $index -1).
It would look something like this:
func PrevDate(i int) string {
if i == 0 {
return ""
}
return posts[i-1].Date
}
// Registering it:
var yourTempl = template.Must(template.New("").
Funcs(map[string]interface{}{"PrevDate": PrevDate}).
Parse(yourStringTemplate))
And from your template you can call it like:
{{range $index, $post := .Posts}}
{{$prevDate := PrevDate $index}}
{{end}}
Solution #2: with a Method of Posts
This solution is analog but is even simpler: add a method to your Posts and you can call it directly. No need to register a function.
For example:
type Post struct {
// Your Post type
Date string
}
type Posts []Post
func (p *Posts) PrevDate(i int) string {
if i == 0 {
return ""
}
return (*p)[i-1].Date
}
And from your template you can call it like:
{{range $index, $post := .Posts}}
{{$prevDate := $.Posts.PrevDate $index}}
{{end}}
Go templates are not designed to support complex logic. There's the Go programming language for that. Templates have limitations as a consequence of this philosophy. One limitation is that template variables cannot be changed.
One way to handle this limitation is to structure the data in Go to match the structure of output. Create a type to hold posts for a date and render a slice of these types. The template simply ranges through PostsForDate and Posts.
type PostsForDate struct {
Date time.Time
Posts []*Post
}
var Dates []PostsForDate
{{range .Dates}}
<div class="post-date">Posts dated: {{.Date}}</div>
{{range .Posts}}
<div class="post-content">{{.Content}}</div>
{{end}}
{{end}}
A simpler option (that goes against the design philosophy to some degree) is to create a type in Go to record a current value and report changes to that value.
type change struct {
current interface{}
}
func (c *change) Changed(next interface{}) bool {
result := c.current != next
c.current = next
return result
}
func newChange() *change {
return &change{&struct{ int }{}} // initial value ensures that first change is fired.
}
and hook it into a template using a template function:
t := template.Must(template.New("").Funcs(template.FuncMap{"change": newChange}).Parse(` some template `))
Use it in a template like this:
{{ $i := change }}
{{ range $post := .Posts }}
{{ $i.Change $post.Date }}
<div class="post-date">Posts dated: {{ $post.Date }}</div>
{{ end }}
<div class="post-content">{{ $post.Content }}</div>
{{ end }}
playground example
If the post Date field is a time.Time and the posts have different times within a day, then the above does not work as desired. A workaround for this is to check for changes in the rendered date (for example $post.Date.Format "2006-01-02"). Add the following method to simplify this:
func (c *change) ChangedValue(next interface{}) interface{} {
if c.current != next {
c.current = next
return next
}
return nil
}
Use it like this:
{{ $i := change }}
{{ range $post := .Posts }}
{{with $i.ChangedValue ($post.Date.Format "2006-01-02")}}
<div class="post-date">Posts dated: {{.}}</div>
{{ end }}
<div class="post-content">{{ $post.Content }}</div>
{{ end }}
This only works when the values are guaranteed to be considered true by the template package.
This solution does not require parsing the template on every use (as in solution #1 in the other answer) and it applies to arbitrary slice types (unlike both solutions in the other answer).

Render a partial template with passed parameters

I know rendering a partial template with additional parameters is possible in Ruby, how can I do it in Go?
I have a partial template _partial1.tmpl:
<div>
text1
{{if foo}}
text2
{{end}}
</div>
using it from the parent template parent.tmpl:
<div>
{{ template "partial1", }} // how do I pass foo param here??
</div>
How do I pass the parameter foo to the partial?
The documentation states that the template directive has two forms:
{{template "name"}}
The template with the specified name is executed
with nil data.
{{template "name" pipeline}}
The template with the specified name is
executed with dot set to the value of the pipeline.
The latter accepts a pipeline statement which's value is then set to the dot value in the executed template. So calling
{{template "partial1" "string1"}}
will set {{.}} to "string1" in the partial1 template. So while there is no way to set the name foo in the partial, you can pass parameters and they will appear in .. Example:
template.html
<div>
{{ template "partial1.html" "muh"}} // how do I pass foo param here??
</div>
partial1.html
{{if eq . "muh"}}
blep
{{else}}
moep
{{end}}
main.go
import (
"html/template"
"fmt"
"os"
)
func main() {
t,err := template.ParseFiles("template.html", "partial1.html")
if err != nil { panic(err) }
fmt.Println(t.Execute(os.Stdout, nil))
}
Running this program will print the template's contents with blep from the partial. Changing the passed value will change this behaviour.
You can also assign variables, so assigning . to foo is possible in the partial:
{{ $foo := . }}

How to add helper class function in twig template with laravel

in blade template I have used like:
{{ Helper::getName() }}
But how it is used with twig template?
If you are using laravel-twigbridge you can use helpers from default Extensions, e.g:
{{ form_open(...) }}
instead of
{{ Form::open(...) }}
See docs

Optimising html/template Composition

I'm looking to see if there is a better (faster, more organised) way to split up my templates in Go. I strongly prefer to stick to html/template (or a wrapper thereof) since I trust its security model.
Right now I use template.ParseGlob to parse all of my template files in within init().
I apply template.Funcs to the resulting templates
I set a $title in each template (i.e. listing_payment.tmpl) and pass this to the content template.
I understand that html/template caches templates in memory once parsed
My handlers only call t.ExecuteTemplate(w, "name.tmpl", map[string]interface{}) and don't do any silly parsing on each request.
I compose templates from multiple pieces (and this is the bit I find clunky) as below:
{{ $title := "Page Title" }}
{{ template "head" $title }}
{{ template "checkout" }}
{{ template "top" }}
{{ template "sidebar_details" . }}
{{ template "sidebar_payments" }}
{{ template "sidebar_bottom" }}
<div class="bordered-content">
...
{{ template "listing_content" . }}
...
</div>
{{ template "footer"}}
{{ template "bottom" }}
My three questions are:
Is this performant, or do the multiple {{ template "name" }} tags result in a potential per-request performance hit? I see a lot of write - broken pipe errors when stress testing heavier pages. This might just be due to socket timeouts (i.e. socket closing before the writer can finish) rather than some kind of per-request composition, though (correct me if otherwise!)
Is there a better way to do this within the constraints of the html/template package? The first example in Django's template docs approaches what I'd like. Extend a base layout and replace the title, sidebar and content blocks as needed.
Somewhat tangential: when template.ExecuteTemplate returns an error during a request, is there an idiomatic way to handle it? If I pass the writer to an error handler I end up with soup on the page (because it just continues writing), but a re-direct doesn't seem like idiomatic HTTP.
With some help on Reddit I managed to work out a fairly sensible (and performant) approach to this that allows:
Building layouts with content blocks
Creating templates that effectively "extend" these layouts
Filling in blocks (scripts, sidebars, etc.) with other templates
base.tmpl
<html>
<head>
{{ template "title" .}}
</head>
<body>
{{ template "scripts" . }}
{{ template "sidebar" . }}
{{ template "content" . }}
<footer>
...
</footer>
</body>
index.tmpl
{{ define "title"}}<title>Index Page</title>{{ end }}
// We must define every block in the base layout.
{{ define "scripts" }} {{ end }}
{{ define "sidebar" }}
// We have a two part sidebar that changes depending on the page
{{ template "sidebar_index" }}
{{ template "sidebar_base" }}
{{ end }}
{{ define "content" }}
{{ template "listings_table" . }}
{{ end }}
... and our Go code, which leverages the map[string]*template.Template approach outlined in this SO answer:
var templates map[string]*template.Template
var ErrTemplateDoesNotExist = errors.New("The template does not exist.")
// Load templates on program initialisation
func init() {
if templates == nil {
templates = make(map[string]*template.Template)
}
templates["index.html"] = template.Must(template.ParseFiles("index.tmpl", "sidebar_index.tmpl", "sidebar_base.tmpl", "listings_table.tmpl", "base.tmpl"))
...
}
// renderTemplate is a wrapper around template.ExecuteTemplate.
func renderTemplate(w http.ResponseWriter, name string, data map[string]interface{}) error {
// Ensure the template exists in the map.
tmpl, ok := templates[name]
if !ok {
return ErrTemplateDoesNotExist
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl.ExecuteTemplate(w, "base", data)
return nil
}
From initial benchmarks (using wrk) it seems to be a fair bit more performant when it comes to heavy load, likely due to the fact that we're not passing around a whole ParseGlob worth of templates every request. It also makes authoring the templates themselves a lot simpler.

Resources