View 使用Jig数据映射器将数组无脂传递到视图
我有这样的View 使用Jig数据映射器将数组无脂传递到视图,view,render,fat-free-framework,View,Render,Fat Free Framework,我有这样的数据/faqs.json { "section-1" : { "sub-section-1" : { "557aef62e0629": { "question": "how are you?", "answer": "fine.. you?" }, "557af3d40b041": { "qu
数据/faqs.json
{
"section-1" : {
"sub-section-1" : {
"557aef62e0629": {
"question": "how are you?",
"answer": "fine.. you?"
},
"557af3d40b041": {
"question": "Question",
"answer": "Answer to question"
}
},
"sub-section-2": {
"557af3d80b041": {
"question": "another section question?",
"answer": "another section answer?"
}
}
},
"section-2" : {
"557af32d201f6": {
"question": "Question",
"answer": "Answer to question"
},
"557af33c7c60e": {
"question": "Question",
"answer": "Answer to question"
}
}
}
在我的控制器方法中:
$faqs = [];
$mapper = new \DB\Jig\Mapper($this->db, 'faqs.json');
$mapper->load();
while (!$mapper->dry()) {
$faqs[] = $mapper->cast();
$mapper->next();
}
$this->f3->set('faqdata', $faqs);
将FAQ数据
发送到视图
在我看来,我试过:
<repeat group="{{ @faqdata[1] }}" key="{{ @key }}" value="{{ @faqs }}">
<div>
<p><span><b>{{ @key }}</b></span></p>
<repeat group="{{ @faqs }}" key="{{ @k }}" value="{{ @v }}">
<dt>{{ @k }}</dt>
<dd>{{ @v }}</dd>
</repeat>
</div>
</repeat>
因此,section-2不是数组的数组吗?这是因为
@faqdata[1]
包含数组的索引:\u id=>“section-2”
。所以你不能只循环它的属性。您应该调用预期属性(问题
和答案
)
无论如何,由于您的目的是获取整个数据数组,因此直接调用将更简单。你不需要地图绘制工具。见:
$faqs=$this->db->read('faqs.json');
现在$FAQ['section-2']
包含了第二部分
更新:
为了显示这种数据,您需要一个递归视图。这可以通过使用
标记的和属性来实现。比照
假设您的视图名为faqs.html
,我们可以在您的案例中这样做:
-
{{@section}}
-
{{@faq.question}
=>
{{@faq.answer}
因此,给定这个示例,我如何迭代并获得我的['question']
和['answer']
索引?我已经更新了答案,以包含递归视图的示例。我已经更正了我的初始答案。关于section-2
是数组的数组,您是对的。问题是您没有想到会出现额外的\u id
字段。它会自动与映射器一起提供。因此,实际上,在您的示例中可以避免使用映射器。您可以从中受益,而不是避免使用\u id
。因此,在您的初始视图中,您可以使用{{@faqs.{u id}}}
而不是{{@key}
,您可以直接调用{{@faqs.question}
和{@faqs.answer}
,而不是通过{@faqs}
循环。
array(2) {
[0]=>
array(3) {
["sub-section-1"]=>
array(5) {
["557aef62e0629"]=>
array(2) {
["question"]=>
string(12) "how are you?"
["answer"]=>
string(11) "fine.. you?"
}
["557af0d114839"]=>
array(2) {
["question"]=>
string(35) "hi there, this is quesiton number 2"
["answer"]=>
string(19) "this is answer no 2"
}
["557af32d201f6"]=>
array(2) {
["question"]=>
string(8) "Question"
["answer"]=>
string(18) "Answer to question"
}
["557af33c7c60e"]=>
array(2) {
["question"]=>
string(8) "Question"
["answer"]=>
string(18) "Answer to question"
}
["557af3d40b041"]=>
array(2) {
["question"]=>
string(8) "Question"
["answer"]=>
string(18) "Answer to question"
}
}
["sub-section-2"]=>
array(1) {
["557af3d80b041"]=>
array(2) {
["question"]=>
string(25) "another section question?"
["answer"]=>
string(23) "another section answer?"
}
}
["_id"]=>
string(9) "section-1"
}
[1]=>
array(3) {
["557af32d201f6"]=>
array(2) {
["question"]=>
string(8) "Question"
["answer"]=>
string(18) "Answer to question"
}
["557af33c7c60e"]=>
array(2) {
["question"]=>
string(8) "Question"
["answer"]=>
string(18) "Answer to question"
}
["_id"]=>
string(9) "section-2"
}
}