gpt4 book ai didi

google-closure-templates - 为什么我的 Closure 模板中 "Hello"后面没有空格?

转载 作者:行者123 更新时间:2023-12-02 21:01:35 26 4
gpt4 key购买 nike

我正在尝试自学闭包模板。我做了一个简单的文件 simple.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}

编译并调用 document.write(examples.simple.hiWorld(); 后,显示的字符串“Hello”和“world”之间没有空格:Helloworld!

为什么不呢?

最佳答案

Closure Templates handle line joining如下:

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.

要在闭包模板中需要的地方添加空格,请使用特殊字符命令{sp}。如果插入了不需要的空格,您可以使用命令{nil}将其删除。有关行连接示例,请参阅 features.soy

simple.soy 将变成:

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

关于google-closure-templates - 为什么我的 Closure 模板中 "Hello"后面没有空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10941827/

26 4 0