CodeIgniter Forums
Parser - Conditions in Loop - 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: Parser - Conditions in Loop (/showthread.php?tid=88388)



Parser - Conditions in Loop - ltarrant - 09-01-2023

Hi,

Is it possible to use the basic allowed conditions in the parser within loops?
If doesn't seem to be working for me.

Code:
{% if !empty($products) %}
works fine outside the loop.

Code:
{% if !empty($price_sellprice) %}
Inside the {products} loops does not work.

Code:
$parser->setConditionalDelimiters('{%', '%}');
I have conditional delimited set at {% %}


Code:
{% if !empty($products) %}
{products}
<div class="cat-listing">
<div class="block-cat-pic"><a href="{!page_url!}"><img
src="{!page_image|image_size(sm)!}"
alt="{page_image_alt}" /></a></div>
<div class="block-cat-info">
<p class="block-cat-header"><a href="{!page_url!}">{!page_heading!}</a></p>
<p>{!page_short_description!}</p>
</div>
<div class="cat-price">
{% if !empty($price_sellprice) %}
<div class='pricesimple-outer'>
<div class='pricesimple-from'>From </div>
<div class='pricesimple-rrp'><strike>{!price_retailprice!}</strike></div>
<div class='pricesimple-price'>{!price_sellprice!}</div>
</div>
{% else %}
<p><strong>Call Us</strong></p>
{% endif %}
</div>
</div>
{/products}
</div>
{% endif %}



RE: Parser - Conditions in Loop - kenjis - 09-03-2023

The following test passes.


PHP Code:
    public function testParseLoop(): void
    
{
        $data = [
            'title'  => 'Super Heroes',
            'powers' => [
                ['name' => 'Tom'],
                ['name' => 'Dick'],
                ['name' => 'Henry'],
            ],
        ];

        $template "{title}\n{powers}{if true}{name}{endif} {/powers}";

        $this->parser->setData($data);
        $this->assertSame("Super Heroes\nTom Dick Henry "$this->parser->renderString($template));
    



RE: Parser - Conditions in Loop - ltarrant - 09-04-2023

(09-04-2023, 08:31 AM)ltarrant Wrote: I may not have explained the issue very well.
I am unable to use conditional logic against pseudo-variables associative array values inside variable pair loops.

The simple example below outputs no names as the variable $name doesn't appear to be available to evaluate so empty is true.

Template: 
Code:
<html>
<head>
    <title>{title}</title>
</head>
<body>
    <h3>{title}</h3>
    {if !empty($powers)}
    {powers}
        {if !empty($name)}
        <p>{name}</p>
        {endif}
    {/powers}
    {endif}
</body>
</html>

Controller:

Code:
public function parserTest() {
    $data = [
        'title'  => 'Super Heroes',
        'powers' => [
            ['name' => 'Tom'],
            ['name' => 'Dick'],
            ['name' => ''],
        ],
    ];
   
    $parser = \Config\Services::parser();
    $parser->setData($data);
    return $parser->render('template');
}

Looking at the CodeIgniter\View\Parser class parse function calls parseConditionals and handles all conditions prior to parsePair. Ideally this would be tweaked to ignore conditions inside variable pairs and parse those conditions from within parsePair when associative array values would be available.



RE: Parser - Conditions in Loop - ltarrant - 09-29-2023

To resolve my issues I ended up using Kenjis CI4 Twig Library and using Twig as a Template engine rather than the built in Parser.

https://github.com/kenjis/codeigniter-ss-twig