D for Web Development?

Michael Reiland via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 9 15:01:14 PDT 2017


On Friday, 9 June 2017 at 15:07:03 UTC, Adam D. Ruppe wrote:
> // compile with the cgi.d, database.d, postgres.d, and dom.d 
> library modules
> // and make sure your C libpq library is available. so on my 
> computer, the
> // compile command is:
> // dmd webtest.d  ~/arsd/{cgi,database,postgres,dom} 
> -L-L/usr/local/pgsql/lib -m64 -version=embedded_httpd
> import arsd.cgi;
> import arsd.postgres;
> import arsd.dom;
>
> void handle(Cgi cgi) {
>         // database setup: psql me
>         // create table test (id integer, data text);
>
>         auto database = new PostgreSql("dbname = me");
>         scope(exit) .destroy(database);
>
>         // insert some data from the request into the 
> database...
>         database.query("INSERT INTO test (data) VALUES (?)", 
> cgi.request("data"));
>
>         // of course, you could also load the html skeleton 
> from a file
>         auto document = new Document("<!DOCTYPE 
> html><html><head><title>Demo</title></head><body></body></html>", true, true);
>
>         foreach(row; database.query("SELECT id, data FROM 
> test")) {
>                 document.mainBody.addChild("b", row["id"]);
>                 document.mainBody.addChild("p", row["data"]);
>         }
>
>         cgi.write(document.toString(), true);
> }
>
> mixin GenericMain!handle;
>

So if I'm understanding this code correctly, your libs don't have 
any sort of routing setup either, and I would have to write the 
code to inspect the URL and do the right thing?


> I don't use any of the templating engines... in the demo above, 
> I used my dom.d though which is a potential base lib to make 
> one. It is a html parser with many functions for modification. 
> You can write your "templates" as just .html files and load 
> them with this, then search for various ID elements or whatever 
> (dom.d has `querySelector` and `querySelectorAll`) and append 
> elements with dom or setting things like `innerText` and 
> `innerHTML`.

I think I'd prefer to use more of a template system as it feels 
more productive having it all inline.  I have concerns about the 
complexity of getting "helper" type functionality in Temple, but 
I'll tackle that when I get to it.

I really really dislike the syntax for DIET, I was never a big 
HAML fan.


More information about the Digitalmars-d-learn mailing list