php Smarty - 变量加法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6801222/
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 01:17:30  来源:igfitidea点击:

Smarty - variable addition

phpsmarty

提问by RSK

I want to add a constant value to a variable in smarty. just like:

我想在 smarty 中为变量添加一个常量值。就像:

{assign var='c' value='0'}
$c=$c+1

回答by duality_

Try this:

尝试这个:

{assign var='c' value=0}
{assign var='c' value=$c+1}

The short form should work too, but you say it doesn't.

简短形式也应该有效,但你说它没有。

{$c=0}
{$c=$c+1}

But this doesn't work because you're using Smarty 2, right? Because in Smarty 3 it should work.

但这不起作用,因为您使用的是 Smarty 2,对吗?因为在 Smarty 3 中它应该可以工作。

回答by fyr

Try:

尝试:

{assign var="c" value="`$something+$constant`"}

But usually the sense of template frameworks is to follow the mvc pattern. So all the logic is done in a controller. Or in the case of you some sort of php script. The view should not hold much logic(less logic better view code). So any sort of calculations should not be in a view. In mvc you will have however some sort of logic like iterations or link generation(through e.g. smarty plugins).

但通常模板框架的意义是遵循mvc模式。所以所有的逻辑都是在一个控制器中完成的。或者在您的情况下使用某种 php 脚本。视图不应该包含太多逻辑(逻辑越少越好查看代码)。因此,不应在视图中进行任何类型的计算。然而,在 mvc 中,您将拥有某种逻辑,例如迭代或链接生成(通过例如 smarty 插件)。

回答by salathe

You can use expressions with the {assign}template function.

您可以将表达式与{assign}模板函数一起使用。

{assign var=c value=$c+1}

Or in its short form,

或者以它的简短形式,

{$c=$c+1}