Welcome Guest, Not a member yet? Register   Sign In
Nested Loop with parser template
#1

[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?
#2

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

[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}
#4

[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}
#5

[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);
#6

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB