BDuelz
BDuelz

Reputation: 3928

Creating html templates using PHP

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

Upvotes: 8

Views: 10482

Answers (4)

NawaMan
NawaMan

Reputation: 25687

One word: Organization. Separating each part of the page will allow each of them to be viewed/edited separately. This simple concept is very beneficial. For example, anyone in the team that want to handle login process can easily figure out that they have to edit login_form.phtml and they can be sure that editing login_form.phtml will less likely to unintentionally interfere with other functionalities.

As of the best practice, here is how I do it (not exactly but similar).

$Title = 'Blah Blah Blah';
$User  = 'Jon Miller';

$ThemeName = "MyGreenPage";
$Contents  = array("User", "Login_Form");

function Include($FileName) {
    if (file_exists($FileName))
        include $FileName;
}

MyGreenPage.phtml:

<html>
  <head>
    <title><?php echo $title; ?></title>
<?php
    foreach($Contents as $Content)
        Include("$Content.pcss");
?>
<?php
    foreach($Contents as $Content)
        Include("$Content.pjs");
?>
  </head>
  <body>
<?php
    foreach($Contents as $Content)
        Include("$Content.phtml");
?>
  </body>
</html>

User.pcss:

/*  Some styles needed by User */

User.pjs:

/*  Some script needed by User */

User.phtml:

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

Login_Form.pcss:

/*  Some styles needed by Login_Form */    

Login_Form.pjs:

/*  Some script needed by Login_Form */

Login_Form.phtml:

    <form>login form</form>

Let me remind you again that this is not that exactly what I do (what I do use OOP) so this may not exactly run as is and you may need to edit it.

Upvotes: 2

John Slegers
John Slegers

Reputation: 47081

The most common way to do HTML templating with PHP, is to use one of these popular templating engines :


Alternatively, you can just put placeholders in your HTML that look something like <% variablename %>. Just load your HTML, do a regex do find all placeholders and replace them with the corresponding variables.


Alternatively, you can load your HTML, parse it as a DOM document and then modify your DOM. I created the library DOM Query to be able to do this with a jQuery-like syntax, but you could use vanilla PHP or one of several other libraries as well.


Alternatively, you can do some HTML templating in frontend with JavaScript. If you want to do HTML templating both on frontend and backend, you might want to consider using Mustache, because Mustache templates can be used both in frontend (with JavaScript) and in backend (with PHP).

Upvotes: 1

Kevin
Kevin

Reputation: 13087

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).

Upvotes: 0

Tim Lytle
Tim Lytle

Reputation: 17624

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.

Upvotes: 3

Related Questions