所以这是我的代码......我在我的项目中使用过。
$app->post(
'/chk_db',
function () use ($app){
require_once 'lib/mysql.php';
$dx = connect_db('MyPhotos');
//XML RESPONSE
$app->response->setStatus(0);
$res = $app->response();
$res['Content-Type'] = 'application/xml';
$view = $app->view();
$view->setTemplatesDirectory('./');
$oArray = array("Status"=> $dx.status, "code" => $dx.code);
return $app->render('chkdb.xml', $oArray);
}
);
所以我把这个数组作为xml模板的输入 (顺便说一句......这是json_encoded ......我只是用它来表示数组......谢谢......)
[{"ObjID":"1","ParenetID":"10001","Path":"http:\/\/localhost\/img\/1.jpg","Title":"1st Image","ChildCount":"0","Owner":"jhim","Comment":"hehe","inode":"0"},
{"ObjID":"2","ParenetID":"10002","Path":"http:\/\/localhost\/img\/2.jpg","Title":"2nd Image","ChildCount":"0","Owner":"nemy","Comment":"test lang","inode":"0"},
{"ObjID":"3","ParenetID":"10003","Path":"http:\/\/localhost\/img\/3.jpg","Title":"3rd Image","ChildCount":"0","Owner":"jayjay","Comment":"para amy output","inode":"0"},
{"ObjID":"4","ParenetID":"10004","Path":"http:\/\/localhost\/img\/4.jpg","Title":"4th Image","ChildCount":"0","Owner":"jhim","Comment":"yeah boy","inode":"0"}]
如何在模板上处理它们? chk_db.xml
{% for x in ????%}
<MyPhotos>
<ObjID>{{x.ObjID}}</ObjID>
...
<inode>{{x.inode}}</inode>
</MyPhotos>
{% else %}
<data>No data Found</data>
{% endfor %}
...谢谢
答案 0 :(得分:0)
你应该给这个数组命名。只需用这种方式重写$app->render
:
return $app->render('chkdb.xml', ['photos' => $oArray]);
并写入模板:
{% for x in photos %}
答案 1 :(得分:0)
哦,伙计......我的坏......我找到了答案...我的模板上没有任何问题......我的错误是没有命名我发送的数据..这是更正后的代码..
$app->post(
'/get_gallery_allphotos',
function () use ($app) {
require_once 'lib/mysql.php';
$re = getAll_photos('MyPhotos');
$app->response->setStatus(200);
$app->response()->headers->set('Content-Type', 'text/xml');
return $app->render('myphotos_allphotos_admin.xml', array("ArrayName" => $re));
}
);
我忘了给我发送的数组命名...所以在这里我把 ArrayName 作为$ re ...的名字......
所以在模板中我使用..
{% for x in ArrayName %}
<MyPhotos>
<ObjID>{{x.ObjID}}</ObjID>
<ParenetID>{{x.ParenetID}}</ParenetID>
<Path>{{x.Path}}</Path>
<Title>{{x.Title}}</Title>
<ChildCount>{{x.ChildCount}}</ChildCount>
<Owner>{{x.Owner}}</Owner>
<Comment>{{x.Comment}}</Comment>
<inode>{{x.inode}}</inode>
</MyPhotos>
{% else %}
Not Found
{% endfor %}
希望它能帮助别人^^,