RejectedSoftware Forums

Sign up

Picking theme at runtime

Hello!

In my project I need to be able to change website theme on runtime, when user changes his preferences in profile.
I would like to stick to Diet engine.

For the time being I'm using something like that:

void renderT(string filename, ARGS...)()
{
	string theme = "b2";

	alias context = s_requestContext;

	if (context.req.session && context.req.session["theme"] != "")
	{
		theme = context.req.session["theme"];
	}
	else
	{
		theme = context.req.fullURL().host != "dev.d3" ? "b2" : "orange";
	}

	final switch (theme)
	{
		case "b2": vibe.web.web.render!("b2/"~filename, ARGS)(); break;
		case "orange": vibe.web.web.render!("orange/"~filename, ARGS)(); break;
	}
}

and use it instead of render() method.
My application is based on vibe.web.web infrastructure.

The problem is that I need to keep my own copy of vibe.d in order to make srequestContext public field.
I am also not 100% sure if it is safe approach (using s
requestContext).

Are there any other ways to achieve the same goal?
Maybe you are rolling your own solution and would like to share it with me? ;)

Best regards!

Re: Picking theme at runtime

You can capture HTTPServerRequest as a function parameter in your vibe.web.web callback function and it'll detect this and send the object, so you can consult the session or cookies from it. Personally I would think themes would belong in different CSS files, so you could definitely send the variable containing the theme name to your diet template to get the corresponding CSS without having to generate a different diet template.

Re: Picking theme at runtime

On Mon, 10 Feb 2014 00:16:05 GMT, Etienne wrote:

You can capture HTTPServerRequest as a function parameter in your vibe.web.web callback function and it'll detect this and send the object, so you can consult the session or cookies from it. Personally I would think themes would belong in different CSS files, so you could definitely send the variable containing the theme name to your diet template to get the corresponding CSS without having to generate a different diet template.

I am aware of being able to capture HTTPServerRequest/HTTPServerResponse.
The thing is I don't want to pass additional arguments without a need. (Assuming I need only to render template, nothing else).
On longer run it becomes cumbersome.

void getSomething(HTTPServerRequest req, HTTPServerResponse res) { render!("something.dt")(req, res); }

vs

void getSomething() { render!("something.dt"); }

Well it isn't just matter of CSS :) Indeed that would be much easier :)
I need to change structure of HTML itself too.

Re: Picking theme at runtime

Am 10.02.2014 09:42, schrieb Damian Ziemba:

On Mon, 10 Feb 2014 00:16:05 GMT, Etienne wrote:

You can capture HTTPServerRequest as a function parameter in your vibe.web.web callback function and it'll detect this and send the object, so you can consult the session or cookies from it. Personally I would think themes would belong in different CSS files, so you could definitely send the variable containing the theme name to your diet template to get the corresponding CSS without having to generate a different diet template.

I am aware of being able to capture HTTPServerRequest/HTTPServerResponse.
The thing is I don't want to pass additional arguments without a need. (Assuming I need only to render template, nothing else).
On longer run it becomes cumbersome.

void getSomething(HTTPServerRequest req, HTTPServerResponse res) { render!("something.dt")(req, res); }

vs

void getSomething() { render!("something.dt"); }

Well it isn't just matter of CSS :) Indeed that would be much easier :)
I need to change structure of HTML itself too.

If you want to avoid passing it as an argument, just be sure that the
global variable is of type TaskLocal!HTTPServerRequest instead of just
HTTPServerRequest, so that multiple concurrent requests don't get
mixed up. If that's given, there is nothing from a safety perspective
that speaks against it.

BTW, there is already such a variable in vibe.web.web
(s_requestContext). It may not be the worst idea to somehow expose
it's contents apart from the current SessionVar support.

Re: Picking theme at runtime

On Mon, 10 Feb 2014 10:18:04 +0100, Sönke Ludwig wrote:

Am 10.02.2014 09:42, schrieb Damian Ziemba:

On Mon, 10 Feb 2014 00:16:05 GMT, Etienne wrote:

You can capture HTTPServerRequest as a function parameter in your vibe.web.web callback function and it'll detect this and send the object, so you can consult the session or cookies from it. Personally I would think themes would belong in different CSS files, so you could definitely send the variable containing the theme name to your diet template to get the corresponding CSS without having to generate a different diet template.

I am aware of being able to capture HTTPServerRequest/HTTPServerResponse.
The thing is I don't want to pass additional arguments without a need. (Assuming I need only to render template, nothing else).
On longer run it becomes cumbersome.

void getSomething(HTTPServerRequest req, HTTPServerResponse res) { render!("something.dt")(req, res); }

vs

void getSomething() { render!("something.dt"); }

Well it isn't just matter of CSS :) Indeed that would be much easier :)
I need to change structure of HTML itself too.

If you want to avoid passing it as an argument, just be sure that the
global variable is of type TaskLocal!HTTPServerRequest instead of just
HTTPServerRequest, so that multiple concurrent requests don't get
mixed up. If that's given, there is nothing from a safety perspective
that speaks against it.

BTW, there is already such a variable in vibe.web.web
(s_requestContext). It may not be the worst idea to somehow expose
it's contents apart from the current SessionVar support.

Yeah and I am using it at the moment.
But I had to manualy change it's visibility from private to public although I would prefer to stay with ~master version of vibed within my project.