模板中的条件元素?
Conditional Elements within template?
我对 meteor 很陌生,所以对它的某些方面不熟悉。
我想知道是否可以根据显示的页面/'parent' 级别模板在子模板中显示某些元素。
('parent' 模板)
<template name="aboutPage">
{{> contactForm }}
</template>
联系表
{{#if is aboutPage}}
<a href="#" class="button">HOME</a>
{{/if}}
类似上面的内容。在 contactForm
内部仅当页面为 aboutPage
时才显示 .button
元素。对于不正确的术语,我深表歉意。
我推荐你使用iron:router包。
然后你这样定义路由。
Router.route('/about', function () {
this.render('aboutPage');
});
现在每次用户转到 /aboutPage
时,<template name="aboutPage">
中的所有 html 都会显示。
查看 GitHub
上的 iron:router Guide
另请参阅 iron:router 中的 this examples。
更新(根据用户评论)
创建助手
Template.example.helpers({
showForm:function(){
if(validation){
return true;
}else{
return false;
}
}
})
我们这样使用助手。
<template name="example">
{{#if showForm}}
<!-- Form here -->
{{else}}
<!-- don't show form -->
{{/if}}
</template>
我对 meteor 很陌生,所以对它的某些方面不熟悉。
我想知道是否可以根据显示的页面/'parent' 级别模板在子模板中显示某些元素。
('parent' 模板)
<template name="aboutPage">
{{> contactForm }}
</template>
联系表
{{#if is aboutPage}}
<a href="#" class="button">HOME</a>
{{/if}}
类似上面的内容。在 contactForm
内部仅当页面为 aboutPage
时才显示 .button
元素。对于不正确的术语,我深表歉意。
我推荐你使用iron:router包。
然后你这样定义路由。
Router.route('/about', function () {
this.render('aboutPage');
});
现在每次用户转到 /aboutPage
时,<template name="aboutPage">
中的所有 html 都会显示。
查看 GitHub
上的 iron:router Guide另请参阅 iron:router 中的 this examples。
更新(根据用户评论) 创建助手
Template.example.helpers({
showForm:function(){
if(validation){
return true;
}else{
return false;
}
}
})
我们这样使用助手。
<template name="example">
{{#if showForm}}
<!-- Form here -->
{{else}}
<!-- don't show form -->
{{/if}}
</template>