This question already has an answer here:
Code-Syntax Collision
Golang or Iris-go uses {{ .VariableName }} to interpret the variable name or they use {{ }} for parsing other function and code too. Now, when I tried framework7 code below in people.html page
{{#each people}}
<li>{{this}}</li>
{{/each}}
I got the error about.html:26: unexpected "#" in command
I was expecting the JS code for framework to be executed
I got the error about.html:26: unexpected "#" in command as Golang is trying to parse the template code inside {{ }}
How can i ask Golang not to parse anything inside that particular syntax and live it for javascript to handle it.
</div>
You may use the Template.Delims()
method to change the delimeters used by the Go template engine. If you change this, it won't collide with the delims used by framework7.
Example changing Go template delims to [[
and ]]
:
func main() {
t := template.Must(template.New("").Delims("[[", "]]").Parse(tmpl))
if err := t.Execute(os.Stdout, "test"); err != nil {
panic(nil)
}
}
const tmpl = `[[.]]
{{#each people}}
<li>{{this}}</li>
{{/each}}`
Output (try it on the Go Playground)
test
{{#each people}}
<li>{{this}}</li>
{{/each}}
If this is inconvenient for you, or you just want some specific actions be left "unprocessed" by the Go template engine, you may also choose to "escape" these specific parts in the Go template, by transforming them into actions which simply output the similar actions for framework7. For example to output {{#each people}}
, in the Go template use:
{{"{{#each people}}"}}
A working example:
func main() {
t := template.Must(template.New("").Parse(tmpl))
if err := t.Execute(os.Stdout, "test"); err != nil {
panic(nil)
}
}
const tmpl = `{{.}}
{{"{{#each people}}"}}
<li>{{"{{this}}"}}</li>
{{"{{/each}}"}}`
Output (try it on the Go Playground):
test
{{#each people}}
<li>{{this}}</li>
{{/each}}