www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Temple templates with vibe.d support and first D experiences

reply =?UTF-8?B?IklzdHbDoW4gWsOzbHlvbWki?= <jelszonak gmail.com> writes:
Hi,

I've been lurking around in the forums for quite a long time now, 
but only recently tried D for a bit more than some trivial 
experimentation. Though the documentation of external libraries 
and the tooling itself is far from a commercial solution in 
quality, I understand that it's a community effort, so my 
experience is good in overall.

I really like what I've seen of vibe.d so far. The only strange 
part was Diet templates: though the concept is not bad at all, 
it's not practical. Unless you're also a good web designer, you 
usually start from existing HTML templates, using Diet templates 
would require a complete rewrite in such cases.

I've seen in some forums that the Temple template engine may be 
more practical in such cases and it has vibe.d support, so I gave 
it a try. I have the following function mapped to some URL:

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
	    auto ctx = new TempleContext;
     ctx.username = "testing";

     // Gives compile error
     // renderTemple!("Hello <%= var.username %>")(res, ctx);

     // I'd expect this behaves the same, but works fine
     string content = compile_temple!("Hello <%= var.username 
%>").toString(ctx);
     res.writeBody(content);
}

In the code above, function renderTemplate() gives the following 
compile error which I could not solve so far:

Compiling using dmd...
Compiling Temple with Vibed support
TempleFunc-mixin-1(51): Error: static assert  "Filter does not 
have a case that accepts a VariantN!32LU"
TempleFunc-mixin-1(17):        instantiated from here: 
__temple_buff_filtered_put!(VariantN!32LU)
../../../.dub/packages/temple-0.7.3/src/temple/package.d:47: 
InlineTemplate(1):        instantiated from here: 
__temple_put_expr!(VariantN!32LU)
src/temple/temple.d(77):        instantiated from here: 
TempleFunc!(TempleHtmlFilter)
../../../.dub/packages/temple-0.7.3/src/temple/package.d(43):     
    ... (2 instantiations, -v to show) ...
../../../.dub/packages/temple-0.7.3/src/temple/vibe.d(53):        
instantiated from here: Temple!("Hello <%= var.username %>", 
TempleHtmlFilter)
source/app.d(...):        instantiated from here: 
renderTemple!("Hello <%= var.username %>", TempleContext)

I'm using dub to build and run the application. Am I doing 
something wrong here, or is it something environmental like a 
library version conflict or wrong D deployment?

