www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vibe.d diet template reuse

reply Jot <Jot datacentralave.com> writes:
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.
Nov 02 2016
parent reply =?UTF-8?Q?S=c3=b6nke_Ludwig?= <sludwig+d outerproduct.org> writes:
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) ---
Nov 14 2016
parent Jot <Jot datacentralave.com> writes:
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) ---
Thanks. Can we include d files directly? or do we need to do this?(does .dt files import the global D context?)
Nov 14 2016