Page 1 of 1
Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 11:17 am
by matthijs
If you have an average MVC app using a Front controller, Controllers and action in the controller layer and Views/templates for the view layer, how do you get "global" information inside the templates. With that global information I mean information not specifically related to the current action. Say you have a main template with a "You are logged in as {username}" or a "Please login" link in case someone is logged in or not.
One way is to pass the user session around in a registry and pass that registry around to every action. Every action again passes on the user session info to each loaded template. However, that would be repeating a lot of code. And in case you decide to change something in that main template (you don't need that user info anymore for example), you have to change each and every action in your app.
Another way would be to have static calls to the front controller or registry or user session.
I know Zend framework uses a mix of passing around stuff in a registry and having static calls to elements in the front controller.
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 11:28 am
by Eran
I know most guys here are against it, but I use static calls to an Authentication class which returns an identity object. The identity object is a simple data object based on how Zend_Auth works.
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 12:14 pm
by allspiritseve
matthijs wrote:One way is to pass the user session around in a registry and pass that registry around to every action. Every action again passes on the user session info to each loaded template. However, that would be repeating a lot of code. And in case you decide to change something in that main template (you don't need that user info anymore for example), you have to change each and every action in your app.
I'm sort of playing around with similar issues myself. The way I've been handling it is to have a View for that main template that knows how to get all of the information it needs from the registry. Thus, each page only needs the name of that view class, and everything should just work:
Code: Select all
$view = new StandardView($this->registry);
$view->set('content', $content->render());
echo $view->render();
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 12:47 pm
by matthijs
pytrin wrote:I know most guys here are against it, but I use static calls to an Authentication class which returns an identity object. The identity object is a simple data object based on how Zend_Auth works.
Yes, I'm familiar with that method. It is an option, and I can see the attractiveness of it.
allspiritseve wrote:I'm sort of playing around with similar issues myself. The way I've been handling it is to have a View for that main template that knows how to get all of the information it needs from the registry. Thus, each page only needs the name of that view class, and everything should just work:
That View, could that also be called a View Helper (in pattern language), or am I mixing concepts then?
I was thinking about this option as well. However, I wasn't sure about the implementation yet. Having to do:
Code: Select all
class somecontroller {
function someAction() {
// action specific code here ..
// create the content for this action here ..
$template = $this->load()->template('someactionspecifictemplate');
$template->set('content', $somecontent);
$content = $template->render();
// now create the "main" template
$view = new StandardView($this->registry);
$view->set('content', $content->render());
}
}
In each and every action seems kind of cumbersome/repeating too much. Suppose I want to switch the StandardView to SomeOtherView I have to go back to all my actions and change it there.
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 2:13 pm
by blueyon
Just keep things simple.
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 2:17 pm
by allspiritseve
matthijs wrote:I was thinking about this option as well. However, I wasn't sure about the implementation yet. Having to do... in each and every action seems kind of cumbersome/repeating too much. Suppose I want to switch the StandardView to SomeOtherView I have to go back to all my actions and change it there.
You have options. If you have a base controller, you could instantiate StandardView by default unless another view is specified. Somewhere along the spectrum of instantiating everything yourself and having everything automated, there should be a happy medium.
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 2:51 pm
by matthijs
blueyon wrote:Just keep things simple.
sure .. the question of course being, what is simple ..
allspiritseve wrote:You have options. If you have a base controller, you could instantiate StandardView by default unless another view is specified. Somewhere along the spectrum of instantiating everything yourself and having everything automated, there should be a happy medium.
Yeah, somehow I need to find a way to pass around objects to the view/templates, without having to pass them through other layers explicitly.
I'm just not sure how, but my feeling is just that there should be a single View object which is responsible for the main view handling. gathering data, gathering the right templates/partials, etc. That single view object could then make a static call to the Authentication object to get the user data, or it can be passed a registry with the user object. But at the same time, each action should be able to inject it's own data, create it's own template.
Another problem related to this is how individual actions, which fill up the content area for example, can pass information to the wrapper/main template. Say you have a main template with a header, menu and footer. That template needs the right title for each individual page. The menu needs to know which page is "active". Etc. Now if you instantiate that main view in your front controller or in a base controller, how are the actions able to inject the page/action specific info to that main template?
Re: Complex views and passing around stuff to the view
Posted: Sun Jul 12, 2009 3:57 pm
by allspiritseve
This is just a guess, but couldn't the actions set this data on the view when it sets the content? ie:
Code: Select all
$view->set('title', 'Page Title');
$view->set('content', $content->render());
Though, maybe something more explicit would be better:
So you could have standard titles for, say, a module and then each individual action could override that title if needed.