Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Templating (if / else if) in loop

I have a loop of records which I am parsing through to a CI template and I need to run an if on a value in that loop but all I get is the string being output in the html can someone tell me what I am doing wrong.

Code:

{bond}
    <tr>
        <td>{bond_issuer}</td>
        <td>{bond_bond}</td>
        <td>
            {if bond_payment_frequency == 'A' }
            Annual
            {if:elseif bond_payment_frequency == 'SA'}
            Semi Annual
            {if:else}
            Quarterly
            {/if}
        </td>
        <td>{bond_coupon_pa}</td>
        <td>{bond_maturity_date}</td>
        <td>{bond_indicative_yield}</td>
        <td>{bond_asx_code}</td>
    </tr>
{/bond}

Output:

{if bond_payment_frequency == 'A' } Annual {if:elseif bond_payment_frequency == 'SA'} Semi Annual {if:else} Quarterly {/if}

Solution:

<?php foreach($bond as $b) {?>
    <tr>
        <td><?=$b->bond_issuer;?></td>
        <td><?=$b->bond_bond;?></td>
        <td>
            <?php
                if($b->bond_payment_frequency === 'A') {
                    echo 'Annual';
                } elseif($b->bond_payment_frequency === 'SA') {
                    echo 'Semi Annual';
                } else {
                    echo 'Quarterly';
                };
            ?>
        </td>
        <td><?=$b->bond_coupon_pa;?></td>
        <td><?=$b->bond_maturity_date;?></td>
        <td><?=$b->bond_indicative_yield;?></td>
        <td><?=$b->bond_asx_code;?></td>
    </tr>
<?php } ?>
like image 828
Justin Erswell Avatar asked May 28 '26 22:05

Justin Erswell


1 Answers

Regarding the documentation, I don't think you can build conditions with the standard CI's Template Parser Class.

You can loop through arrays like you did with {bond} [...] {/bond}, but I don't think you can write {if} conditions.

Like the doc says :

The Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple variables or variable tag pairs.

[...]

Also Note: The Template Parser Class is not a full-blown template parsing solution. We've kept it very lean on purpose in order to maintain maximum performance.

like image 75
B F Avatar answered May 30 '26 10:05

B F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!