Vibe.d diet template reuse

Sönke Ludwig via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 14 00:28:24 PST 2016


Am 03.11.2016 um 06:31 schrieb Jot:
> I would like to create some generic diet templates for different html
> functionality.
>
> Some code in the template will need to be setup/changed for it to
> function properly.
>
> How can I write code that allows for one to express generic statements
> in the template but access/modify them in another template?
>
> Is there a way to pass around a context, including functional code
> between templates?
>
> e.g., I might want to create a d function in a diet template that will
> be used in generating another template.
>
> e.g. (pseudo),
>
> block
>    -auto MyFunc(int x) { return 3*x; }
>    include test
>
> ....
>
> test.dt:
>
> block
>    -for(i; 1..MyFunc(3))
>       ...
>
>
> having such a feature allows me to generalize my templates quite a bit
> and reuse them for various html features rather than duplicating the
> majority of the code but I need a way to pass some unspecified
> functionality to a template before instantiation.

The example above should basically work - the included template is 
inserted into the outer context and can access any functions or 
variables declared there. Alternatively, you can also define a function 
in an included template, which then contains/generates the appropriate 
dynamic content:

---
block
   include test
   - auto MyFunc(int x) { return 3*x; }
   - insertFoo(MyFunc(3));
---

test.dt:
---
- function void insertFoo(int n)
   - for (i; 0 .. n)
     p foo: #{i}
---


More information about the Digitalmars-d-learn mailing list