在Smarty中至少运行一次循环


run loop atleast one time in Smarty

如何在Smarty中至少运行一次循环?

如果我使用for,而to0,我的循环将永远不会运行!

我有一个表单,我想至少显示一次,但如果fieldCounter的值大于1,则循环应该运行到fieldCounter的值。

{for $start=1 to $fieldCounter}
<input type="text" name="price{$start}" value="" />
{/for}

Smarty中有do {} while();吗?

您可以使用max():

{assign var=_to value=max($fieldCounter,1)}
{for $start=1 to $_to}
<input type="text" name="price{$start}" value="" />
{/for}

现在$_to将始终至少是1

您也可以尝试smartys{forelse}。

{for $start=1 to $fieldCounter}
  <input type="text" name="price{$start}" value="" />
{forelse}
  <input type="text" name="price1" value="" />
{/for}

http://www.smarty.net/docs/en/language.function.for.tpl