简体   繁体   English

从特定值向Smarty中的var赋值

[英]Assign value to var in Smarty from specific values

Can I assign this: 我可以分配这个吗?

{{$phone_numbers.0.phone_number}} 
{{if $phone_numbers.0.extension}} EXT. {{$phone_numbers.0.extension}}{{/if}}

to a var in Smarty3? 到Smarty3中的var? How? 怎么样? I have seen assign but I don't know how to concatenate values using it, any help? 我已经看到过assign但是我不知道如何使用它来连接值,有什么帮助吗?

EDIT: is this the right way? 编辑:这是正确的方法吗?

What do you think about this: 你怎么看待这件事:

{{$phone_number=$phone.phone_number}}
{{if $phone.extension}} {{$phone_number|cat:"EXT. $phone.extension"}}{{/if}}

You cannot use conditionals in {assign} but you can use {capture} : 您不能在{assign}使用条件{assign}但可以使用{capture}

{{capture name=foo assign=bar}}
  {{$phone_numbers.0.phone_number}} 
  {{if $phone_numbers.0.extension}} EXT. {{$phone_numbers.0.extension}}{{/if}}
{{/capture}}

This renders the content of the {capture} block but doesn't output it. 这将呈现{capture}块的内容,但不会输出。 Instead it stores is for later use. 相反,它存储供以后使用。

You can find it in {$smarty.capture.foo} or in the variable {$bar} . 您可以在{$smarty.capture.foo}或变量{$bar}找到它。

The assign attribute is optional; assign属性是可选的; you don't use it and the variable {$bar} (or whatever name you use) is not modified. 您不会使用它,并且变量{$bar} (或您使用的任何名称)都不会被修改。 However, the captured content is always stored in the variable {$smarty.capture.foo} (or whatever name you use for it). 但是,捕获的内容始终存储在变量{$smarty.capture.foo} (或您使用的任何名称)。

As an alternative way, in the second example, the right syntax would be: 作为替代方式,在第二个示例中,正确的语法为:

{{$phone_number=$phone_numbers.0.phone_number}}
{{if $phone_numbers.0.extension}}
{{$phone_number=$phone_number|cat:" EXT. "|cat:$phone_numbers.0.extension}}
{{/if}}

and then use {{$phone_number}} 然后使用{{$ phone_number}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM