闭包模板:从 soy 文件中传递的参数设置全局变量

Closure Templates: setting global variable from passed paramater in soy file

有没有办法将.soy文件中的全局变量设置为从.html传入的参数?这样所有模板都能够访问全局变量,以避免将相同参数重新传递给每个模板的冗余。

例如,可以这样工作的东西:

HTML:

document.write(wet4.gcweb.setGlobal({templatedomain:"example.ca"}));    

大豆:

/**
 * Test.
 * @param templatedomain 
 */
{template .setGlobal}
globalVariable = $templatedomain
{/template}

并且可以从所有其他模板访问 globalVariable

我对 Google 闭包模板的体验仅限于 Atlassian 插件开发中的 Java 后端,但是,模板使用全局数据的保留变量:$ij。以下内容摘自文档的 Injected Data 部分:

Injected data is data that is available to every template. You don't need to use the @param declaration for injected data, and you don't need to manually pass it to called subtemplates.

Given the template:

{namespace ns autoescape="strict"}

/** Example. */
{template .example}
  foo is {$ij.foo}
{/template}

In JavaScript, you can pass injected data via the third parameter.

// The output is 'foo is injected foo'.
output = ns.example(
    {},  // data
    null,  // optional output buffer
    {'foo': 'injected foo'})  // injected data

In Java, using the Tofu backend, you can inject data by using the setIjData method on the Renderer.

SoyMapData ijData = new SoyMapData();
ijData.put("foo", "injected foo");

SoyTofu tofu = ...;
String output = tofu.newRenderer("ns.example")
    .setIjData(ijData)
    .render();

Injected data is not scoped to a function like parameters. The templates below behave in the same way as the ".example" template above despite the lack of any data attribute on the call tag.

{namespace ns autoescape="strict"}

/** Example. */
{template .example}
  {call .helper /}
{/template}

/** Helper. */
{template .helper private="true"}
  foo is {$ij.foo}
{/template}