FatFree 使用 Jig 数据映射器将数组传递给视图
FatFree pass array to a view using Jig data mapper
我有data/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);
发送 faqdata
到视图。
在我看来我尝试过:
<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
个常见问题解答,但我收到错误:
Invalid argument supplied for foreach()
为什么 @faqs
被视为 foreach() 的无效参数?
编辑:
这是 var_dump
显示的内容:
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"
}
}
那么 section-2
数组不就是数组吗?
那是因为@faqdata[1]
包含了数组的索引:_id => 'section-2'
。所以你不能只遍历它的属性。您应该改为调用预期的属性(question
和 answer
)。
无论如何,由于您的目的是获取整个数据数组,因此直接调用 Jig::read 会更简单。你不需要映射器。参见:
$faqs = $this->db->read('faqs.json');
现在 $faqs['section-2']
包含第二部分。
更新:
为了显示那种数据,您需要一个递归视图。这可以通过使用 <include>
标签的 with
属性来实现。比照。 docs.
假设您的视图被命名为 faqs.html
:
,我们可以按照您的情况执行此操作
<ul>
<repeat group="@faqs" key="@section" value="@data">
<li>
<span>{{ @section }}</span>
<check if="($faq=reset(@data)) && isset(@faq.question)">
<true>
<ul>
<repeat group="@data" value="@faq">
<li>
{{ @faq.question }}
=>
{{ @faq.answer }}
</li>
</repeat>
</ul>
</true>
<false>
<include href="faqs.html" with="faqs={{ @data }}"/>
</false>
</check>
</li>
</repeat>
</ul>
我有data/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);
发送 faqdata
到视图。
在我看来我尝试过:
<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
个常见问题解答,但我收到错误:
Invalid argument supplied for foreach()
为什么 @faqs
被视为 foreach() 的无效参数?
编辑:
这是 var_dump
显示的内容:
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"
}
}
那么 section-2
数组不就是数组吗?
那是因为@faqdata[1]
包含了数组的索引:_id => 'section-2'
。所以你不能只遍历它的属性。您应该改为调用预期的属性(question
和 answer
)。
无论如何,由于您的目的是获取整个数据数组,因此直接调用 Jig::read 会更简单。你不需要映射器。参见:
$faqs = $this->db->read('faqs.json');
现在 $faqs['section-2']
包含第二部分。
更新:
为了显示那种数据,您需要一个递归视图。这可以通过使用 <include>
标签的 with
属性来实现。比照。 docs.
假设您的视图被命名为 faqs.html
:
<ul>
<repeat group="@faqs" key="@section" value="@data">
<li>
<span>{{ @section }}</span>
<check if="($faq=reset(@data)) && isset(@faq.question)">
<true>
<ul>
<repeat group="@data" value="@faq">
<li>
{{ @faq.question }}
=>
{{ @faq.answer }}
</li>
</repeat>
</ul>
</true>
<false>
<include href="faqs.html" with="faqs={{ @data }}"/>
</false>
</check>
</li>
</repeat>
</ul>