vibe.d selectively include attribute into tag using diet template

Steven Schveighoffer schveiguy at gmail.com
Sat Feb 27 19:12:55 UTC 2021


On 2/27/21 12:48 PM, JG wrote:
> Hi,
> 
> I know that one can do the following:
> tag(attribute='#{dexpression}')
> 
> But, is there a way to deal with attributes that don't get assigned values.
> That is, is there a way to produce
> <tag>
> or
> <tag attribute>
> depending on a boolean variable?
> 
> Of course one can do:
> - if (booleanVariable)
>   tag(attribute)
>    include ...
> - if (!booleanVariable)
>   tag
>    include ...
> 
> But this seems rather messy.

Yes, if you assign a boolean value to it directly, then if true, the 
attribute is included, if not, it's not.

e.g.:

tag(attribute=booleanVariable)

Note the lack of quotes. If you us an expression without quotes in diet, 
it becomes an interpolation. In the special case that it's a boolean, it 
becomes a switch to tell whether to include the attribute or not. If 
it's a complex expression you might need parentheses:

tag(attribute=(booleanVariable ? true : false))

-Steve


More information about the Digitalmars-d-learn mailing list