分配给Smarty中的变量

时间:2015-01-21 15:11:11

标签: php smarty template-engine

我想将qty_hidden输入的value属性的结果保存到变量$selectedQty中,然后在下拉框中使用它来确定应该选择哪个选项。

<input type="hidden" name="qty_hidden" value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}">

<select name="qty">
<option value="1"{if $selectedQty==1}{" selected='selected'"}{/if}>1</option>
<option value="2"{if $selectedQty==2}{" selected='selected'"}{/if}>2</option>
<option value="3"{if $selectedQty==3}{" selected='selected'"}{/if}>3</option>
</select>

在阅读Smarty文档后,我仍然不了解如何将其放入变量中。

1 个答案:

答案 0 :(得分:1)

只需在之前分配它并稍后使用它:

{$selectedQty = ""} <?php // default ?>
{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}
   {$selectedQty = $customizedDatas.$productId.$productAttributeId|@count}
{else}
   {$selectedQty = ($product.cart_quantity-$quantityDisplayed)}
{/if}
<input type="hidden" name="qty_hidden" value="{$selectedQty}">

<select name="qty">
<option value="1"{if $selectedQty==1}{" selected='selected'"}{/if}>1</option>
<option value="2"{if $selectedQty==2}{" selected='selected'"}{/if}>2</option>
<option value="3"{if $selectedQty==3}{" selected='selected'"}{/if}>3</option>
</select>

详细了解assigning variables in the docs