如何在Smarty中组合两个字符串?
为什么这在 Smarty 中不起作用?
{my_function($test.'a1')}
它显示以下错误:
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "test.tpl" on line 1 "{my_function($test.'a1')}" Unexpected "'a1'", expected one of: "{" , "$" , "identifier" , INTEGER' in...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我只使用了一点 smarty,但我认为如果你用反引号包围你的串联,那么它会正确地评估它们。下面的示例:
还有一个内置的 allocate 函数也可能很有用: http://www.smarty.net/docsv2/en/language.custom.functions.tpl#language.function.assign
最后,如果所有其他方法都失败,请在 php 中进行 concat 并分配将其传递给单个变量并传递到模板。
编辑,忽略上述建议,我认为您应该使用以下语法:
http://www.smarty.net/docsv2/en/language.syntax.quotes.tpl
I have only used smarty a little but I think if you surround your concatenation with backticks then it will evaluate them properly. Example below:
There is also the assign built in function which may also be useful: http://www.smarty.net/docsv2/en/language.custom.functions.tpl#language.function.assign
Finally, if all else fails do the concat in php and assign it to a single variable and passing that to the template.
Edit, ignore the above suggestions, I think you should be using the following syntax:
http://www.smarty.net/docsv2/en/language.syntax.quotes.tpl
如果您将其传递给函数,则可以进行捕获或分配
我还没有尝试在函数的参数上使用修饰符。但你可以尝试一下。此外,由于它是一个自定义 smarty 函数,您可以添加第三个可选参数并将函数中的值连接起来。
If you're doing it passing into a function you can do a capture or assign
I have not tried using a modifier on the parameter to a function. But you could try it out. Also since its a custom smarty function you could add a third optional parameter and concatenate the values in side the function.
这是因为 Smarty 不理解 PHP 语法。它使用自己的语法,如此处所述,并快速与这里是 PHP 语法。 (我添加了后一个链接来强调这一点——Smarty 语法就是 Smarty 语法。)
请参阅 将变量嵌入双引号,这应该给出如何完成任务的想法。 (提示:在这种情况下,引号内必须使用反引号)。
快乐编码。
It is because Smarty does not understand PHP syntax. It uses it's own syntax as described here and quickly compared to PHP syntax here. (I have included the latter link to reinforce the point -- Smarty syntax is Smarty syntax.)
See the section on embedding vars in double quotes which should given an idea of how to accomplish the task. (Hint: for this case back-ticks must be used inside the quotes).
Happy coding.
我找到了这段代码并且对我来说工作得很好
Location smarty\plugins\function.concat.php
On .tpl code:
I have found this code and works fine for me
Location smarty\plugins\function.concat.php
On .tpl code:
这就是你所需要的
这将返回绿色,红色
This is what you need
This will return Green, Red