www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibe.d: is it possible to use bare HTML with the functionalty of DIET

reply someone <someone somewhere.com> writes:
Regarding vibe.d I think I'll give it a try (maybe placing it 
behind nginx at first) since I do really got a good 
first-impression ... kudos to the developers/maintainers :)

I like the idea of having D at my disposal within a web page, 
actually, it is a terrific feature to say the least.

What I do not like (even a bit) are the pseudo-HTML DIET 
templates. I can understand they can make life easy for some, but 
I am not the guy having any trouble writing well-good-structured 
HTML/XHTML/XML/etc to begin with, nor I am the kind of guy 
grunting because I will be forced to write closing tags and the 
like.

That being said, my specific question is:

Can I use vibe.d *without* DIET templates manually writing say, 
XHTML 1.1 pages, *while having D* at my disposal with the - 
prefixes I have seen so far ?

I mean, I am almost sure I can write D functions returning text 
and making my web page on-the-fly, but this is not what I have in 
mind, I would like to have an actual text file for a web page 
with the aforementioned - prefixes to actually hook D code 
leveraging the pre-compiled feature of DIET templates.
Aug 30 2021
next sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Tuesday, 31 August 2021 at 00:09:14 UTC, someone wrote:
 Can I use vibe.d *without* DIET templates manually writing say, 
 XHTML 1.1 pages, *while having D* at my disposal with the - 
 prefixes I have seen so far ?
I don't know much about vibe.d (I have my own D web stuff) but just for fun I wanted to try passing my dom.d through the ctfe engine to do the embedded code thing. 50ish lines for the basic extractor i slapped together in 20 mins. --- import arsd.dom; string toD(string s) { return `append(` ~ "`" ~ s ~ "`" ~ `);`; } template loadTemplateMixin(string doc) { string helper() { Document document = new Document; document.parseSawAspCode = (string) => true; document.parseStrict(doc); string code; void expand(Element element) { if(auto asp = cast(AspCode) element) { if(asp.source.length > 3 && asp.source[1] == '=') code ~= `append(` ~ asp.source[2 .. $-1] ~ `);`; else code ~= asp.source[1 .. $-1]; } else if(auto tn = cast(TextNode) element) { code ~= toD(tn.toString()); } else if(auto sn = cast(SpecialElement) element) { code ~= toD(sn.toString()); } else { code ~= toD("<" ~ element.tagName); foreach(k, v; element.attributes) { code ~= toD(" "); code ~= toD(k.htmlEntitiesEncode); code ~= toD("=\""); code ~= toD(v.htmlEntitiesEncode); code ~= toD("\""); } code ~= toD(">"); foreach(child; element.children) expand(child); code ~= toD("</" ~ element.tagName ~ ">"); } } expand(document.root); return code; } enum loadTemplateMixin = helper(); } // USAGE HERE // this could be in a file import("file.html") too btw enum doc = `<html><script> foo</script><style>css</style><test id="main"><%= my_string[0 .. 5] %></test> <span>foo</span> <span>foo</span> <span>foo</span> <% foreach(item; strings) append(item); %> </html>`; void main() { string html; // and it can see these variables in the <% %> blocks string my_string = "hello world"; string[] strings = ["omg", "wtf", "lol"]; void append(string s) { html ~= s; } mixin(loadTemplateMixin!doc); import std.stdio; writeln(html); } --- Not exactly the fastest compile though but I could prolly optimize that if i spent a lil more time on it. Now that it yields a string though you can return that to vibe using whatever method it uses. Also note that you get a compile error on malformed input xhtml too.
Aug 30 2021
prev sibling next sibling parent reply bauss <jj_1337 live.dk> writes:
On Tuesday, 31 August 2021 at 00:09:14 UTC, someone wrote:
 Regarding vibe.d I think I'll give it a try (maybe placing it 
 behind nginx at first) since I do really got a good 
 first-impression ... kudos to the developers/maintainers :)

 I like the idea of having D at my disposal within a web page, 
 actually, it is a terrific feature to say the least.

 What I do not like (even a bit) are the pseudo-HTML DIET 
 templates. I can understand they can make life easy for some, 
 but I am not the guy having any trouble writing 
 well-good-structured HTML/XHTML/XML/etc to begin with, nor I am 
 the kind of guy grunting because I will be forced to write 
 closing tags and the like.

 That being said, my specific question is:

 Can I use vibe.d *without* DIET templates manually writing say, 
 XHTML 1.1 pages, *while having D* at my disposal with the - 
 prefixes I have seen so far ?

 I mean, I am almost sure I can write D functions returning text 
 and making my web page on-the-fly, but this is not what I have 
 in mind, I would like to have an actual text file for a web 
 page with the aforementioned - prefixes to actually hook D code 
 leveraging the pre-compiled feature of DIET templates.
You might be interested in https://yuraiweb.org/ Even though it's a work in progress then you should be able to get by just fine with the basics for now.
Aug 31 2021
parent someone <someone somewhere.com> writes:
On Tuesday, 31 August 2021 at 07:40:10 UTC, bauss wrote:

 You might be interested in https://yuraiweb.org/

 Even though it's a work in progress then you should be able to 
 get by just fine with the basics for now.
