Meteor.js 应用程序模板中的多产量 [英] Multiple yield in Meteor.js application template

查看:14
本文介绍了Meteor.js 应用程序模板中的多产量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在布局文件中有一个用于 Iron-router 的通用 {{>yield}},用于呈现我的页面,即模板.

在我的一个页面中,我有一个侧边菜单,根据此菜单中的选择,我想在此页面中加载与此页面相关的不同模板.

我怎样才能做到这一点?

解决方案

我使用 Iron-router 的 layout template 选项做了类似的事情.假设我想在这个主视图中创建一个包含多个视图/模板的主视图,该视图将发生变化.首先我要声明我的路线:

Router.map(function () {this.route('家', {小路: '/',模板:'myHomeTemplate',layoutTemplate: '布局',产量模板:{'myAsideTemplate': {to: 'aside'},'myFooter': {to: 'footer'}}});});

布局模板的 html 如下所示:

 <模板名称="布局"><旁边>{{>产量区域='aside'}}</一边><div>{{>屈服}}

<页脚>{{>yield region='footer'}}</页脚>

以便在产量 asidefooter 中指定的模板在指定的位置呈现.对于您的情况,您可以指定 sidemenu 收益.

不,您有基本的布局和想法,您可以定义另一条路线.假设我们称之为 differentHome.

Router.map(function () {this.route('不同的家', {路径:'/不同的家',模板:'myHomeTemplate',layoutTemplate: '布局',产量模板:{'myDifferentAsideTemplate': {to: 'aside'},'myDifferentFooter': {to: 'footer'}}});});

注意此路由声明,我正在更改产量模板,但我不会更改将在主产量中呈现的基本模板.现在在一个事件中,您可以重新路由这将改变两个不同的收益模板:

Router.go("differentHome");

或者您可以使用 html 进行路由,例如使用链接.

编辑(偶然的解决方案):

使用会话变量来指示侧边菜单选项.

HTML:<模板名称="main">......<div class="sideMenu">{{#if sideMenu1}}{{>side1Template}}{{/如果}}{{#if sideMenu2}}{{>side2Template}}{{/如果}}

JS:模板.main.helpers({sideMenu1:函数(){return Session.equals("sideMenuChoice", "sideMenu1")},sideMenu2:函数(){return Session.equals("sideMenuChoice", "sideMenu2")}});

现在在一个事件中将 Session 设置为 sideMenuChoice.

I have one general {{>yield}} for iron-router in a layout file which renders my pages, which are templates.

In one of my pages, I have a side menu and according to the selection in this menu, I want to load different templates related to this page in this page.

How can I achieve this?

解决方案

I have done a similar thing using iron-router's layout template option. Say I want to create a home view with multiple views/templates inside of this home view that will change. First I would declare my route:

Router.map(function () {
  this.route('home', {
  path: '/',
  template: 'myHomeTemplate',
  layoutTemplate: 'layout',
  yieldTemplates: {
    'myAsideTemplate': {to: 'aside'},
    'myFooter': {to: 'footer'}
    }
  });
});

Where the html for the layout template would look like:

 <template name="layout">
   <aside>
     {{> yield region='aside'}}
   </aside>

  <div>
    {{> yield}}
  </div>

  <footer>
    {{> yield region='footer'}}
  </footer>
</template>

So that the templates specified in the yields aside and footer get rendered in the specified spot. For your case you can specify a sidemenu yield.

No that you have the basic layout and idea you can define another route. Say we call it differentHome.

Router.map(function () {
  this.route('differentHome', {
  path: '/differentHome',
  template: 'myHomeTemplate',
  layoutTemplate: 'layout',
  yieldTemplates: {
    'myDifferentAsideTemplate': {to: 'aside'},
    'myDifferentFooter': {to: 'footer'}
    }
  });
});

Notice on this route declaration I am changing the yield templates, but I am not changing the basic template that will be rendered in the main yield. Now on an event you can re-route which will change the two different yields templates:

Router.go("differentHome"); 

Or you can use html to route, say with a link.

EDIT (Haphazard Solution):

Use Session Variable To Dictate Side Menu choice.

HTML:
<template name="main">
  ......
   <div class="sideMenu">   
     {{#if sideMenu1}}
        {{> side1Template}}
     {{/if}}

     {{#if sideMenu2}}
        {{> side2Template}}
     {{/if}}
   </div>
 </template>

JS:
Template.main.helpers({
    sideMenu1 : function () {
         return Session.equals("sideMenuChoice", "sideMenu1")  
    },
    sideMenu2 : function () {
         return Session.equals("sideMenuChoice", "sideMenu2")  
    }
 });

Now on an event set the Session to what ever sideMenuChoice.

这篇关于Meteor.js 应用程序模板中的多产量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