Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 781
Coder1 Avatar asked Jul 14 '13 17:07

Coder1


People also ask

What is go template language?

Go's template is designed to be extended by developers, and provides access to data objects and additional functions that are passed into the template engine programmatically. This tutorial only uses functions universally provided in the text/template package, and does not discuss the specifics of data access.

What is a template in go?

Go template uses names for its objects that are not immediately familiar. Once you have familiarized yourself with the concepts, the canonical documentation becomes easier to read. Not surprisingly, the template is the entire text that is submitted to the template engine to be rendered. The template consists of text and "actions".

Why can't I read nested go template code?

Go template outputs everything between actions, including whitespace and line feeds. This can make it challenging to read nested template code. If an action's left delimiter is followed immediately by a minus sign and ASCII space character { {- , all trailing white space is trimmed from the immediately preceding text.

How to insert escape characters in interpreted string literals?

To insert escape characters, use interpreted string literals delimited by double quotes. const s = " First line " + "Second line" fmt.Println(s) First line Second line The escape character denotes a horizontal tab and is a line feed or newline. Double quote escape Use \"to insert a double quote in an interpreted string literal:

Is it possible to print in go template?

0. Go template provides a very limited number of functions universally, and instead relies on the specific implementation to add functions to enhance the user experience. However, print is one of the available functions. Later in the tutorial, there is a list of the built-in functions provided by all Go template environments.


2 Answers

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

produces

{{ }} 
like image 177
Mostafa Avatar answered Oct 06 '22 12:10

Mostafa


A simple workaround would be using

{{`{{Your.Angular.Data}}`}} 
like image 44
JSNoob Avatar answered Oct 06 '22 12:10

JSNoob