2017-01-03 76 views
1

我阅读了很多文章,其中包括good tutorial,但我仍然无法找到我的代码出了什么问题。我的意图是配置我正在设计的网页,以便能够使用Iron Router。如何使用IronRouter和流星将模板呈现到模板中

它基本上显示了带有字幕的图片数量,当用户选择一个图片时,消息以新的模板和格式显示(这就是为什么我需要铁路路由器)。

我在设置时遇到了麻烦:ApplicationLayout模板应该为每个用户渲染提升模板,以便显示他/她的所有图片。但是,当它显示时,我得到body.html的html,但不是boost.html中的。

Java脚本助手和事件处理程序正常工作;我已经检查过了。这就是为什么我只包含body.html,boost.html和routes.js的代码。请注意,该代码对应于三个不同的文件: 这两个html文件都位于同一文件夹(ui)中,而js文件位于lib文件夹中,都在我的项目目录中。

在此先感谢!

body.html

<template name="ApplicationLayout"> 
    <body> 
    {{> sAlert}} 
     <div id="background"> 
     <img src="http://s1.picswalls.com/wallpapers/2015/11/21/beautiful-motivation-wallpaper_10342177_288.jpg" class="stretch" alt="" /> 
    </div> 
     <div class="container"> 
      <header> 
       {{#if currentUser}} 
       <h1>Your Boosts! ({{incompleteCount}})</h1> 
       {{else}} 
       <h1>Community Boosts!</h1> 
       {{/if}} 
       {{> undoRedoButtons}} 

       {{> loginButtons}} 

       {{#if currentUser}} 
       <form class="new-boosts"> 
        <input type="text" name="text" placeholder="Type to add a short description"/> 
        <input type="text" name="image" accept = "image/*" placeholder="Type to add a picture url"/> 
        <input type="number" name="importance" min = "0" placeholder="Type to choose an importance position"/> 
        <textarea class = "text-area" name="message" rows = "10" cols = "5" placeholder="Type a message for yourself"></textarea> 
        <input type="submit" value="Submit" class="new-boosts first"> 
       </form> 
       {{/if}} 
      </header> 
      <ul> 
       {{#each boosts}} 
        {{> yield}} 
       {{/each}} 
      </ul> 
     </div> 
    </body> 
    </template> 

boost.html

<template name="boost"> 

     <article class="image-with-header"> 
      <img class="image" src="{{image}}" /> 
     </article> 

     <li class="{{#if private}}private{{/if}}"> 
      <button class="delete">&times;</button> 

      {{#if isOwner}} 
       <button class="toggle-private"> 
       {{#if private}} 
       Private 
       {{else}} 
        Public 
       {{/if}} 
       </button> 
      {{/if}} 

      <span class="text"><strong>{{username}}</strong> - <input type="text" value="{{text}}" name="caption"> - <input type="number" value="{{importance}}" name="importance"> </span> 
     </li> 
    </template> 

routes.js

Router.configure({ 
    layoutTemplate: 'ApplicationLayout' 
}); 

Router.route('/', function() { 
    this.layout('ApplicationLayout'); 
    this.render('boost'); 
}); 

回答

1

我相信问题是,你是在each循环内使用{{> yield}}。试试这个:

*。html的

<template name="ApplicationLayout"> 
    <body> 
     {{!-- ... --}} 
      <ul> 
      {{> yield}} 
      </ul> 
     {{!-- ... --}} 
    </body> 
</template> 

<template name="boosts"> 
    {{#each boosts}} 
     {{> boost}} 
    {{/each}} 
</template> 

<template name="boost"> 
    {{!-- ... --}} 
</template> 

* .js文件

// in tempalte file 
Template.boosts.helpers({ 
    boosts() { 
    // return boosts here 
    } 
}); 

// in router file 

Router.configure({ 
    layoutTemplate: 'ApplicationLayout' 
}); 

Router.route('/', function() { 
    this.render('boosts'); 
}); 
+0

这回答我的问题,程序现在工作。尽管如此,我还是有兴趣了解为什么收益率在每个循环中都不起作用。 @Khang你会想知道为什么Iron Router能够这样工作吗?还是只是任意的?谢谢! – Lesscomfortable