I am trying to teach myself Closure templates. I made a simple file simply.soy:
{namespace examples.simple}
/**
* says hello to the world
* @param? name Optional parameter specifying who you are greeting.
*/
{template .hiWorld}
Hello
{if $name}
{$name}!
{else}
world!
{/if}
{/template}
After I compile and call document.write(examples.simple.hiWorld();
, however, the displayed string has no space between "Hello" and "world": Helloworld!
Why not?
Closure Templates handle line joining as follows:
Within the body of a template, you can indent the lines as much as you want because the template compiler removes all line terminators and whitespace at the beginning and end of lines, including spaces preceding a rest-of-line comment. The compiler completely removes empty lines that consist of only whitespace. Consecutive lines are joined according to the following heuristic: if the join location borders a template or HTML tag on either side, the lines are joined with no space. If the join location does not border a template or HTML tag on either side, the lines are joined with exactly one space.
To add a space in a Closure Templates where one is needed, use the special character command {sp}
. In cases where an unwanted space is inserted, you may remove it using the command {nil}
. For line joining examples, see features.soy.
simple.soy would become:
{namespace examples.simple}
/**
* says hello to the world
* @param? name Optional parameter specifying who you are greeting.
*/
{template .hiWorld}
Hello{sp}
{if $name}
{$name}!
{else}
world!
{/if}
{/template}