如何在Smarty中组合两个字符串?

发布于 2024-12-04 01:41:50 字数 332 浏览 2 评论 0 原文

为什么这在 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...

Why doesn't this work in Smarty?

{my_function($test.'a1')}

It's showing following error:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江南月 2024-12-11 01:41:50

我只使用了一点 smarty,但我认为如果你用反引号包围你的串联,那么它会正确地评估它们。下面的示例:

{my_function(`$test.'a1'`)} 

还有一个内置的 allocate 函数也可能很有用: http://www.smarty.net/docsv2/en/language.custom.functions.tpl#language.function.assign

最后,如果所有其他方法都失败,请在 php 中进行 concat 并分配将其传递给单个变量并传递到模板。


编辑,忽略上述建议,我认为您应该使用以下语法:

{my_function var="`$test`a1"}

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:

{my_function(`$test.'a1'`)} 

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:

{my_function var="`$test`a1"}

http://www.smarty.net/docsv2/en/language.syntax.quotes.tpl

や三分注定 2024-12-11 01:41:50

如果您将其传递给函数,则可以进行捕获或分配

{capture assign="parameter"}{$test}a1{/capture} {my_function($parameter)}

{assign var="parameter" value=$test|cat:"a1"} {my_function($parameter)}

我还没有尝试在函数的参数上使用修饰符。但你可以尝试一下。此外,由于它是一个自定义 smarty 函数,您可以添加第三个可选参数并将函数中的值连接起来。

<?php

function smarty_function_my_function($params, $smarty) {
   $input = join('', $params);
}

If you're doing it passing into a function you can do a capture or assign

{capture assign="parameter"}{$test}a1{/capture} {my_function($parameter)}

{assign var="parameter" value=$test|cat:"a1"} {my_function($parameter)}

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.

<?php

function smarty_function_my_function($params, $smarty) {
   $input = join('', $params);
}
酸甜透明夹心 2024-12-11 01:41:50

这是因为 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.

筱果果 2024-12-11 01:41:50

我找到了这段代码并且对我来说工作得很好

Location smarty\plugins\function.concat.php

<?php
function smarty_function_concat($params){
    return implode('', $params);
}
?>

On .tpl code:

{concat 1="字符串1" 2="第二个" 3="另一个字符串"}

I have found this code and works fine for me

Location smarty\plugins\function.concat.php

<?php
function smarty_function_concat($params){
    return implode('', $params);
}
?>

On .tpl code:

{concat 1="string1" 2="second" 3="an other string"}

他不在意 2024-12-11 01:41:50

这就是你所需要的

{$variable_1 = "Green"}
{$variable_2 = "Red"}
{$combined = "`$variable_1`, `$variable_2`"}
{$combined}

这将返回绿色,红色

This is what you need

{$variable_1 = "Green"}
{$variable_2 = "Red"}
{$combined = "`$variable_1`, `$variable_2`"}
{$combined}

This will return Green, Red

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文