Free PHP scripts and resources since 2006
It is currently Thu Aug 28, 2008 10:45 pm




Post new topic Reply to topic  [ 1 post ] 
Introduction to Templating in PHP 
Author Message
PHP-ing since 2006
User avatar

Joined: Sun Mar 09, 2008 12:14 pm
Posts: 124
Location: /usr/local/apache/htdocs/phpscriptcoder.php
Post Introduction to Templating in PHP
Introduction to Templating with PHP

Templating is important to nearly every website now. It allows you to use the same
design on all your pages, while having different content. There are many ways of
doing it; this article will focus on the most common.

Method 1: File Inclusion

Read the paragraph below before using this code!

Code:
[header code]
<?php
include($_GET['file']);
?>
[footer code]


This is the most simple templating system. Pages are accessed by going to the
URL http://domain.com/index.php?file=aboutus.php, etc. However, you should
not use this code. Why? People could use a URL such as
http://domain.com/index.php?file=/etc/passwd or even
http://domain.com/index.php?file=http:/ ... ngtool.php, and
include any file they want from your server or elsewhere.

One way to prevent hacking with this method is to create a file list, and
prevent files other than those in the list from being accessed.

Code:
[header code]
<?php
$allowed = array('aboutus.php', 'products.php', 'home.php', 'contact.php');
if(in_array($_GET['file'], $allowed))
   include($_GET['file']);
else
   die('You are not allowed to access that file!')
?>
[footer code]


Method 2: Single File

In this method, all the pages are stored in one PHP file.

Code:
[header code]
<?php
$pages = array(
   'aboutus' => '
Page contents 1
',
   'products' => '
Page contents 2
',
   'home' => '
Page contents 3
',
   'contact' => '
Page contents 4
'
);
if(in_array($_GET['page'], $pages))
   echo $pages[$_GET['page']];
else
   echo 'The page you tried to access does not exist.';
?>
[footer code]


This can be easier to edit, because you only need to edit one file, but you also
have to remember to escape your quotes, e.g. don\'t, I\'ve, etc. as the pages
are stored as PHP strings.

Method 3: Content Management System

A content management system is a pre-made PHP script that makes it easy to setup
a website. You can usually download more templates, or make your own, although
it is usually more difficult than if you were making a template system from
scratch. These script also contain many features, called modules or plugins,
that allow you to add new stuff to your site--forum, poll, blog, directory,
store, etc. and you can also download more of these.

Method 4: Template File


Template files contain all the necessary information for a page's structure and layout,
and placeholders for the content. They need a method of storing the page data as
well. Here is an example of a template file:

Code:
<html>
<head>
<title>My Site - [pagetitle]</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="pagetitle">[pagetitle]</div>
[pagecontent]
</body>
</html>


In this example, the data files are stored in separate files, similar to method 1. However,
the files contain a PHP array rather than the page contents.

Code:
<?php
$page = array(
        'title' => 'About Us',
        'content' => 'This is our about us page!'
);
?>


A PHP script ties it all together.

Code:
<?php
$allowed = array('aboutus', 'products', 'home', 'contact');
if(in_array($_GET['file'], $allowed))
   include($_GET['file'].'.php');
else
   die('You are not allowed to access that file!')
$template = file_get_contents('template.html');
$replace = array('[pagetitle]', '[pagecontent]');
$replacements = array($page['title'], $page['content']);
$template = str_replace($replace, $replacements, $template)
echo $template;
?>


This method is the most flexible, as you can easily add new template variables (the text
in brackets in the above example). There are also PHP templating libraries available,
however these are often hard to use, and you can easily make your own.

_________________
Image
Click here for pie
My new blog!

My AIM is phpscriptcoder if you need instant script support (or want to chat), I'm on ~4:30-9:00 pm ET M-F, random times on weekends.


Sun Jun 08, 2008 9:18 am
Profile WWW
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © phpBB Group.
Based on a design by Vjacheslav Trushkin for Free Forums/DivisionCore.
[
SEO MOD © 2007 StarTrekGuide ]
';