Thanks for the tip bauss :) ! I am exploring it right now. Main problem is the lack of documentation: https://yuraiweb.org/docs/specifications/basics ... every section is empty. My two cents-so-far: https://yuraiweb.org/ Functionality Diet Yurai ... Total Score* 9(11)/20 20/20 * A higher score is better. Methinks things like this are downs, its like insulting the intelligence of their potential customers. They are advertising software not kitchen appliances for gramma. At least they are reminding me that higher scores are better ... thanks for the tip you yurai developers !
Aug 31 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/30/21 8:09 PM, someone wrote:
 Regarding vibe.d I think I'll give it a try (maybe placing it behind 
 nginx at first) since I do really got a good first-impression ... kudos 
 to the developers/maintainers :)
 
 I like the idea of having D at my disposal within a web page, actually, 
 it is a terrific feature to say the least.
 
 What I do not like (even a bit) are the pseudo-HTML DIET templates. I 
 can understand they can make life easy for some, but I am not the guy 
 having any trouble writing well-good-structured HTML/XHTML/XML/etc to 
 begin with, nor I am the kind of guy grunting because I will be forced 
 to write closing tags and the like.
 
 That being said, my specific question is:
 
 Can I use vibe.d *without* DIET templates manually writing say, XHTML 
 1.1 pages, *while having D* at my disposal with the - prefixes I have 
 seen so far ?
The generation of code to output the page depends on the diet file format (i.e. code islands are designated by the leading `-`). However, vibe-d does not require using the diet template system. There are others which probably do what you want (search on code.dlang.org), but I'm a huge fan of diet templates (I actually prefer writing non-template html that way), so I don't have any experience with others. Given how templating systems work (and how D allows strings to be used as code using mixins), it's likely pretty trivial to write a simple templating system to do this. All you need is an escape protocol that is unlikely to appear in HTML, and you can probably get away with a 10 line function that doesn't need to actually parse the HTML. -Steve
Aug 31 2021
parent reply someone <someone somewhere.com> writes:
On Tuesday, 31 August 2021 at 14:06:32 UTC, Steven Schveighoffer 
wrote:

 The generation of code to output the page depends on the diet 
 file format (i.e. code islands are designated by the leading 
 `-`).
 However, vibe-d does not require using the diet template system.
Does that means I can still get the code islands resolved on a, say, plain-XHTML file ? Or does that means that I should output HTML/XHTML from my own functions instead ?
 There are others which probably do what you want (search on 
 code.dlang.org), but I'm a huge fan of diet templates (I 
 actually prefer writing non-template html that way), so I don't 
 have any experience with others.
https://yuraiweb.org/ as pointed by another user seems something I probably want to give it a try -it still relies on vibe.d but switched to plain HTML with its own code-islands more-or-less a-la ASP.net ... although it is still a work-in-progress.
 Given how templating systems work (and how D allows strings to 
 be used as code using mixins), it's likely pretty trivial to 
 write a simple templating system to do this. All you need is an 
 escape protocol that is unlikely to appear in HTML, and you can 
 probably get away with a 10 line function that doesn't need to 
 actually parse the HTML.
Probably. Bit I am not a huge fan of modifying libraries for minor functionality fixes (unless is really really necessary). For whatever reasons I already have custom nginx builds etc etc so I do not want to keep tracking and fixing more software -in the end is a pain-in-the-ass. However, I think vibe.d perhaps ought to provide a switch/flag to enable/disable DIET constructs on source files (views) resolving its code islands. Or more straightforward, if the requested file already has another extension other than the one used by DIET files just resolve the code-islands and be with it. No client-side code changes -business as usual. Or use another directory other than /views/ for bare files without DIET syntax. Just thinking ... what do you think ?
 -Steve
Aug 31 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/31/21 8:40 PM, someone wrote:
 On Tuesday, 31 August 2021 at 14:06:32 UTC, Steven Schveighoffer wrote:
 
 The generation of code to output the page depends on the diet file 
 format (i.e. code islands are designated by the leading `-`).
 However, vibe-d does not require using the diet template system.
Does that means I can still get the code islands resolved on a, say, plain-XHTML file ? Or does that means that I should output HTML/XHTML from my own functions instead ?
Vibe just provides an output range for use in diet. You can use anything, including just writing the data yourself, or using an alternative template system.
 Given how templating systems work (and how D allows strings to be used 
 as code using mixins), it's likely pretty trivial to write a simple 
 templating system to do this. All you need is an escape protocol that 
 is unlikely to appear in HTML, and you can probably get away with a 10 
 line function that doesn't need to actually parse the HTML.
Probably. Bit I am not a huge fan of modifying libraries for minor functionality fixes (unless is really really necessary). For whatever reasons I already have custom nginx builds etc etc so I do not want to keep tracking and fixing more software -in the end is a pain-in-the-ass.
You aren't modifying anything. Vibe-d provides default access to diet-ng, but you have no obligation to use it. Just use the output range (`HTTPServerResponse.bodyWriter`) and hook up your preferred templating system. I think you are misunderstanding the architecture of vibe. There is no need to replace anything inside vibe to use a different templating system, it does not depend on diet at all, just provides default access. The views directory isn't exactly special either (though there may be code in dub that deals with looking at modification times), nor is the extension `.dt`. Most of the diet compiler is dealing with transforming the pug format into HTML, and proper string interpolation. The code island stuff is quite trivial (just copied as-is). Oh, and I realized I forgot about string interpolation. You definitely want a way to change D expressions into string output. You can still do this with code-islands, but a good template system would handle the boilerplate for you. Which leads me to -- diet really should be split into 2 parts, one that handles the pug parsing and compiling, and one that handles proper string interpolation. Then you could leverage that second part. -Steve
Sep 01 2021