Thread overview
Vibe.d diet template reuse
Nov 03, 2016
Jot
Nov 14, 2016
Sönke Ludwig
Nov 14, 2016
Jot
November 03, 2016
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.



November 14, 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}
---
November 14, 2016
On Monday, 14 November 2016 at 08:28:24 UTC, Sönke Ludwig wrote:
> Am 03.11.2016 um 06:31 schrieb Jot:
>> [...]
>
> 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}
> ---

Thanks.

Can we include d files directly? or do we need to do this?(does .dt files import the global D context?)