render - FatFree pass array to a view using Jig data mapper -
i have data/faqs.json this.
{ "section-1" : { "sub-section-1" : { "557aef62e0629": { "question": "how you?", "answer": "fine.. you?" }, "557af3d40b041": { "question": "question", "answer": "answer question" } }, "sub-section-2": { "557af3d80b041": { "question": "another section question?", "answer": "another section answer?" } } }, "section-2" : { "557af32d201f6": { "question": "question", "answer": "answer question" }, "557af33c7c60e": { "question": "question", "answer": "answer question" } } } and in controller method:
$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); to send faqdata view.
in view tried:
<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> to read section-2 of faqs, error:
invalid argument supplied foreach()
why @faqs considered invalid argument foreach()?
edit:
this var_dump shows:
array(2) { [0]=> array(3) { ["sub-section-1"]=> array(5) { ["557aef62e0629"]=> array(2) { ["question"]=> string(12) "how you?" ["answer"]=> string(11) "fine.. you?" } ["557af0d114839"]=> array(2) { ["question"]=> string(35) "hi there, quesiton number 2" ["answer"]=> string(19) "this answer no 2" } ["557af32d201f6"]=> array(2) { ["question"]=> string(8) "question" ["answer"]=> string(18) "answer question" } ["557af33c7c60e"]=> array(2) { ["question"]=> string(8) "question" ["answer"]=> string(18) "answer question" } ["557af3d40b041"]=> array(2) { ["question"]=> string(8) "question" ["answer"]=> string(18) "answer 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 question" } ["557af33c7c60e"]=> array(2) { ["question"]=> string(8) "question" ["answer"]=> string(18) "answer question" } ["_id"]=> string(9) "section-2" } } so isn't section-2 array of arrays?
that's because @faqdata[1] contains index of array: _id => 'section-2'. can't loop through properties. should call expected properties instead (question , answer).
anyway since intent whole data array, direct call jig::read simpler. don't need mapper that. see:
$faqs = $this->db->read('faqs.json'); now $faqs['section-2'] contains 2nd section.
update:
in order display kind of data, need recursive view. can achieved, using with attribute of <include> tag. cf. docs.
here's how in case, assuming view named 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>
Comments
Post a Comment