简体   繁体   English

如何在Meteor JS中使用路由器动态更改主体模板?

[英]How to change body template dynamically by using router in Meteor JS?

I need to know about to change body templates dynamically whenever click button in meteor js.For example total 3 templates are there in my app. 我需要知道每次在流星js中单击按钮时都会动态更改主体模板。例如,我的应用程序中总共有3个模板。

  1. one template is header template 一个模板是标题模板
  2. 2nd one is side menu template 第二个是侧面菜单模板
  3. and another is content template 另一个是内容模板

Whenever click menu items then only changing to content template by using router package and not goes to new page.So please suggest me what to do for this? 每当单击菜单项时,仅通过使用路由器软件包更改为内容模板,而不转到新页面。因此,请建议我该怎么做?

Thanks in Advance. 提前致谢。

This is do-able using the layout and yeild functionnalities of the router. 使用路由器的布局和等级功能可以做到这一点。 As defined in this awesom tutorial http://www.manuel-schoebel.com/blog/iron-router-tutorial 如本awesom教程中所定义:http://www.manuel-schoebel.com/blog/iron-router-tutorial

In Meteor we only render and rerender parts of the dom and not always the whole thing. 在Meteor中,我们仅渲染和重新渲染dom的一部分,而并不总是渲染整个对象。 As an example we want to have a layout that specifies a menu, a footer and an area where the main content is rendered in. 作为示例,我们希望有一个布局,该布局指定菜单,页脚和显示主要内容的区域。

This gives this kind of html 这给出了这种HTML

<template name="complexLayout">
 <div class="left">
    {{> yield region="menu"}}
  </div>


  <div class="main">
    {{> yield}}
  </div>
  <div class="bottom">
    {{> yield region="footer"}}
  </div>
</template>

In this template are three areas that the Iron-Router renders in. One 'Main-Yield' and two 'Named-Yields' or 'Regions' with the names 'menu' and 'footer'. 在此模板中,Iron-Router呈现了三个区域。一个名为“ Main-Yield”的区域,两个名为“ menu”和“ footer”的“ Named-Yields”或“ Regions”。 The names are important if we want to specify what template we want to render where. 如果我们要指定要在哪里渲染的模板,则名称很重要。 We now want that the Iron-Router to use our layout and to render the template 'myMenu' to the yield with the name 'menu' and the template 'myFooter' to the yield named 'footer'. 现在,我们希望Iron-Router使用我们的布局,并将模板“ myMenu”渲染为名称为“ menu”的良品,并将模板“ myFooter”渲染为名为“ footer”的良品。

this.route('home', {
  path: '/',
  layoutTemplate: 'complexLayout',
  yieldTemplates: {
    'myMenu': {to: 'menu'},
    'myFooter': {to: 'footer'}
  }
});

Using this, all routes (or targeted ones) may have the same layout and have only some parts changed depending on the current page. 使用此功能,所有路由(或目标路由)可能具有相同的布局,并且仅更改了某些部分,具体取决于当前页面。 Do not forget that one can use the Router.configure function to set defaults. 不要忘记,可以使用Router.configure函数设置默认值。

Router.configure({
  layoutTemplate: 'complexLayout',
  notFoundTemplate: 'notFound',
  loadingTemplate: 'loading'
});

Doing this will allow the developer to define the default layouts to use on different states of the application. 这样做将使开发人员可以定义要在应用程序的不同状态下使用的默认布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM