zhe*_*yue 6 javascript meteor spacebars
我从不同的域提供了所有图像文件,并将该主机名作为变量放入Meteor.settings中.那么,如何在Meteor模板中访问此变量?
例如,在此模板中,img.example.com
用Meteor.settings或其他全局变量中定义的变量替换的最佳实践是什么?我不认为通过使用帮助程序将它传递给每个模板是个好主意.
<template name="products">
{{#each items}}
<img src="http://img.example.com/{{id}}.png">
{{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)
Tom*_*nik 11
您可以通过帮助程序将数据传递到模板的唯一方法.您可以使用全局帮助器:
Template.registerHelper('imgExampleUrl', function() {
return 'img.example.com';
});
Run Code Online (Sandbox Code Playgroud)
然后,您可以在许多模板中使用全局帮助器:
<template name="products">
{{#each items}}
<img src="http://{{imgExampleUrl}}/{{id}}.png">
{{/each}}
</template>
<template name="otherTemplate">
<img src="http://{{imgExampleUrl}}/{{id}}.png">
</template>
Run Code Online (Sandbox Code Playgroud)
或者,如果您想从settings.json获取imgExampleUrl的值
Template.registerHelper('imgExampleUrl', function() {
return Meteor.settings.public.imgExampleUrl;
});
Run Code Online (Sandbox Code Playgroud)
你的settings.json:
{
"public": {
"imgExampleUrl": "img.example.com"
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4239 次 |
最近记录: |