• home
  • forum
  • my
  • kt
  • download
  • Introduction to Templating with PHP

    Author: 2009-03-06 10:23:54 From:

    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!

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

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

    [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:

    My Site - [pagetitle]
    
    [pagetitle]
    [pagecontent]

    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.

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

    A PHP script ties it all together.

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

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (34)
      Cookies and Sessions (11)
      Counters (15)
      Database Related (20)
      Date and Time (13)
      Development (19)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot