Creating Html Templates Using Php
Solution 1:
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.
Solution 2:
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");
functionInclude($FileName) {
if (file_exists($FileName))
include$FileName;
}
MyGreenPage.phtml
:
<html><head><title><?phpecho$title; ?></title><?phpforeach($Contentsas$Content)
Include("$Content.pcss");
?><?phpforeach($Contentsas$Content)
Include("$Content.pjs");
?></head><body><?phpforeach($Contentsas$Content)
Include("$Content.phtml");
?></body></html>
User.pcss
:
/* Some styles needed by User */
User.pjs
:
/* Some script needed by User */
User.phtml
:
<h3><?phpecho$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.
Solution 3:
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).
Solution 4:
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).
Post a Comment for "Creating Html Templates Using Php"