MeteorJs模板条件显示不同的模板

时间:2022-04-27 20:30:33

I have a meteorjs default layout file and i am routing to different pages using iron-route, What i am trying to do is not show navigation on the home page. I can do this by using two separate files but i want to do this with single file.

我有一个meteorjs默认布局文件,我使用铁路线路由到不同的页面,我想要做的是不在主页上显示导航。我可以通过使用两个单独的文件来完成此操作,但我想用单个文件执行此操作。

<template name="layout">

    <head>
        <title>
            {{title}}
        </title>
    </head>
    <body>

        {{> navigation }}

        <div class="nav_padding">
            {{> yield}}
        </div>


        {{> footer}}

    </body>

    </template>

My route

    Router.route('/', function(){
        this.layout('homeLayout');
        this.render('home', {
            data:{title: 'some'}
        });
    });

1 个解决方案

#1


There are a few ways you can do this, but here's a simple solution...

有几种方法可以做到这一点,但这是一个简单的解决方案......

Add a helper to your layout template which indicates if the nav should be shown based on the route name:

在布局模板中添加一个帮助器,指示是否应根据路径名称显示导航:

Template.myLayout.helpers({
  showNav: function() {
    return Router.current().route.getName() !== 'home';
  }
});

Modify your template to conditionally show the navigation template:

修改模板以有条件地显示导航模板:

<template name="myLayout">
  {{#if showNav}}
    {{> navigation }}
  {{/if}}
  <div class="nav_padding">
    {{> yield}}
  </div>
  {{> footer}}
</template>

In order for your route to be named you may need to modify your route to something like this:

为了命名您的路线,您可能需要修改您的路线,如下所示:

Router.route('/', {
  name: 'home',
  template: 'home',
  layoutTemplate: 'myLayout',
  data: {title: 'some'}
});

#1


There are a few ways you can do this, but here's a simple solution...

有几种方法可以做到这一点,但这是一个简单的解决方案......

Add a helper to your layout template which indicates if the nav should be shown based on the route name:

在布局模板中添加一个帮助器,指示是否应根据路径名称显示导航:

Template.myLayout.helpers({
  showNav: function() {
    return Router.current().route.getName() !== 'home';
  }
});

Modify your template to conditionally show the navigation template:

修改模板以有条件地显示导航模板:

<template name="myLayout">
  {{#if showNav}}
    {{> navigation }}
  {{/if}}
  <div class="nav_padding">
    {{> yield}}
  </div>
  {{> footer}}
</template>

In order for your route to be named you may need to modify your route to something like this:

为了命名您的路线,您可能需要修改您的路线,如下所示:

Router.route('/', {
  name: 'home',
  template: 'home',
  layoutTemplate: 'myLayout',
  data: {title: 'some'}
});