简体   繁体   English

关闭模板:在大豆文件中通过传递的参数设置全局变量

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

is there a way to set global variables in the .soy file to parameters passed in from .html? 有没有一种方法可以将.soy文件中的全局变量设置为从.html传入的参数? So that all templates would be able to access the global variables to avoid the redundancy of repassing the same parameters to each template. 这样所有模板都可以访问全局变量,从而避免了将相同参数传递给每个模板的冗余。

For example something that would work like this: 例如,可以像这样工作的东西:

HTML: HTML:

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

soy: 大豆:

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

and globalVariable could be accessed from all other templates 和globalVariable可以从所有其他模板访问

My experience with Google Closure Templates is limited to the Java backend in Atlassian plugin development, however, the templates use a reserved variable for global data: $ij . 我对Google Closure模板的经验仅限于Atlassian插件开发中的Java后端,但是,这些模板使用保留变量存储全局数据: $ij The following is taken from the Injected Data section of the documentation: 以下内容摘自文档的“ 注入数据”部分:

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. 您不需要对注入的数据使用@param声明,也不需要手动将其传递给调用的子模板。

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. 在JavaScript中,您可以通过第三个参数传递注入的数据。

 // 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. 在Java中,使用Tofu后端,可以使用Renderer上的setIjData方法注入数据。

 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. 尽管call标签上没有任何数据属性,但下面的模板的行为与上面的“ .example”模板相同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM