phpsyntaxprestashopsmarty

for looping PHP syntax to Smarty Syntax


I have a PPH syntax from prestashop file:

<select name="deadline" id="deadline" onclick="dead_line_all()">    
<option value="235959" selected="selected">'.$this->l('NONE').'</option>';  
for($hours=0; $hours<24; $hours++)      
for($mins=0; $mins<60; $mins+=30)   
$this->_html .= '
<option value="'.str_pad($hours,2,'0',STR_PAD_LEFT).''.str_pad($mins,2,'0',STR_PAD_LEFT).'00">               '.str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' 
</option>'; 
$this->_html .= ' </select>

How to change it into smarty syntax in the smarty template (xxx.tpl)?


Solution

  • Their are two ways to do this. First simple assign this select html to smarty variable in php file and print in xxx.tpl php file

    <?php
    $html .= '<select name="deadline" id="deadline" 
          onclick="dead_line_all()">';'
          $html .= '<option value="235959" selected="selected">--Select--</option>';
         for($hours=0; $hours<24; $hours++){
            for($mins=0; $mins<60; $mins+=30){
           $html .= '<option value="'.str_pad($hours,2,'0',STR_PAD_LEFT).''.str_pad($mins,2,'0',STR_PAD_LEFT).'00">'.str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
            }
         }
    $html .= '</select>';
    
    $smarty->assign('select',$html);
    ?>
    

    xxx.tpl template file

     
    
    
        {$select}
    
    
    

    this will print select html in template file.

    2.Second option in code within template xxx.tpl file

    <select name="deadline" id="deadline" onclick="dead_line_all()">
       <option value="235959" selected="selected">Select</option>
      {for $hour=0 to 24}
       <option value="{$hour|str_pad:2:0:STR_PAD_LEFT}:30">{$hour|str_pad:2:0:STR_PAD_LEFT}:30</option>
       <option value="{$hour|str_pad:2:0:STR_PAD_LEFT}:60">{$hour|str_pad:2:0:STR_PAD_LEFT}:60</option>
      {/for}
     </select>
    

    also you need to create new smarty modifier plugin for str_pad function.

    <?php
    
    function smarty_modifier_str_pad($string,$length,$pad_string,$pad_type) {
        return str_pad($string,$length,$pad_string,$pad_type);
    }
    
    $smarty->registerPlugin(Smarty\Smarty::PLUGIN_MODIFIER, 'str_pad', 'smarty_modifier_str_pad');
    
    ?>
    

    for more info on adding modifiers https://smarty-php.github.io/smarty/stable/api/extending/modifiers/

    second example is for latest smarty library