Results 1 to 10 of 10

Thread: Template Class:::Help ME

Threaded View

  1. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Template Class:::Help ME

    I use a templating system on two of the sites I've made. One site is my home page and I actually use PHP for the templates. I do this by having a template class:
    PHP Code:
    <?php
    // template.php
    class Template
    {
        var 
    $path;

        function 
    Template($path null)
        {
            
    $this->path $path;
        }

        function 
    display()
        {
            @include(
    $this->path);
        }
    }
    ?>
    The templates themselves are just PHP files containing HTML and PHP, with the difference being that they use PHP's alternate templating like syntax for control structures such as loops. A simple template may look like this:
    PHP Code:
    <?php // template.tpl.php ?>
    <html>
      <head>
        <title><?php echo($this->title?></title>
      </head>
      <body>
        <h1>My Friends</h1>
        <?php foreach($this->friends as $friend): ?>
          <p><?php echo($friend?></p>
        <?php endforeach; ?>
      </body>
    </html>
    A simple script which uses this template looks like this.
    PHP Code:
    <?php
    // index.php

    require_once 'template.php';

    $template = new Template('template.tpl.php');

    /* assign some variables */
    $template->title 'Custom Title';
    $template->friends = Array('me''graham''mr burns''bart');

    /* now display the template */
    $template->display();
    ?>
    It is worth noting that this method does require some discipline and consistency in approach to the templates. It is ideal and very elegant for projects where there are only a small number of people, all who know the language.

    However, there are situations where it may be necessary to allow other people access to the output layer of your web site. These people are not necessarily PHP developers and allowing them to modify PHP files directly could interfere with other layers of your application. Fortunately the Smarty project addresses this problem. It is a very powerful templating system written in PHP, for PHP and uses the same syntax as you are using in the templates, in addition to supporting looping and logical control structures. The template above written for Smarty would look like this:
    Code:
    {* template.tpl *}
    <html>
      <head>
        <title>{$title}</title>
      </head>
      <body>
        <h1>My Friends</h1>
        {foreach from="$friends item="friend"}
          <p>{$friend}</p>
        {/foreach}
      </body>
    </html>
    The PHP code to display the template is also very similar. Note that the SmartyTemplate class is an inherited version of the standard smarty class, this is described here.
    PHP Code:
    <?php
    // index.php

    require_once 'template.php';

    // a special class for smarty
    $template = new SmartyTemplate;

    /* assign some variables */
    $template->assign('title',  'Custom Title');
    $template->assign('friends', Array('me''graham''mr burns''bart'));

    /* now display the template */
    $template->display('template.tpl';);
    ?>
    Smarty compiles all templates into HTML and as such it is very fast, it separates the output layer of your application completely and securely from the PHP code.

    I did once try to write my own templating language, but it is very complicated, especially when you want to start matching nested statements of varying depths. So, I abandoned the idea and used smarty for one of my sites - why reinvent the wheel?
    Last edited by visualAd; Sep 16th, 2005 at 04:30 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width