Trent Scott
Trent Scott

Reputation: 2028

How to Convert this Smarty Code Back to PHP?

From my understanding, Smarty includes a number of built-in functions that have equivalencies in PHP.

How do I convert the code below back to native PHP? Is the "section" similar to a for loop?

<table width="400" border="0">
    {section name=x loop=$records}
    <tr>
        {section name=y loop=$records[x]}
        <td align="right">
            <input type="checkbox" name="{$records[x][y].prefkey}" {if $records[x][y].prefval eq "on"}checked{/if} />
        </td>
        <td align="left">
            <strong>&nbsp;{$records[x][y].prefkey}</strong>
        </td>
        {/section}
    </tr>
    {/section}
</table>

Upvotes: 0

Views: 861

Answers (2)

AJ.
AJ.

Reputation: 28194

Here is an example, using simple arrays for your data objects:

<?php                                                                                               
// generate some test data                                                                          
$records = array(                                                                                   
    array(                                                                                          
        array('prefkey'=>"foo",'prefval'=>"on"),                                                    
        array('prefkey'=>"bar",'prefval'=>"off"),                                                   
    ),                                                                                              
    array(                                                                                          
        array('prefkey'=>"foo",'prefval'=>"off"),                                                   
        array('prefkey'=>"bar",'prefval'=>"off"),                                                   
    ),                                                                                              
    array(                                                                                          
        array('prefkey'=>"foo",'prefval'=>"off"),                                                   
        array('prefkey'=>"bar",'prefval'=>"on"),                                                    
    ),                                                                                              
);                                                                                                  
?>                                                                                                  
<table width="400" border="0">                                                                      
<?php for($x=0; $x<count($records); $x++){ ?>                                                       
<tr>                                                                                                
    <?php for($y=0; $y<count($records[$x]); $y++){ ?>                                               
        <td align="right">
        <input type="checkbox" name="<?=$records[$x][$y]['prefkey']; ?>" <?=($re                    
cords[$x][$y]['prefval'] == "on"? "checked" : "") ?>/></td>                                         
        <td align="left">                                                                           
            <strong>&nbsp;<?=$records[$x][$y]['prefkey']; ?></strong>                               
        </td>                                                                                       
        <?php }?>                                                                                   
</tr>                                                                                               
<?php }?>                                                                                           
</table>

If the data is contained in actual objects, you'll need to change the accessor syntax.

Upvotes: 2

Damp
Damp

Reputation: 3348

this {section name=x loop=$records}{section}

is equivalent to foreach(array_keys($records) as $x) { }

Upvotes: 1

Related Questions