CodeIgniter Forums
Is it possible to use nested loops in parser templates? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Is it possible to use nested loops in parser templates? (/showthread.php?tid=72974)



Is it possible to use nested loops in parser templates? - HardyW - 03-06-2019

I am not sure if nested loops are supported by the parser, at least I am not able to display the following template correctly:
Code:
<table class="data_table">
  <thead>
    <tr>
      {header_entries}
        <th>{header_title}</th>
      {/header_entries}
    </tr>
  </thead>
  <tbody>
    {data_entries}
      <tr>
        {data_row}
          <td>{data_cell}</td>
        {/data_row}
      </tr>
    {/data_entries}
  </tbody>
</table>

The headers works but not the table's body when using these data:


Code:
   $header_entries = [['header_title' => 'one'],
                      ['header_title' => 'two']];
   $data_entries = [['data_row' => [['data_cell' => 'one - one'],
                                    ['data_cell' => 'one - two']],
                     'data_row' => [['data_cell' => 'two - one'],
                                    ['data_cell' => 'two - two']]]];
   $parser = \Config\Services::parser();
   echo $parser->setData(['header_entries' => $header_entries,
                          'data_entries' => $data_entries])->render('templates/data_table_template');



RE: Is it possible to use nested loops in parser templates? - ciadmin - 03-06-2019

If you want nested variables to also see the level above them, then you need to pass true as a second parameter to the render() call. See https://codeigniter4.github.io/CodeIgniter4/outgoing/view_renderer.html#view-renderer-options


RE: Is it possible to use nested loops in parser templates? - HardyW - 03-07-2019

(03-06-2019, 05:37 PM)ciadmin Wrote: If you want nested variables to also see the level above them, then you need to pass true as a second parameter to the render() call. See https://codeigniter4.github.io/CodeIgniter4/outgoing/view_renderer.html#view-renderer-options

No, this is not I would like to do. I just would like to have two nested loops. But this does not seem to work because the output is:

Quote:-->header: one two
-->body: two - one two - two
The header loop is correct but the second (inner) loop is not. Only the last result of the loop is used.


RE: Is it possible to use nested loops in parser templates? - ciadmin - 03-14-2019

The view parser supports "cascading data", but not "nested loops" the way you are looking for.
I suggest adding a feature request for this, if needed for your app... https://forum.codeigniter.com/forum-29.html