php 如何在不显示的情况下增加 smarty 中分配的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8674831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:14:40  来源:igfitidea点击:

How do you increment an assigned variable in smarty without displaying it

phpsmarty

提问by Francis Lewis

So I have an assigned variable in smarty:

所以我在 smarty 中分配了一个变量:

{assign var=number value=0}

Now I can increment it using

现在我可以使用增加它

{$number++}

or

或者

{++$number}

Which is exactly what I need, only problem is, it displays the value of $number on the page. Is there a way I can increment the value but not display it?

这正是我需要的,唯一的问题是,它在页面上显示 $number 的值。有没有办法可以增加值但不显示它?

This is not used inside of a loop otherwise I would use something like iteration or index.

这不在循环内使用,否则我会使用迭代或索引之类的东西。

回答by Cyclonecode

You could do this:

你可以这样做:

{assign var=val value=1}
{assign var=val value=$val+1}
{$val} // displays 2

The above will be compiled to:

以上将被编译为:

$this->assign('val', 1);
$this->assign('val', $this->_tpl_vars['val']+1);
echo $this->_tpl_vars['val'];

or

或者

{assign var=var value=1}
{capture assign=var}{$var+1}{/capture}
{$var} // displays 2

Which in turn will be compiled as:

依次编译为:

$this->assign('var', 1);
ob_start();
echo $this->_tpl_vars['var']+1;
$this->_smarty_vars['capture']['default'] = ob_get_contents();
$this->assign('var', ob_get_contents());
ob_end_clean();
echo $this->_tpl_vars['var'];

another approach would be to write a small plugin:

另一种方法是编写一个小插件:

// plugins/function.inc.php
function smarty_function_inc($params, Smarty &$smarty)
{
   $params['step'] = empty($params['step']) ? 1 : intval($params['step']);

   if (empty($params['var'])) {
      trigger_error("inc: missing 'var' parameter");
      return;
   }
   if (!in_array($params['var'], array_keys($smarty->_tpl_vars))) {
      trigger_error("inc: trying to increment unassigned variable ".$params['var']);
      return;
   }
   if (isset($smarty->_tpl_vars[$params['var']])) {
      $smarty->assign($params['var'],
      $smarty->_tpl_vars[$params['var']] + $params['step']);
   }
}

The function would then be called like this, notice that stepis optional and if not given the variable will be incremented by one:

然后将像这样调用该函数,注意它step是可选的,如果没有给出,变量将增加 1:

{assign var=var value=0}
{inc var=var step=2}
{$var} // displays 2

Reference
Smarty {assign}
Smarty {capture}
Extending Smarty With Plugins

参考
Smarty {assign}
Smarty {capture}
使用插件扩展 Smarty

回答by trapper

It's cleaner just to do it like this...

这样做更干净......

{$number = $number +1}

回答by huncyrus

Better to use the built in Smarty "counter" >> {counter}element.

最好使用内置的 Smarty“计数器”>>{counter}元素。

So, in template you can use:

因此,在模板中您可以使用:

<div>Some text, html code, whatever... </div>
{* init the counter! *}
{counter start=0 print=false} {* now the tpl doesn't show the "0" number *}


{* 3x run :D *}
{some_cyclic_stuff_like_foreach_or_section}
    Run the counter: {counter}
{/some_cyclic_stuff_like_foreach_or_section}

It will print for you:

它将为您打印:

Run the counter: 1
Run the counter: 2
Run the counter: 3

So, at least, you can use it with the print=falseoption, and you have the counter but it's hidden.

因此,至少,您可以将它与print=false选项一起使用,并且您拥有计数器但它是隐藏的。

If you use it the "variable way" (like the upper section write) you also can hide it with html/css or just simply don't let it print :)

如果您以“可变方式”使用它(如上部分写入),您也可以使用 html/css 隐藏它,或者只是不让它打印:)

回答by ridecar2

If I had to do this I would do something like the following: {php}$number++{/php}but it is very ugly to have to use php in a smarty template. This might suggest that there is a better way of doing what you planned to do.

如果我必须这样做,我会执行以下操作:{php}$number++{/php}但是必须在 smarty 模板中使用 php 非常难看。这可能表明有更好的方法来完成您计划做的事情。