Welcome Guest, Not a member yet? Register   Sign In
Parser - Conditions in Loop
#1

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 %}
Reply
#2

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));
    
Reply
#3

(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.
Reply
#4

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB