www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibe.d selectively include attribute into tag using diet template

reply JG <someone somewhere.com> writes:
Hi,

I know that one can do the following:


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.
Feb 27 2021
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/27/21 12:48 PM, JG wrote:
 Hi,
 
 I know that one can do the following:

 
 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
Feb 27 2021
parent reply JG <someone somewhere.com> writes:
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?
Feb 27 2021
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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: can be written as: tag(attribute=dexpression) -Steve
Feb 28 2021
parent JG <someone somewhere.com> writes:
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: can be written as: tag(attribute=dexpression) -Steve
Thanks
Mar 01 2021