vibe-d / vibe.d

Official vibe.d development
MIT License
1.15k stars 284 forks source link

is there a way to ensure that a variable is always available to the diet templates #1556

Open SingingBush opened 8 years ago

SingingBush commented 8 years ago

I'm using the WebInterface and have the following struct as a session var:

struct CurrentUser {
    string username = null;
    bool authenticated = false;
    bool administrator = false;
}

available to the class as SessionVar!(CurrentUser, "user") currentUser;

I'm currently getting the values off it and passing them on to the diet templates like this:

    @method(HTTPMethod.GET)
    void index() {
        CurrentUser cUser = currentUser;
        render!("mytemplate.dt", cUser);
    }

but that's horrible and can get really messy later when there's more going on. I'd like to just do:

    @method(HTTPMethod.GET)
    void index() {
        render!("mytemplate.dt");
    }

and have currentUser available to all my diet templates by some kind of global config, so my methods can focus on the data that is specific to them. is that possible?

SingingBush commented 8 years ago

I've been looking at vibe.templ.utils as it seems that the injector stuff maybe what I'm after. I'm not quite sure how to implement it yet though.