www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vibe.d diet templates

reply JG <JG somewhere.com> writes:
Suppose I have an array of attributes and values v is there any 
way to apply these attributes to a tag?

So that something like



becomes

<tag attribute0=value0 ....>

where v[0][0]="attribute0" and v[0][1]="value0"?
Jun 17
parent reply WebFreak001 <d.forum webfreak.org> writes:
On Thursday, 17 June 2021 at 08:23:54 UTC, JG wrote:
 Suppose I have an array of attributes and values v is there any 
 way to apply these attributes to a tag?

 So that something like



 becomes

 <tag attribute0=value0 ....>

 where v[0][0]="attribute0" and v[0][1]="value0"?
I think there is nothing for this built-in in diet, so you have to manually emit raw HTML: ```diet - import std.xml : encode; - auto start = appender!string; - start ~= "<tag"; - foreach (pair; v) - start ~= " "; - start ~= pair[0].encode; - start ~= "=\""; - start ~= pair[1].encode; - start ~= '"'; - start ~= ">"; |!= start // content here |!= "</tag>" ``` Maybe some attribute splat operator would be nice for this.
Jun 17
parent reply JG <someone somewhere.com> writes:
On Thursday, 17 June 2021 at 09:16:56 UTC, WebFreak001 wrote:
 On Thursday, 17 June 2021 at 08:23:54 UTC, JG wrote:
 Suppose I have an array of attributes and values v is there 
 any way to apply these attributes to a tag?

 So that something like



 becomes

 <tag attribute0=value0 ....>

 where v[0][0]="attribute0" and v[0][1]="value0"?
I think there is nothing for this built-in in diet, so you have to manually emit raw HTML: ```diet - import std.xml : encode; - auto start = appender!string; - start ~= "<tag"; - foreach (pair; v) - start ~= " "; - start ~= pair[0].encode; - start ~= "=\""; - start ~= pair[1].encode; - start ~= '"'; - start ~= ">"; |!= start // content here |!= "</tag>" ``` Maybe some attribute splat operator would be nice for this.
Thanks, this works. I would have thought this would be a common enough use case to have support in diet. Anyone else wanted this?
Jun 17
next sibling parent reply WebFreak001 <d.forum webfreak.org> writes:
On Thursday, 17 June 2021 at 16:26:57 UTC, JG wrote:
 [...]

 Thanks, this works. I would have thought this would be a common 
 enough use case to have support in diet. Anyone else wanted 
 this?
Opened an issue here: https://github.com/rejectedsoftware/diet-ng/issues/91
Jun 17
parent JG <JG somewhere.com> writes:
On Thursday, 17 June 2021 at 18:54:41 UTC, WebFreak001 wrote:
 On Thursday, 17 June 2021 at 16:26:57 UTC, JG wrote:
 [...]

 Thanks, this works. I would have thought this would be a 
 common enough use case to have support in diet. Anyone else 
 wanted this?
Opened an issue here: https://github.com/rejectedsoftware/diet-ng/issues/91
Thanks for opening that issue.
Jun 17
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/17/21 12:26 PM, JG wrote:

 Thanks, this works. I would have thought this would be a common enough 
 use case to have support in diet. Anyone else wanted this?
I haven't found a need for it, as I'm usually only dynamically configuring attribute values, not attribute names. But my web-fu is pretty weak. However, what I *have* wanted is to have attribute values support `Nullable!T` such that they are only included if the item is non-null. See [here](https://github.com/rejectedsoftware/diet-ng/issues/28). -Steve
Jun 17
parent reply kdevel <kdevel vogtner.de> writes:
On Thursday, 17 June 2021 at 19:14:28 UTC, Steven Schveighoffer 
wrote:
 On 6/17/21 12:26 PM, JG wrote:

 However, what I *have* wanted is to have attribute values 
 support `Nullable!T` such that they are only included if the 
 item is non-null. See 
 [here](https://github.com/rejectedsoftware/diet-ng/issues/28).
BTW: Is it possible to replace the diet generator in vibe.d with a template engine like moustache [1] which is agnostic wrt the code it produces? In the past 25 or so years I frequently encountered designed HTML pages where only some data had to be inserted here or there. If I got the vibe.d model right one would have to reimplement these HTML pages in the diet language first. [1] https://github.com/repeatedly/mustache-d
Jun 17
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/17/21 4:22 PM, kdevel wrote:
 On Thursday, 17 June 2021 at 19:14:28 UTC, Steven Schveighoffer wrote:
 On 6/17/21 12:26 PM, JG wrote:

 However, what I *have* wanted is to have attribute values support 
 `Nullable!T` such that they are only included if the item is non-null. 
 See [here](https://github.com/rejectedsoftware/diet-ng/issues/28).
BTW: Is it possible to replace the diet generator in vibe.d with a template engine like moustache [1] which is agnostic wrt the code it produces? In the past 25 or so years I frequently encountered designed HTML pages where only some data had to be inserted here or there. If I got the vibe.d model right one would have to reimplement these HTML pages in the diet language first. [1] https://github.com/repeatedly/mustache-d
Of course. Vibe's diet support is wholly based on the diet-ng project, and you don't have to use it. It's just there out of the box. when you do: ```d res.render!("sometemplate.dt", all, my, args); ``` It's just a UFCS call. You could replace this with: res.renderMustache(...) Where you have to write the adapter. It's possible to get a char output range out of HTTPServerResponse, which you then can write to and it's just sent back to the client. This seems like it would do the trick, you just have to pass the output range in as the sink: https://mustache-d.dpldocs.info/mustache.MustacheEngine.render.2.html -Steve
Jun 17