在 Smarty 中赋值给一个变量

Assigning to a variable in Smarty

我想将 qty_hidden 输入的值属性的结果保存到变量 $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的文档,还是不明白怎么把它放到一个变量中。

只需预先赋值,稍后使用即可:

{$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