首页 > 解决方案 > 新手问题 - php & mustache 迭代

问题描述

我在迭代一组数据时遇到了困难。我的代码如下:

$root = $_SERVER['DOCUMENT_ROOT'];
require $root.'/assets/js/mustache.php-2.13.0/src/Mustache/Autoloader.php';
include($root."/utilities.php");
Mustache_Autoloader::register();
$url = "http://development.somesite.com/pages/lookup/sql/getData.php?dir=A&process=20&d=O";
$r = json_decode(Insert($url));

$template = "{{{#r}}}<b>{{PropName}}</b></br>{{{/r}}}";

$m = new Mustache_Engine;
$html = $m->render($template, $r); 
echo "html: ".$html."</br>";
var_dump($r);

我得到的结果是:

html:

array(317) { [0]=> object(stdClass)#2 (2) { ["PropNo"]=> string(3) "409" ["PropName"]=> string(21) "WRD Avenue LLC" } [1]=> object(stdClass)#3 (2) { ["PropNo"]=> string(3) "410" ["PropName"]=> string(21) "1111 Avenue AB" }...}

该数组是使用 json_decode 创建的。我确信答案很简单,但我显然在这里遗漏了一些东西。

谢谢你的帮助。

标签: phpmustachemustache.php

解决方案


我得到它的工作如下:

将 $template 更改为:

$template = "{{#r}}<b>{{PropName}}</b></br>{{/r}}";

并将渲染更改为:

$html = $m->render($template, array('r'=>$r)); 

推荐阅读