How to assign an array within a smarty template file?

前端 未结 5 663
萌比男神i
萌比男神i 2020-12-09 07:53

I was wondering if it was possible to assign an array to a variable within a Smarty template file? I have tried this

{assign var=\'file\' value = array(\'dir         


        
相关标签:
5条回答
  • 2020-12-09 07:59
    {php}
      $this->assign("array", array('dir','doc','exe'));
    {/php}
    
    {foreach from=$array item=item}
      {$item}
    {/foreach}
    

    From Smarty v.3 new syntax is available

    {$array = ['item1','item2',$item3]}
    

    see for more details : http://www.smarty.net/docs/en/language.syntax.variables.tpl

    0 讨论(0)
  • 2020-12-09 08:12

    I just found another answer here that allows you to do this without the use of {php} tags (recommended by Smarty)

    {assign var='icon' value=','|explode:"dir,doc,exe"}
    

    still open to more ideas though...

    0 讨论(0)
  • 2020-12-09 08:12

    what about {$system=['freebsd','windows','macosx','linux']}?

    0 讨论(0)
  • 2020-12-09 08:19
    $smarty->assign("lat",$lat);
    
    {foreach $lat as $latlongval}
        {assign var="myArray" value=","|explode:$latlongval} 
        {$myArray['0']}
        {$myArray['1']}
    {/foreach}
    
    0 讨论(0)
  • 2020-12-09 08:20

    its not right way to write a code with in smarty template file. you should create a array in php and then get the values from smarty.

    This is the right way to create a standard development code. like.
    

    PHP:

    public function arrSam(){
        $colors = array( 0 => '#1f1f1f', 1 => '#696969', 2 => '#878787', 3 => '#b4b4b4', 4 => '#d2d2d2', 5 => '#f0f0f0', 6 => '#ffffff');
        $smarty->assign('colors', $colors);
    }
    

    Smarty:

    {assign var=colors value=$smarty->arrSam()}
    {$colors|print_r}
    
    0 讨论(0)
提交回复
热议问题