vibe-d / vibe.d

Official vibe.d development
MIT License
1.15k stars 284 forks source link

Angular/Mustache-like templates for vibe.d? #1134

Open IgorStepanov opened 9 years ago

IgorStepanov commented 9 years ago

Hello. As I see, vibe.d has only one template tool: diet. Without going into the debate about diet advantages/disadvantages, we have to admit that it looks strange for a beginner, and not always attractive for the experienced user. I suggest to add alternative template tool, similar to mustache templates. In this case, templates in vibe.d seem comfortable for the majority of web programmers. Now about implementation: I have one implementation of templates. Initially, it didn't make for vibe.d, however I ready to adapt it to the requirements of vibe.d What it can:

{{#import common/common.dsp}} <!-- Import macroses for another file. -->

<!-- Insert `test_1` value from the model, which passed to template processor. -->
simple text{{test_1}}text 

<!-- Different expressions are allowed (in this case, built-in function 'fxx.bar' is called).
You able to register your built-in values for the temlplate processor.
-->
{{fxx.bar()}} 
{{fxx.bar("}}")}}

<!-- If-then-else statement. All variables is taken from `Model` -->
{{#if foo != boo}}
Yes
{{#else}}
No
{{/#if}}

<!-- Foreach construction. key and foo values inside foreach hides a global key and foo values -->
{{#foreach key, foo; array}}
    {{key}} => {{foo.val}}
{{/#foreach}}
{{foo}}

<!-- My templates allow to be extended with the user constructions. They should be declared in D. swich-case is the one of them. -->
{{#switch 1 + 1}}
{{#case 1}}
One
{{/#case}}
{{#case 2}}
Two
{{/#case}}
{{#default}}
Unknown
{{/#default}}
{{/#switch}}

<!-- Macros (subtemplate), which takes two arguments. -->
{{@mySub a, b}}
    {{#if a}}
             <p>{{b}}</p>
             {{&mySub a - 1, b}} <!--recursion is allowed-->
        {{/#if}}
{{/@mySub}}

{{&mySub 3, "BOOM"}} <!-- Writes "BOOM" three times -->
{{&myCommonSub "foo"}} <!-- Call a subtemplate from imported file. -->

{{{<html>}}} <!-- Encode html, writes "&lt;html&gt;" -->
s-ludwig commented 9 years ago

There is already a Mustache template implementation (mustache-d), as well as some similar libraries: temple, embd

All of them can work with vibe.d, and I think one of them (I think "temple") has some additional convenience functions for seamless integration into vibe.d apps.

The plan for vibe.d is to eventually split up the library into separate DUB packages, so any new templating solution should already be implemented as a separate package, too. Your implementation looks to have some advanced syntax features - I think some more than mustache-d -, so maybe it makes sense to simply try to extend mustache-d?

Geod24 commented 9 years ago

The plan for vibe.d is to eventually split up the library into separate DUB packages, so any new templating solution should already be implemented as a separate package, too.

On that topic, do you plan to create a Vibe.d organization, or similar, where project that officially integrate with Vibe.d (and are well tested) are put ? I can foresee some compatibility issues with the moving targets that D and Vibe.d are.