简体   繁体   English

标记内的条件式

[英]Smarty conditional inside a tag

I want to create an attribute in a an just if a condition applies. 我想在条件适用的情况下在中创建一个属性。 Say I have a condition in a var like this: $mycondition. 假设我在这样的var中有一个条件:$ mycondition。

I am trying to do: 我正在尝试做:

<a {if $mycondition} attribute = "test" {{/if}} href="...">link</a>

How would I do this? 我该怎么做?

Just try to remove double brackets in the end of if condition. 只需尝试在if条件的末尾删除双括号。 Also, don't leave spaces between attribute and it's value. 另外,请不要在属性及其值之间留空格。 This should work for you: 这应该为您工作:

<a {if $mycondition}attribute="test"{/if} href="...">link</a>

Take a look at the smarty assign function. 看一下smarty分配功能。 Here is a link to the documentation. 是文档的链接。 You can do something like 你可以做类似的事情

 <{if $mycondition}><{assign var="attribute" value="test"}<{/if}>

You can then use attribute like <{$attribute}> in your code. 然后,您可以在代码中使用<{$attribute}>类的<{$attribute}> Will that work? 那行得通吗?

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

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