如何遍历对象的数组中的小胡子
问题描述:
我有对象的数组被设置为髭渲染功能:如何遍历对象的数组中的小胡子
require 'vendor/Mustache/Autoloader.php';
Mustache_Autoloader::register();
$m = new Mustache_Engine();
echo $m->render($template, $data);
$数据包含对象的数组,像这样:
(这是从http://www.jsoneditoronline.org的截图,我已经打开了你的信息的对象之一)
现在在我的$模板合作德我有这样的:
{{#.}}
<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));
所以现在“数据”变成了髭模板中的铅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));
似乎是显而易见的,我知道...
这可能不是你的问题。胡须也可以迭代顶层数据,例如:'$ m-> render('{{。}} x {{/。}}',[1,2,3])==“xxx “'。如果没有看到您用于原始问题的完整代码和数据,则很难说您的问题是什么。 – bobthecow