检测哪个模板包含了 Meteor 中的当前模板 [英] Detect which template includes the current one in Meteor
问题描述
我想检测哪个模板包含另一个模板,因为我只想为一个特定模板创建一个 css 类.
这是它应该如何工作:
<div class="panel panel-default {{#if templateX}}specialCSS{{/if}}" id="{{_id}}">模板>Template.includedTempl.helpers({模板X:函数(){如果 (this.includedBy('templX.html')) {返回真;}返回假;}});
我该怎么做?任何帮助将不胜感激.
一般来说,子模板确定其父上下文可能具有挑战性,因为它可能嵌套任意次数.因此,父母通常更容易为孩子提供触发所需行为的上下文.举个例子:
app.html
{{>parentTemplate parentContext}}<模板名称="parentTemplate">{{>childTemplate specialContext}}{{>子模板}}模板><模板名称=childTemplate"><div class="{{isSpecialClass}}"><p>父名称:{{name}}</p>
模板>
app.js
if (Meteor.isClient) {Template.body.helpers({//向父级添加一些上下文 do demo 如何修改它父上下文:{名称:'戴夫'}});Template.parentTemplate.helpers({特殊上下文:函数(){//复制父数据上下文var data = _.clone(Template.instance().data || {});//修改上下文以表明孩子是特殊的data.isSpecial = true;返回数据;}});Template.childTemplate.helpers({isSpecialClass: 函数 () {//获取这个孩子的上下文(注意它可以是未定义的)var data = Template.instance().data;如果(数据&& data.isSpecial)//如果这个孩子很特别,就添加awesome"类返回真棒";}});}
这里,父模板创建了两个子模板.第一个孩子被赋予父上下文和 isSpecial
.带有 isSpecial
的模板将包含一个带有 awesome
类的 div
.唯一棘手的部分是父母和孩子的助手都必须使用 Template.instance确定它们的上下文.
推荐阅读
I want to detect which template includes another, because I want to have a css class only for one specific template.
Here is how it should work:
<template name="includedTempl">
<div class="panel panel-default {{#if templateX}}specialCSS{{/if}}" id="{{_id}}">
</template>
Template.includedTempl.helpers({
templateX: function() {
if (this.includedBy('templX.html')) {
return true;
}
return false;
}
});
How can I do this? Any help would be greatly appreciated.
In general it can be challenging for a child template to determine its parent context because it may be nested an arbitrary number of times. Therefore, it's often easier for the parent to provide a context to the child which triggers the desired behavior. Here's an example:
app.html
<body>
{{> parentTemplate parentContext}}
</body>
<template name="parentTemplate">
{{> childTemplate specialContext}}
{{> childTemplate}}
</template>
<template name="childTemplate">
<div class="{{isSpecialClass}}">
<p>parent name: {{name}}</p>
</div>
</template>
app.js
if (Meteor.isClient) {
Template.body.helpers({
// add some context to the parent do demo how it can be modified
parentContext: {name: 'dave'}
});
Template.parentTemplate.helpers({
specialContext: function () {
// make a copy of the parent data context
var data = _.clone(Template.instance().data || {});
// modify the context to indicate the child is special
data.isSpecial = true;
return data;
}
});
Template.childTemplate.helpers({
isSpecialClass: function () {
// grab the context for this child (note it can be undefined)
var data = Template.instance().data;
if (data && data.isSpecial)
// add the 'awesome' class if this child is special
return 'awesome';
}
});
}
Here, the parent template creates two child templates. The first child is given the parent context along with isSpecial
. The template with isSpecial
will contain a div
with the awesome
class. The only tricky part is that both the parent and the child helpers must use Template.instance to determine their context.
Recommended Reading
这篇关于检测哪个模板包含了 Meteor 中的当前模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!