CodeIgniter Forums
Nested Loop with parser template - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Nested Loop with parser template (/showthread.php?tid=51685)



Nested Loop with parser template - El Forum - 05-14-2012

[eluser]giovannidc[/eluser]
Hey,

Was wondering if anyone had a solution to creating a nested loop with the template parser:

Code:
{users_details}
<input type="text" name="displayname" value="{user_displayname}"/>
<input type="text" name="email" value="{user_email}"/>

<select name="company">             {companies}
<option value="{company_id}">{company_name}</option>
{/companies}        
</select>

{/users_details}

The above code does not display the way I want it. Instead of getting:
Code:
<option value="1">Microsoft</option>
I'm getting
Code:
{companies}
<option value="{company_id}">{company_name}</option>
{/companies}

So my assumption is that nested parsing is not allowed?


Nested Loop with parser template - El Forum - 05-15-2012

[eluser]giovannidc[/eluser]
So any takers on this?


Nested Loop with parser template - El Forum - 05-15-2012

[eluser]craig.hoog[/eluser]
When looking at the CI User Guide I'm seeing an example of nested parsing:

Code:
{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
{/blog_entries}



Nested Loop with parser template - El Forum - 05-15-2012

[eluser]giovannidc[/eluser]
Yeah that works for looping, which I'm doing. The problem I'm getting is when you're adding a second loop inside a loop:
Code:
{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
{blog_comments}
<p>{comment}</p>
{/blog_comments}
{/blog_entries}



Nested Loop with parser template - El Forum - 05-15-2012

[eluser]Aken[/eluser]
The parser should be able to handle this no problem. I just did a quick test and it worked fine. Here's the code, compare it to yours and see if you can find where it might not be functioning properly. Also compare your CodeIgniter version to the latest and see if it has been upgraded to include this functionality since your version.

Code:
// application/views/template.php

{chunk}
<section class="chunk">

{divs}
<div>
  {var1}, {var2}
</div>
{/divs}

</section>
{/chunk}

// Controller

  $this->load->library('parser');
  
  $data = array(
   'chunk' => array(
    array(
     'divs' => array(
      array(
       'var1' => 'test',
       'var2' => 'hi',
      ),
      array(
       'var1' => 'test',
       'var2' => 'hi',
      ),
      array(
       'var1' => 'test',
       'var2' => 'hi',
      ),
     ),
    ),
    array(
     'divs' => array(
      array(
       'var1' => 'test',
       'var2' => 'hi',
      ),
      array(
       'var1' => 'test',
       'var2' => 'hi',
      ),
      array(
       'var1' => 'test',
       'var2' => 'hi',
      ),
     ),
    ),
   ),
  );
  
  $this->parser->parse('template', $data);



Nested Loop with parser template - El Forum - 05-23-2012

[eluser]giovannidc[/eluser]
Thanks.. I see I had an issue with tables. The parser doesnt work so nicely inside a <tr> or <td>


Nested Loop with parser template - El Forum - 05-23-2012

[eluser]Aken[/eluser]
Are you talking about the Table class, or just table HTML? The parser doesn't care what HTML is around the tags, so other HTML should not be the issue, unless there's broken braces somewhere or something.