Silverstripe 3 template loops functions and nested loops -
good morning,
i've been struggling silverstripe, , it's template parsing regards nested loops , functions within loops while now, it's becoming maddening.
if can alternate solution besides ajax (it must pure php/html/ss), acceptable, thanks.
[situation]
have 2 data objects: objecta [has_many objectb], , objectb [has_one objecta].
i've implemented tab-pane using css-bootstrap, , display these 2 objects linked in respective tabs.
example:
objecta_car mapped [objectb_process1, objectb_process2]
objecta_plane mapped [objectb_process3, objectb_process4]
scenario 1:
please see sample pseudo code below:
///>sample.ss snippet <% loop objecta_datalist %> <div class="tab-pane"> <h3>$objecta_datalist.title</h3> <!-- prints objecta title --> <hr /> <h4>$objecta_datalist.description</h4> <!-- prints objecta description --> debug: $pos <!-- prints objecta loop count/index --> <!-- problem starts here --> <% loop objectb_datalist %> debug: objecta count: $top.pos <!-- prints 1, regardless --> debug: objecta count2: $up.pos <!-- same issue above --> debug: $pos <!-- due scope, prints objectb loop count/index --> <% end_loop %> </div> <!--//#tab-pane--> <% end_loop %>
my aim in above example was trying match objecta $pos inside objectb loop id belongs objectb. not working, because can't objecta current loop $pos while inside objectb.
scenario 2:
tried using function next.
///>sample extension code public function testfn(){ return "<pre>testfn called!</pre>"; }//testfn ///>sample.ss $testfn <!-- works outside of loop --> <% loop objecta_datalist %> $testfn <!-- not work! --> <% end_loop %>
scenario 3
got fed trying above scenarios work, , tried using controller.
so, i've decided pass arraylist template iterate on 1 loop.
but, can't seem values out. see below:
///>sample_controller.php public function sample(ss_httprequest $request){ ///... snippet ... $buildarr = array(); foreach($objecta $obja){ $buildarr[$obja->id]['obja'] = $obja; //store objecta $objb = objectb::get()->filter(array('status'=>'enabled','objecta_id'=>$obja->id)); $buildarr[$obja->id]['objb'] = $objb; //store objectb }//foreach loop $sample_data = new arraylist($buildarr); //if var_dump here, looks great. //so nothing seems problem $sample_data array $data = array('mysample' => $sample_data); return $this->customise($data)->renderwith(array('sample', 'page')); }//sample ///>sample.ss <% loop mysample %> <!-- don't know how data out --> <!-- i've tried $title, $mysample.title, $mysample.objecta.title --> <!-- nothing seems work. --> <% end_loop %>
any assistance provided appreciated, thanks.
good morning!
i can coming solution using scenarios 2 , 3. first 1 wouldn't know how address scope of first loop correctly.
scenario 2
this 1 quite simple. need refer general scope top:
///>sample extension code public function testfn(){ return "<pre>testfn called!</pre>"; }//testfn ///>sample.ss $testfn <!-- works outside of loop --> <% loop objecta_datalist %> $top.testfn <!-- should work --> <% end_loop %>
scenario 3
i adjusted third scenario job done here:
///>sample_controller.php public function sample(ss_httprequest $request){ // objecta $objecta = objecta::get(); // run through objecta , add // information objectb list of both $data = new arraylist(); foreach($objecta $obja){ $objb = objectb::get()->filter(array( 'status' => 'enabled', 'objecta_id' => $obja->id )); $data->add(array( 'obja' => $obja, 'objb' => $objb )); }//foreach loop return $this->customise(array('mysample' => $data)) ->renderwith(array('sample', 'page')); }//sample ///>sample.ss <% loop $mysample %> <!-- single dataobject --> <p>obja: $obja.id</p> <!-- datalist --> <% loop $objb %> <p>objb: $id</p> <% end_loop %> <% end_loop %>
this approach should work single dataobject datalist (similar arraylist). depending on data add arraylist within controler need adjust ss-file.
let me know if got further questions.
disclaimer: untested , without warranty, should still work
Comments
Post a Comment