【问题标题】:Template not rendering with Iron: Router模板不使用 Iron 渲染:路由器
【发布时间】:2015-02-15 12:40:40
【问题描述】:

我正在尝试在 Iron: Router 中设置 layoutTemplate。我已经阅读了我在自己的项目中实现的文档(以减少重复代码)。不过有些不对劲。内部模板正在渲染,但不是布局。

位于'client/templates/defaultlayout.html'的布局文件

<template name="defaultlayout">
  <head>
    <title>Grocery Getter</title>
  </head>
  <div class="container">
    <header>
      {{> yield "headerButtons"}}
      <h1>{{title}}</h1>
      {{> loginButtons}}
      {{> yield "headertextinput"}}
    </header>
    <body>
      {{> yield}}
    </body>
  </div>
</template>

这是我要填充位于“client/templates/login.html”的“{{> yield}}”部分的模板:

<template name="login">
  <h4>Please login or create an account.</h4>
</template>

这是我的 Iron:位于 'client/routes.js' 的路由器东西:

Router.configure({
  layoutTemplate: 'defaultlayout'
});
Router.route('/',function()
{
  if(!Meteor.user())
    {
      this.redirect('/login');
    }
  else
    {
      this.redirect('/householdsignup');
    }
});

Router.route('/login',function()
{
  if(!Meteor.user())
    {
      this.render('login', function(){
        data: 'Grocery Getter'
      });
    }
    else
    {
      this.redirect('/householdsignup');
    }
});

我不确定我在这里做错了什么。

提前感谢您的帮助!!!

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    您的布局在我看来无效。据我所知,模板不能有头。你没有收到流星的错误吗?

    试试这个:

    main.html:

    <head>
      <title>Grocery Getter</title>
    </head>
    

    defaultlayout.html:

    <template name="defaultlayout">
      <div class="container">
        <header>
          {{> yield "headerButtons"}}
          <h1>{{title}}</h1>
          {{> loginButtons}}
          {{> yield "headertextinput"}}
        </header>
        {{> yield}}
      </div>
    </template>
    

    此外,您可能希望将 routes.js 移动到共享的客户端/服务器空间中,例如,在项目根目录中。

    【讨论】:

    • 谢谢!结果很完美!我正在一点一点地学习如何在这个平台上写作。
    猜你喜欢
    • 2015-05-24
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2015-12-24
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多