我在迭代一个数据数组时遇到了困难。 我的代码如下。
$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创建的。 我相信答案很简单,但我显然遗漏了什么。
谢谢你的帮助。
我让它工作如下。
将$template改为:
$template = "{{#r}}<b>{{PropName}}</b></br>{{/r}}";
并把render改为:
$html = $m->render($template, array('r'=>$r));