模板中的条件元素

Conditional Elements within template?

本文关键字:条件 元素      更新时间:2023-09-26

我对流星很陌生,所以我对它的某些方面不熟悉

我想知道是否可以根据显示的页面/"父"级模板在子模板中显示某些元素。

("父"模板)

<template name="aboutPage">
     {{> contactForm }}
</template>

联系人表单

{{#if is aboutPage}}
    <a href="#" class="button">HOME</a>
{{/if}}

沿着上面的线的东西。仅当页面为aboutPage时,才会在contactForm内部显示.button元素。我为不正确的术语道歉。

我建议您使用iron:router包。

你这样定义路线。

 Router.route('/about', function () {
      this.render('aboutPage');
    });

现在,每当用户转到/aboutPage时,<template name="aboutPage">中的所有html都会显示出来。

看看铁:GitHub 上的路由器指南

还可以看看iron:router的例子。

更新(基于用户评论)创建辅助

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>