www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Recommendation about templating engine library

reply Andrea <ilmanzo gmail.com> writes:
Hi folks,

Working on a side project I have the need to generate text files 
(mainly D source code) via a templating system. My use case is to 
have some JSON data populated at runtime from an API and fill-in 
placeholders in the text with the ability of doing loops over 
arrays, simple conditions and so on, mostly what 
https://pkg.go.dev/text/template provides for Go.

Any recommendation on a good library to use ?

I tried https://code.dlang.org/packages/temple but I don't wand 
to bring in the whole vibe-d dependency ; other projects I found 
in DUB like `mustache-d`, `jax` or `djinn` seems mostly 
unmaintained. Opinions ?

Many thanks
Mar 11
next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 March 2024 at 14:26:01 UTC, Andrea wrote:
 Hi folks,

 Working on a side project I have the need to generate text 
 files (mainly D source code) via a templating system. My use 
 case is to have some JSON data populated at runtime from an API 
 and fill-in placeholders in the text with the ability of doing 
 loops over arrays, simple conditions and so on, mostly what 
 https://pkg.go.dev/text/template provides for Go.

 Any recommendation on a good library to use ?

 I tried https://code.dlang.org/packages/temple but I don't wand 
 to bring in the whole vibe-d dependency ; other projects I 
 found in DUB like `mustache-d`, `jax` or `djinn` seems mostly 
 unmaintained. Opinions ?

 Many thanks
You have already mentioned mustache-d. If it compiles with the recent compilers go for it. I used it some time a go for a similar task involving in d code gen.
Mar 11
parent reply bachmeier <no spam.net> writes:
On Monday, 11 March 2024 at 14:59:52 UTC, Ferhat Kurtulmuş wrote:

 You have already mentioned mustache-d. If it compiles with the 
 recent compilers go for it. I used it some time a go for a 
 similar task involving in d code gen.
I found mustache-d easy enough and good enough for my needs. I haven't used it with a recent compiler, but it's hard to see how it would need much maintenance.
Mar 11
parent reply Andrea <ilmanzo gmail.com> writes:
On Monday, 11 March 2024 at 15:50:28 UTC, bachmeier wrote:
 I found mustache-d easy enough and good enough for my needs. I 
 haven't used it with a recent compiler, but it's hard to see 
 how it would need much maintenance.
just trying it out and kinda fits my needs; the main issues are lack of documentation and the need to explicit loop on array data structures in the code (using sub-contexts) instead of having a "foreach" loop statement in the template itself; at the end the template turns out to be cleaner but you need to write some code to feed it the proper way. On other fancier templating engines, It's handy to have a `{{%foreach item ; list}}` keyword and iterate on `{{item}}`. this is what I came up as a quick hack: https://gist.github.com/ilmanzo/3163cad4e2246f2553f1a90735e79e6b
Mar 11
parent reply Inkrementator <anon example.org> writes:
On Monday, 11 March 2024 at 16:10:24 UTC, Andrea wrote:
 just trying it out and kinda fits my needs; the main issues are 
 lack of documentation and the need to explicit loop on array 
 data structures in the code (using sub-contexts) instead of 
 having a "foreach" loop statement in the template itself; at 
 the end the template turns out to be cleaner but you need to 
 write some code to feed it the proper way.
Having used djinn, it is "mostly unmaintained" because it is feature complete. It addresses your criticisms while potentially introducing new problems. It is very simple and the documentation is complete (due to the simplicity). It is easy to get into, because it just allows you to insert D code into textfiles. So constructs like foreach are a given, no need for subcontexts.
 this is what I came up as a quick hack: 
 https://gist.github.com/ilmanzo/3163cad4e2246f2553f1a90735e79e6b
In djinn, you wouldn't have to convert your struct to a dict first to use it. Your template would look something like this tests.d.dj ``` [: foreach(test; testcase){:] // --- [= test.description ] --- unittest { [:foreach(case, test.cases){ :] // Description: [= case.description ] ... }; [: }} :] ``` Then you'd have a main.d file with: ```d void main(){ // The template can use all your variables in the current scope due to being mixed in auto jsonString = stdin.byLineCopy.array.join; auto json = parseJSON(jsonString); string slug = json["exercise"].str; auto tests = fromJSON!(TestSuite[])(json["cases"]); auto output = stdout.lockingTextWriter(); // magic variable, output range that the template will write to mixin(translate!"tests.d.dj"); } ``` The boilerplate in your main function would be reduced, but the disadvantage is that is doesn't place constraints on you like mustache. If you insert place too much logic inside your template, it might become hard to reason about.
Mar 14
parent Andrea <ilmanzo gmail.com> writes:
On Thursday, 14 March 2024 at 14:23:36 UTC, Inkrementator wrote:
 Having used djinn, it is "mostly unmaintained" because it is 
 feature complete. It addresses your criticisms while 
 potentially introducing new problems.
 It is very simple and the documentation is complete (due to the 
 simplicity). It is easy to get into, because it just allows you 
 to insert D code into textfiles. So constructs like foreach are 
 a given, no need for subcontexts.
good point; I think for my use case makes sense to use it, will give a try. Thanks!
Mar 14
prev sibling parent reply Sergey <kornburn yandex.ru> writes:
On Monday, 11 March 2024 at 14:26:01 UTC, Andrea wrote:
Opinions ?

 Many thanks
There is also diet : https://code.dlang.org/packages/diet-ng
Mar 11
parent reply Andrea <ilmanzo gmail.com> writes:
On Monday, 11 March 2024 at 15:22:39 UTC, Sergey wrote:
 On Monday, 11 March 2024 at 14:26:01 UTC, Andrea wrote:
Opinions ?

 Many thanks
There is also diet : https://code.dlang.org/packages/diet-ng
Is'nt `diet` specific for HTML / XML structured text ?
Mar 11
parent Sergey <kornburn yandex.ru> writes:
On Monday, 11 March 2024 at 15:34:11 UTC, Andrea wrote:
 There is also diet : https://code.dlang.org/packages/diet-ng
Is'nt `diet` specific for HTML / XML structured text ?
right. Just mentioned Go library also mostly for HTML generation.
Mar 11