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

4

66

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?

Socorrosocotra answered 14/7, 2013 at 17:37 Comment(0)
E
35

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.

Equivalent answered 14/7, 2013 at 17:45 Comment(3)
Worth mentioning, this change can be done on the Angular side too.Fenestrated
@tjameson Thanks, that also helped me find the revel framework supports it as a config. To change it to [[ and ]], it would be dfined in app.conf as template.delimiters = "[[ ]]".Socorrosocotra
@BenjaminGruenbaum Great info, I wasn't aware, but now that I think about it, hardly surprised they already have a solution for that. Thanks.Socorrosocotra
N
114
{{"{{"}}
{{"}}"}}

produces

{{
}}
Nancynandor answered 14/7, 2013 at 18:37 Comment(4)
@Julien Yeah, because the GoLang templater is very strict and escapes the characters for you. You'll have to jump through a couple hoops to get it to accept them as raw, safe characters.Vizard
@WesAlvaro Using html/template is expected to have the url encoded result. Change the html/template to text/template works, please refer to go-vim.appspot.com/p/Cj_n9mvnxgPedestrianize
@ChouW Not quite. That's not what I was demonstrating. The [src] attribute of the <img> is treated special and gets its contents encoded. The answer isn't to switch to a different template type, that would cause other issues.Vizard
@WesAlvaro I got it. You want to heads-up that we should take care when we are handling HTML tags, right?Pedestrianize
X
68

A simple workaround would be using

{{`{{Your.Angular.Data}}`}}
Xeroderma answered 14/8, 2016 at 10:2 Comment(1)
This doesn't work if your template data is a Go string. In other words, var templateData = `{{`{{Angular.Data}}`}}` is invalid Go (in that trivial example, you don't need to use tick-strings, but in real world examples this is useful).Joyous
E
35

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.

Equivalent answered 14/7, 2013 at 17:45 Comment(3)
Worth mentioning, this change can be done on the Angular side too.Fenestrated
@tjameson Thanks, that also helped me find the revel framework supports it as a config. To change it to [[ and ]], it would be dfined in app.conf as template.delimiters = "[[ ]]".Socorrosocotra
@BenjaminGruenbaum Great info, I wasn't aware, but now that I think about it, hardly surprised they already have a solution for that. Thanks.Socorrosocotra
C
-4

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 {{ }}

Conservative answered 30/3, 2016 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.