如何处理 XML 模板上的多维数组?
How to handle Multidimensional Array on XML Template?
这是我的代码...我在项目中使用的代码。
$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 %}
谢谢...
你应该git给这个数组起个名字。只需这样重写 $app->render
部分:
return $app->render('chkdb.xml', ['photos' => $oArray]);
并在模板中写入:
{% for x in photos %}
哦天哪...我的错..我找到了这个问题的答案...我的模板没有任何问题..我没有命名我发送的数组是我的错..这是更正后的代码..
$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 %}
希望对其他人有所帮助^^,
这是我的代码...我在项目中使用的代码。
$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 %}
谢谢...
你应该git给这个数组起个名字。只需这样重写 $app->render
部分:
return $app->render('chkdb.xml', ['photos' => $oArray]);
并在模板中写入:
{% for x in photos %}
哦天哪...我的错..我找到了这个问题的答案...我的模板没有任何问题..我没有命名我发送的数组是我的错..这是更正后的代码..
$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 %}
希望对其他人有所帮助^^,