我从不同的域提供了所有图像文件,并将该主机名作为变量放入Meteor.settings中.那么,如何在Meteor模板中访问此变量?
例如,在此模板中,使用Meteor.settings或其他全局变量中定义的变量替换img.example.com的最佳做法是什么?我不认为通过使用帮助程序将它传递给每个模板是个好主意.
<template name="products">
{{#each items}}
<img src="http://img.example.com/{{id}}.png">
{{/each}}
</template>
解决方法
您可以通过帮助程序将数据传递到模板的唯一方法.你可以使用
global helper:
Template.registerHelper('imgExampleUrl',function() {
return 'img.example.com';
});
然后,您可以在许多模板中使用全局帮助器:
<template name="products">
{{#each items}}
<img src="http://{{imgExampleUrl}}/{{id}}.png">
{{/each}}
</template>
<template name="otherTemplate">
<img src="http://{{imgExampleUrl}}/{{id}}.png">
</template>
或者,如果您想从settings.json获取imgExampleUrl的值
Template.registerHelper('imgExampleUrl',function() {
return Meteor.settings.public.imgExampleUrl;
});
你的settings.json:
{
"public": {
"imgExampleUrl": "img.example.com"
}
}