thanks
István
Mar 18 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 18 March 2015 at 08:13:37 UTC, István Zólyomi wrote:
 Hi,

 I've been lurking around in the forums for quite a long time 
 now, but only recently tried D for a bit more than some trivial 
 experimentation. Though the documentation of external libraries 
 and the tooling itself is far from a commercial solution in 
 quality, I understand that it's a community effort, so my 
 experience is good in overall.

 I really like what I've seen of vibe.d so far. The only strange 
 part was Diet templates: though the concept is not bad at all, 
 it's not practical. Unless you're also a good web designer, you 
 usually start from existing HTML templates, using Diet 
 templates would require a complete rewrite in such cases.

 I've seen in some forums that the Temple template engine may be 
 more practical in such cases and it has vibe.d support, so I 
 gave it a try. I have the following function mapped to some URL:

 void hello(HTTPServerRequest req, HTTPServerResponse res)
 {
 	    auto ctx = new TempleContext;
     ctx.username = "testing";

     // Gives compile error
     // renderTemple!("Hello <%= var.username %>")(res, ctx);

     // I'd expect this behaves the same, but works fine
     string content = compile_temple!("Hello <%= var.username 
 %>").toString(ctx);
     res.writeBody(content);
 }

 In the code above, function renderTemplate() gives the 
 following compile error which I could not solve so far:

 Compiling using dmd...
 Compiling Temple with Vibed support
 TempleFunc-mixin-1(51): Error: static assert  "Filter does not 
 have a case that accepts a VariantN!32LU"
 TempleFunc-mixin-1(17):        instantiated from here: 
 __temple_buff_filtered_put!(VariantN!32LU)
 ../../../.dub/packages/temple-0.7.3/src/temple/package.d:47: 
 InlineTemplate(1):        instantiated from here: 
 __temple_put_expr!(VariantN!32LU)
 src/temple/temple.d(77):        instantiated from here: 
 TempleFunc!(TempleHtmlFilter)
 ../../../.dub/packages/temple-0.7.3/src/temple/package.d(43):
    ... (2 instantiations, -v to show) ...
 ../../../.dub/packages/temple-0.7.3/src/temple/vibe.d(53):
   instantiated from here: Temple!("Hello <%= var.username %>", 
 TempleHtmlFilter)
 source/app.d(...):        instantiated from here: 
 renderTemple!("Hello <%= var.username %>", TempleContext)

 I'm using dub to build and run the application. Am I doing 
 something wrong here, or is it something environmental like a 
 library version conflict or wrong D deployment?

 thanks
 István
which dmd version?
Mar 18 2015
parent reply =?UTF-8?B?IklzdHbDoW4gWsOzbHlvbWki?= <jelszonak gmail.com> writes:
 which dmd version?
I'm using DMD64 D Compiler v2.066.0 with DUB version 0.9.22 and I've got the following dependencies in my dub.json: "dependencies": { "vibe-d": "~>0.7.19", "temple": "~>0.7.3" }
Mar 18 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 18 March 2015 at 13:44:39 UTC, István Zólyomi wrote:
 which dmd version?
I'm using DMD64 D Compiler v2.066.0 with DUB version 0.9.22 and I've got the following dependencies in my dub.json: "dependencies": { "vibe-d": "~>0.7.19", "temple": "~>0.7.3" }
It might not solve your problem but i strongly recommend using 2.066.1 instead. There are serious problems with 2.066.0 that were fixed in 2.066.1
Mar 18 2015
parent reply =?UTF-8?B?IklzdHbDoW4gWsOzbHlvbWki?= <jelszonak gmail.com> writes:
Still does not compile, thanks for the idea though. I think it's 
better to avoid Temple, compilation of Diet templates seems to be 
better anyway. E.g. temple seems to accept <% var.nonexistingname 
%> while diet gives a compile error for #{nonexistingname}.

Meanwhile I figured out an easy way to use existing HTML files 
with Diet, you can avoid reformatting your files to this exotic 
format. Just mark each line to be predefined content, simply 
prefixing them with the '|' character like this:

doctype html
| <html>
| <head> <title> testing vibe </title> </head>
| <body>
|     <h1> My vibe example </h1>
|     Hello #{username}!
|     <p> #{content} </p>
| </body>
| </html>

This can be easily automated by a few-liner script or whatever 
you prefer.



On Wednesday, 18 March 2015 at 16:27:41 UTC, John Colvin wrote:
 It might not solve your problem but i strongly recommend using 
 2.066.1 instead. There are serious problems with 2.066.0 that 
 were fixed in 2.066.1
Mar 20 2015
parent =?UTF-8?B?U8O2bmtlIEx1ZHdpZw==?= <sludwig rejectedsoftware.com> writes:
Am 20.03.2015 um 10:42 schrieb "István Zólyomi":
 Still does not compile, thanks for the idea though. I think it's better
 to avoid Temple, compilation of Diet templates seems to be better
 anyway. E.g. temple seems to accept <% var.nonexistingname %> while diet
 gives a compile error for #{nonexistingname}.

 Meanwhile I figured out an easy way to use existing HTML files with
 Diet, you can avoid reformatting your files to this exotic format. Just
 mark each line to be predefined content, simply prefixing them with the
 '|' character like this:

 doctype html
 | <html>
 | <head> <title> testing vibe </title> </head>
 | <body>
 |     <h1> My vibe example </h1>
 |     Hello #{username}!
 |     <p> #{content} </p>
 | </body>
 | </html>

 This can be easily automated by a few-liner script or whatever you prefer.



 On Wednesday, 18 March 2015 at 16:27:41 UTC, John Colvin wrote:
 It might not solve your problem but i strongly recommend using 2.066.1
 instead. There are serious problems with 2.066.0 that were fixed in
 2.066.1
Another possibility is to use a Jade-to-HTML converter, such as http://html2jade.org/
Apr 07 2015