tags:

views:

127

answers:

3

Im learning a lot about how MVC frameworks work by looking around and studying existing ones. It seems that every framework I see has a layout where each method in each controller has its own template file. So there will be a login template, a logout template, register, so on and so on.

My question is, how and why would you create a template for the entire page all within one file. Lets say you wanted to show the login form on more than one page, wouldn't you have to create the login form for each template that you want it to display on? Doesn't that go against the don't repeat yourself rule (DRY)?

The way i've been doing things so far is I've been creating liitle chunks of templates and then combining them to create each page. So instead of doing something like this,

$title = 'Blah Blah Blah';
$user  = 'Jon Miller';

include 'index.phtml';

<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>
    <h3><?php echo $user; ?></h3>
    <form>login form</form>
  </body>
</html>

I've been doing this

$title = 'Blah Blah Blah';

include 'header.phtml';

$user  = 'Jon Miller';

include 'user.phtml';
include 'login_form.phtml';
include 'footer.phtml';

header.phtml
<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>

user.phtml
    <h3><?php echo $user; ?></h3>

login_form.phtml
    <form>login form</form>

footer.phtml
  </body>
</html>

As alway, I would just like to know the proper way to do it, along with how and why...It just seems to go against the DRY rule.

Thanks

+3  A: 

You should check out the concepts of 'layouts' and 'view helpers'. While I've linked to the Zend Framework version of those concepts, other MVC frameworks (and the MVC concept) should have them as well.

The basic idea is that your page 'view' - for example the login form - is included into your site 'layout' - the general template that is used throughout your site. When you request a different controller, with a different view - for example a user profile - that view is also included in the same layout.

To include something like a login form on all pages, a view helper can be used. That view helper could display the current user, or display a login form, depending on the login status. View helpers may be included in the layout, or included by the specific controller (as long as MVC framework allows some kind of named render segments).

The two step 'include' method works better than linear inclusion of parts (including header, then content, then footer - what you're doing now) because your templates do not have to split HTML tags. The Zend Guide has a good visual example of view templates in a layout.

Tim Lytle
A: 
NawaMan
A: 

For this project I am working on, all views are XSL templates. Instead of passing variables to the view, I generate all the XML in the controller and transform it with the view.

I like this structure because I can keep all my templates in one file, and arrange them anyway I want. It's also a standard template language which is very flexible and has tons of documentation. This has been working out really well so far.

A really good example of this structure is the WoW Armory website (view the source).

Kevin