2016-03-14 87 views
0

我有對象的數組被設置爲髭渲染功能:如何遍歷對象的數組中的小鬍子

require 'vendor/Mustache/Autoloader.php'; 
    Mustache_Autoloader::register(); 
    $m = new Mustache_Engine(); 

echo $m->render($template, $data); 

$數據包含對象的數組,像這樣:

enter image description here

(這是從http://www.jsoneditoronline.org的截圖,我已經打開了你的信息的對象之一)

現在在我的$模板合作德我有這樣的:

  {{#.}} 
       <article> 

        <h1> 
         <a href="layouts_post.html">{{name}}</a> 
        </h1> 

        <p>{{{text}}}</p> 

        <h2>{{jobtitle}}</h2> 

       </article> 
     {{/.}} 

迭代不工作,我想知道如果有一個鬍子通過對象的數組迭代如上的方式。

(僅供參考 - 我試着重建我的數據,但沒有成功,每次鬍子是不能夠重複。)

的幫助非常感謝提前。

回答

1

好我的工作了這一點...

爲了鬍子有一個包含變量,需要將其指定爲在渲染函數中的參數。所以得到這個工作,我分配到$ data數組到「數據」鍵:

require 'vendor/Mustache/Autoloader.php'; 
Mustache_Autoloader::register(); 
$m = new Mustache_Engine(); 

echo $m->render($template, array('data' => $data)); 

所以現在「數據」變成了髭模板中的鉛VAR:

 {{#data}} 
       <article> 

        <h1> 
         <a href="layouts_post.html">{{name}}</a> 
        </h1> 

        <p>{{{text}}}</p> 

        <h2>{{jobtitle}}</h2> 

       </article> 
     {{/data}} 

事實上,原來,你可以在這個函數分配陣列的整個主機,並讓他們在模板佈局可供選擇:

require 'vendor/Mustache/Autoloader.php'; 
Mustache_Autoloader::register(); 
$m = new Mustache_Engine(); 

echo $m->render($template, array('data' => $data, 'anotherValue' => $someOtherData)); 

似乎是顯而易見的,我知道...

+0

這可能不是你的問題。鬍鬚也可以迭代頂層數據,例如:'$ m-> render('{{。}} x {{/。}}',[1,2,3])==「xxx 「'。如果沒有看到您用於原始問題的完整代碼和數據,則很難說您的問題是什麼。 – bobthecow