Complex views and passing around stuff to the view
Moderator: General Moderators
Complex views and passing around stuff to the view
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.
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
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.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Complex views and passing around stuff to the view
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: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.
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
Yes, I'm familiar with that method. It is an option, and I can see the attractiveness of it.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.
That View, could that also be called a View Helper (in pattern language), or am I mixing concepts then?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:
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());
}
}
Re: Complex views and passing around stuff to the view
Just keep things simple.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Complex views and passing around stuff to the view
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.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.
Re: Complex views and passing around stuff to the view
sure .. the question of course being, what is simple ..blueyon wrote:Just keep things simple.
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.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.
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?
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Complex views and passing around stuff to the view
This is just a guess, but couldn't the actions set this data on the view when it sets the content? ie:
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.
Code: Select all
$view->set('title', 'Page Title');
$view->set('content', $content->render());Code: Select all
$view->setTitle('Page Title');