RejectedSoftware Forums

Sign up

example of running code in template

Hi, total noober here.

in my app.d i have added this function:

int hello(){

return 3;

}

//end
...how can i call this function in my index.dt? I tried:

block body

h1 Example page - Home
p hey #{hello()}


did i miss the example in your documentation on the website? i
got some simple addition to work, but i can't call a function.

thank you

Re: example of running code in template

On Thu, 31 May 2012 19:27:07 +0200, Richard Kubina rjkubina@gmail.com
wrote:

Hi, total noober here.

in my app.d i have added this function:

int hello(){

return 3;

}

//end
..how can i call this function in my index.dt? I tried:

block body

h1 Example page - Home
p hey #{hello()}


did i miss the example in your documentation on the website? i got some
simple addition to work, but i can't call a function.

thank you

If I'm not mistaken (noob here too), #{variable} is variables you pass to
the template from your code, or variables you create in the diet template.
You can call functions be prepending an -

block body

-auto msg = hello();
p #{msg}

it might also work with

-auto msg = hello();
p= msg

Or even (but I dont think so):

p= hello();

You might need to import your file first, but that is as easy as:

- import yourmodule;

Re: example of running code in template

Am 31.05.2012 19:27, schrieb Richard Kubina:

Hi, total noober here.

in my app.d i have added this function:

int hello(){
return 3;
}

//end
..how can i call this function in my index.dt? I tried:

block body
h1 Example page - Home
p hey #{hello()}

did i miss the example in your documentation on the website? i got some
simple addition to work, but i can't call a function.

thank you

You need to make the function visible to the template first. There are
three possibilities for this:

  1. inside the template, import the module that defines the function:

    block body

    - import mymodule;
    h1 Example page - Home
    p hey #{hello()}
    
    
  2. Pass the function as a template argument to res.render() - note that
    res.render() has some issues in DMD 2.059 and I wouldn't recommend to
    use it yet:

    res.render!("home.dt", hello);

  3. Do the equivalent with res.renderCompat():

    res.renderCompat!("home.dt", "hello")(Variant(&hello));

I've added a ticket for the website to remember adding an example to the
documentation.

Sönke

Re: example of running code in template

Am 31.05.2012 20:38, schrieb simendsjo:

On Thu, 31 May 2012 19:27:07 +0200, Richard Kubina rjkubina@gmail.com
wrote:

Hi, total noober here.

in my app.d i have added this function:

int hello(){
return 3;
}

//end
..how can i call this function in my index.dt? I tried:

block body
h1 Example page - Home
p hey #{hello()}

did i miss the example in your documentation on the website? i got
some simple addition to work, but i can't call a function.

thank you

If I'm not mistaken (noob here too), #{variable} is variables you pass
to the template from your code, or variables you create in the diet
template.
You can call functions be prepending an -

block body
-auto msg = hello();
p #{msg}

it might also work with
-auto msg = hello();
p= msg

Or even (but I dont think so):
p= hello();

All three methods should work, except that "p= ..." expects an
expression, so it's just "p= hello()".

You might need to import your file first, but that is as easy as:

  • import yourmodule;

Thanks to D's function local imports! Nice little feature.

Btw. a reference for the template syntax is on my TODO list for the next
days.