How do I escape “{{” and “}}” delimiters in Go templates?

GoGo Templates

Go Problem Overview


I’m using AngularJS as the front-end JS library, with Go templates within Revel framework to to generate the markup on the back-end.

But both Go and Angular use {{ and }} for delimiters in their templates. How can I escape them in Go to pass them to AngularJS?

Go Solutions


Solution 1 - Go

{{"{{"}}
{{"}}"}}

produces

{{
}}

Solution 2 - Go

A simple workaround would be using

{{`{{Your.Angular.Data}}`}}

Solution 3 - Go

I don't know how to escape it, but you could choose a different delimiter instead using Delims:

func (t *Template) Delims(left, right string) *Template

According to the mailing list, this is probably the best option. The argument was that if you escape it, your templates will be hard to read, so it would probably be better anyway to change the delimiter instead of trying to hack around it.

Solution 4 - Go

In Revel, there is a way to handle it:

In /conf/app.conf, add this line:

template.delimiters="[[ ]]"

It will use [[]] instead of using default {{}}, you can also use:

template.delimiters="{{{ }}}"

So, for revel, it uses {{{ }}}, for angularJS, it uses {{ }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCoder1View Question on Stackoverflow
Solution 1 - GoMostafaView Answer on Stackoverflow
Solution 2 - GoJSNoobView Answer on Stackoverflow
Solution 3 - GobeatgammitView Answer on Stackoverflow
Solution 4 - GoTimothy YeView Answer on Stackoverflow