簡體   English   中英

使用部分在smarty tpl中進行變量

[英]make a variable in smarty tpl using section

我想使用部分在smarty tpl中創建一個變量。 我在類中使用for循環分配了變量。 我的代碼是:

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}是從該部分檢索的值。 您必須用value替換field name

最終我用另一種方法得到了它。 我將所有值放在一個變量中並分配它,並在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM