使用部分在smarty tpl中创建一个变量

make a variable in smarty tpl using section

我想在 smarty tpl using section 中创建一个变量。我用 for 循环在 class 中分配了变量。我的代码是:

for($i=1;$i<=$_REQUEST['parcel_quantity'];$i++)                     
{                       
    ${"grith_".$i}=$_REQUEST['parcel_grith_'.$i];
    ${"width_".$i}=$_REQUEST['parcel_width_'.$i];
    ${"height_".$i}=$_REQUEST['parcel_height_'.$i];
    ${"weight_".$i}=$_REQUEST['parcel_weight_'.$i];
    ${"volumetric_weight".$i}=(${"grith_".$i}*${"width".$i}*${"height_".$i})/4000;
    $objSmarty->assign("volumetric_weight".$i,${"volumetric_weight".$i}); 
    $objSmarty->assign("select_l".$i,${"grith_".$i});   
    $objSmarty->assign("select_w".$i,${"width_".$i});                   
    $objSmarty->assign("select_h".$i,${"height_".$i});                  
    $objSmarty->assign("select_weight".$i,${"weight_".$i}); 
}

现在我想在 tpl 中使用这些赋值:

{section name=data start=0 loop=$parcel_quantity}`
    <li><a ><strong>Dimentions :-</strong>&nbsp;{$select_h}{$smarty.section.data.index+1}{$unit}&nbsp;X&nbsp;{$select_w}{$smarty.section.data.index+1}{$unit}&nbsp;X&nbsp;{$select_l}{$unit}</a></li>
    <li><a ><strong>Volumetric Weight :-</strong>&nbsp;{$volumetric_weight}{$smarty.section.data.index+1}Kg</a></li>
{/section}

像这样尝试它会起作用:

tpl :

{section name=data start=0 loop=$parcel_quantity}
   {$parcel_quantity[data].value}
{/section}

此处,{$parcel_quantity[data].value} 是从该部分检索到的值。您必须将 field name 替换为 value

最后我用另一种方法得到了它。我将所有值放在一个变量中并分配它并用作 tpl 中的 smarty 变量。

 for($i=1;$i<=$_REQUEST['parcel_quantity'];$i++)
                            {
                            ${"grith_".$i}=$_REQUEST['parcel_girth_'.$i];   
                            ${"width_".$i}=$_REQUEST['parcel_width_'.$i];   
                            ${"height_".$i}=$_REQUEST['parcel_height_'.$i]; 
                            ${"weight_".$i}=$_REQUEST['parcel_weight_'.$i]; 
                            ${"volumetric_weight".$i}= ($_REQUEST['parcel_girth_'.$i]*$_REQUEST['parcel_width_'.$i]*$_REQUEST['parcel_height_'.$i])/4000 ;

                            if(${"weight_".$i}>${"volumetric_weight".$i})
                        ${"weight_main_".$i}=${"weight_".$i};
                        else
                        ${"weight_main_".$i}=${"volumetric_weight".$i};

                        $total_weight+= ${"weight_main_".$i};



                            // Put all values in variable with design 
                            $veiw_in_right.='
                             <li><strong>Parcel Detail:'.$i.'</strong></li><li><a ><strong>Dimentions :-</strong>&nbsp;'.${"height_".$i}.$unit.'&nbsp;X&nbsp;'.${"width_".$i}.$unit.'&nbsp;X&nbsp;'.${"grith_".$i}.$unit.'</a></li>
              <li><a ><strong>Volumetric Weight :-</strong>&nbsp;'.${"volumetric_weight".$i}.'Kg</a></li>
              <li><a ><strong>Weight:-</strong>&nbsp;'.${"weight_".$i}.'Kg</a><hr></li>';   

         $objSmarty->assign("veiw_in_right",$veiw_in_right);

                        }

在 Tpl 中只需输入变量:

<li><a ><strong>No of Pieces :-</strong>&nbsp;{$parcel_quantity}</a><hr /></li>
        {$veiw_in_right}
     <li><a ><strong>Total Weight :-</strong>&nbsp;{$total_weight}Kg</a></li>