【问题标题】:Closure Templates: setting global variable from passed paramater in soy file闭包模板:从大豆文件中传递的参数设置全局变量
【发布时间】:2015-05-14 15:46:39
【问题描述】:

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

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

HTML:

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

大豆:

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

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

【问题讨论】:

    标签: html google-closure-templates soy-templates


    【解决方案1】:

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

    注入的数据是可用于每个模板的数据。你不 需要对注入的数据使用@param 声明,而您不需要 需要手动将其传递给被调用的子模板。

    给定模板:

    {namespace ns autoescape="strict"}
    
    /** Example. */
    {template .example}
      foo is {$ij.foo}
    {/template}
    

    在 JavaScript 中,您可以通过第三个参数传递注入的数据。

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

    在 Java 中,使用 Tofu 后端,您可以使用 渲染器上的 setIjData 方法。

    SoyMapData ijData = new SoyMapData();
    ijData.put("foo", "injected foo");
    
    SoyTofu tofu = ...;
    String output = tofu.newRenderer("ns.example")
        .setIjData(ijData)
        .render();
    

    注入的数据不限于像参数这样的函数。这 下面的模板与“.example”模板的行为方式相同 尽管调用标签上没有任何数据属性,但上述内容。

    {namespace ns autoescape="strict"}
    
    /** Example. */
    {template .example}
      {call .helper /}
    {/template}
    
    /** Helper. */
    {template .helper private="true"}
      foo is {$ij.foo}
    {/template}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      相关资源
      最近更新 更多