如何在不使用帮助程序的情况下访问Meteor模板中的全局变量? [英] How to access global variables in Meteor template without using a helper?
问题描述
我从不同的域提供了所有图像文件,并将该主机名作为变量放入Meteor.settings中。然后,我如何在Meteor模板中访问此变量?
I have all my image files served from a different domain, and I put that host name as a variable into Meteor.settings. Then, how can I access this variable within a Meteor template?
例如,在此模板中,替换 img.example的最佳做法是什么.com
使用Meteor.settings或其他一些全局变量中定义的变量?我不认为通过使用助手将它传递给每个模板是个好主意。
For example, in this template, what's the best practice to replace img.example.com
with a variable defined in Meteor.settings or some other global variables? I don't think it's a good idea to pass it to every template by using helpers.
<template name="products">
{{#each items}}
<img src="http://img.example.com/{{id}}.png">
{{/each}}
</template>
推荐答案
如何将数据传递到模板的唯一方法是通过帮手。您可以使用全球帮助者:
The only way how you can pass data into your templates is via helpers. You can use global helper:
Template.registerHelper('imgExampleUrl', function() {
return 'img.example.com';
});
然后你可以在许多模板中使用全局助手:
Then you can use global helper in many templates:
<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的值
Or if you want to get value of imgExampleUrl from settings.json
Template.registerHelper('imgExampleUrl', function() {
return Meteor.settings.public.imgExampleUrl;
});
您的settings.json:
Your settings.json:
{
"public": {
"imgExampleUrl": "img.example.com"
}
}
这篇关于如何在不使用帮助程序的情况下访问Meteor模板中的全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!