Escaping special characters for templates
Gosu templates use standard characters to indicate the beginning of a special block of Gosu code or other template structures. In some cases, to avoid ambiguity for the Gosu parser you must escape special characters.
For non-template-tag use, escape ${ or <%
Gosu templates use the following two-character sequences to begin a template expression
${<%
In a String
literal in your code, if you want to use these sequences to indicate
template tags, you do not need to escape these special characters.
If you require either of those two special
two-character sequences in your String,
rather than as a template tag, escape the first character of that sequence.
To escape a character, add a backslash character immediately before the
character to escape. For example:
- To define a variable containing the non-template text
"Hello${There":var s = "Hello\${There" - To define a variable containing the non-template text
"Hello<%There":var s = "Hello\<%There"
If your String uses the initial character
of the template sequence, but the next character does not complete that
sequence, you do not need to escape the character. For example:
- To define a variable containing the non-template text
"Hello$There", just use:var s = "Hello$There" - To define a variable containing the non-template text
"Hello<There", just use:var s = "Hello<There"
Within template tag blocks, use standard Gosu escaping rules
In typical use, if you defined a String, you must escape any quotation
marks with the syntax \"
to avoid ambiguity about the end of the String. For example:
var quotedString = "\"This string has quotation marks around it\", is that correct?"This code creates a String with the following value,
including quotation marks:
"This string has quotation marks around it", is that correct?If you use a template, this rule does not apply between your template-specific opening and closing tags that contain Gosu code. Instead, use standard Gosu syntax for the code between those open and closing tags.
For example, the following two lines are valid Gosu code:
var s1 = "${ "1" }"
var s2 = "${ "1" } \"and\" ${ "1" }"
Note that the first character within the template’s Gosu block is an unescaped quotation mark.
The following code is invalid because of incorrect escaping of internal quotation marks:
var s = "${ \"1\" }"In this invalid case, the first character in the template’s Gosu block is an escaped quotation mark.
In the rare case that your Gosu code requires
creating a String literal
containing a quotation mark character, remember that the standard Gosu
syntax rules apply. You need to escape any quotation marks inside the
String literal. For example,
the following is valid Gosu:
var quotedString = "${ "\"This string has quotation marks around it\", is that correct?" }"Note that the first character
in the template’s Gosu block is an unescaped quotation mark. This template
generates a String with
the value:
"This string has quotation marks around it", is that correct?