Recommendation about templating engine library

Inkrementator anon at example.org
Thu Mar 14 14:23:36 UTC 2024


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.


More information about the Digitalmars-d-learn mailing list