RejectedSoftware Forums

Sign up

Pass variable to layout.dt

I develop a web application and I need help. How do I pass variable layout.dt file or how can I show version number at layout.dt

Re: Pass variable to layout.dt

On Wed, 23 Mar 2016 11:58:38 GMT, can wrote:

I develop a web application and I need help. How do I pass variable layout.dt file or how can I show version number at layout.dt

Anything that you pass to render!() will be available in the rendered template, as well as in any template that it includes and the one it derives from. So the following should work:

layout.dt:

html
  body
    p The version is #{ver}
    block main

page.dt:

extends layout

block main
  p These are the page contents.

app.d:

void getPage(HTTPServerRequest req, HTTPServerResponse res)
{
  string ver = "1.0.0";
  res.render!("page.dt", ver);
}

Re: Pass variable to layout.dt

Hi Sönke,

Thanks for reply. This works, but I need to send each rendering method. This is not good. What can I do?

app.d:

import vibe.d;

final class Service
{
	void get()
	{
	  string ver = "1.0.0";
	  render!("page.dt", ver);
	}

	void getTest()
	{
	  render!("test.dt"); --> Error: undefined identifier 'ver' 
	}
}

shared static this()
{
	auto router = new URLRouter;
	router.registerWebInterface(new Service());

	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	settings.bindAddresses = ["::1", "127.0.0.1"];
	listenHTTP(settings, router);

	logInfo("Please open http://127.0.0.1:8080/ in your browser.");
}



On Fri, 25 Mar 2016 11:40:35 GMT, Sönke Ludwig wrote:
> On Wed, 23 Mar 2016 11:58:38 GMT, can wrote:
>> I develop a web application and I need help. How do I pass variable layout.dt file or how can I show version number at layout.dt
> 
> Anything that you pass to `render!()` will be available in the rendered template, as well as in any template that it includes and the one it derives from. So the following should work:
> 
> layout.dt:

html
body

p The version is #{ver}
block main

page.dt:

extends layout

block main
p These are the page contents.


app.d:

void getPage(HTTPServerRequest req, HTTPServerResponse res)
{
string ver = "1.0.0";
res.render!("page.dt", ver);
}

Re: Pass variable to layout.dt

Am 30.03.2016 um 09:28 schrieb can:

Hi Sönke,

Thanks for reply. This works, but I need to send each rendering method. This is not good. What can I do?

What I usually do is to use some form of inheritance:

class ViewData {
     string ver;
}

class FooViewData : ViewData {
     int foo;
}

class BarViewData : ViewData {
     int bar;
}

class Service {
     void getFoo()
     {
         scope info = new FooViewData;
         render!("foo.dt", info);
     }

     void getBar()
     {
         scope info = new BarViewData;
         render!("bar.dt", info);
     }
}

That way you'll still have to pass something for every render(), but
the general arguments are decoupled from the page specific ones.