2017-01-03 44 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