How to write Iron-router nested template?

别来无恙 提交于 2019-12-10 16:33:32

问题


I have 2 templates:

1) mainLayout.html -> general layout which must be used by all pages. (e.g. page title, navbar, footer)

Router.configure({
  layoutTemplate: 'mainLayout'
})

<template name="mainLayout">
  {{> header}}
  <div class="container-fluid">
    {{> yield}}
  </div>
</template>

2) specialLayout.html -> custom layout which is inheriting main-layout.html but with additional template (e.g. side/tab menu)

How should I define specialLayout? Note that specialLayout should have the title, navbar, and footer defined in mainLayout.

If I have route X which want to use specialLayout, how should I write it?


回答1:


I don't know any simple method at this moment, but most things can be achieved by several less elegant ways:

  • Copy your common parts into separate templates and use them in both layouts, i.e.:

    <template name="mainLayout">
      {{> navbar}}
      <div>
        {{> content}}
      </div>
      {{> footer}}
    </template>
    
    <template name="specialLayout">
      {{> navbar}}
      <div>
        {{> content}}
        {{> sidebar}}
      </div>
      {{> footer}}
    </template>
    
  • Make your special part an option in the main layout instead of a separate one:

    <template name="mainLayout">
      ...
      <div>
        {{#if layout.renderSidebar}}
          <div class="col-2">>
            {{> yield 'sidebar'}}
          </div>
        {{/if}}
    
        <div class="{{#if layout.renderSidebar}} col-10 {{else}} col-12 {{/if}}">
          {{> yield}}
        </div>
      </div>
      ...
    </template>
    

    Then in the appropriate routes enable sidebar in the data:

    this.map('pathName', {
      ...
      data: function() {
        return {
          layout: {renderSidebar: true},
          ...
        };
      },
    });
    


来源:https://stackoverflow.com/questions/25926176/how-to-write-iron-router-nested-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!