Thread overview
vibe.d selectively include attribute into tag using diet template
Feb 27, 2021
JG
Feb 28, 2021
JG
Mar 01, 2021
JG
February 27, 2021
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.
February 27, 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
February 28, 2021
On Saturday, 27 February 2021 at 19:12:55 UTC, Steven Schveighoffer wrote:
>
> 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.

Thank you very much for this.

> If you use an expression without quotes
> in diet, it becomes an interpolation.

Would you mind explaining in more detail what this means? How could one use this, other than with booleans?
February 28, 2021
On 2/28/21 12:29 AM, JG wrote:
> On Saturday, 27 February 2021 at 19:12:55 UTC, Steven Schveighoffer wrote:
>>
>> If you use an expression without quotes
>> in diet, it becomes an interpolation.
> 
> Would you mind explaining in more detail what this means? How could one use this, other than with booleans?

So if you have an expression as the right side of an attribute assignment, it is treated as a D expression, which is then evaluated and turned into string form.

So e.g. your original example:

tag(attribute='#{dexpression}')

can be written as:

tag(attribute=dexpression)

-Steve
March 01, 2021
On Sunday, 28 February 2021 at 18:10:26 UTC, Steven Schveighoffer wrote:
> On 2/28/21 12:29 AM, JG wrote:
>> On Saturday, 27 February 2021 at 19:12:55 UTC, Steven Schveighoffer wrote:
>>>
>>> If you use an expression without quotes
>>> in diet, it becomes an interpolation.
>> 
>> Would you mind explaining in more detail what this means? How could one use this, other than with booleans?
>
> So if you have an expression as the right side of an attribute assignment, it is treated as a D expression, which is then evaluated and turned into string form.
>
> So e.g. your original example:
>
> tag(attribute='#{dexpression}')
>
> can be written as:
>
> tag(attribute=dexpression)
>
> -Steve

Thanks