PHP
  Home arrow PHP arrow Page 3 - Simple PHP Templates With PatTemplate
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Simple PHP Templates With PatTemplate
By: Havard Lindset
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 173
    2002-06-26

    Table of Contents:
  • Simple PHP Templates With PatTemplate
  • An Overview Of PatTemplate
  • How to Process Template In PHP
  • Different Template Types
  • Template Visibility
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Simple PHP Templates With PatTemplate - How to Process Template In PHP


    (Page 3 of 6 )

    Let's just jump right in and take a look at another example of using patTemplate:

    <?PHP
    // Include the patTemplate-file
    include("includes/patTemplate.php");

    // Initialize the patTemplate-class, and create an object
    $tmpl = new patTemplate();

    // Set which directory contains the template-files.
    $tmpl->setBasedir("templates");

    // Set which template-file to read.
    $tmpl->readTemplatesFromFile("example1.tmpl.html");

    // Add variables to the template
    $tmpl->addVar("article", "HEADLINE", "This is the headline");
    $tmpl->addVar("article", "CONTENT", "And this is the content...");

    // Parse and display template
    $tmpl->displayParsedTemplate("article");
    ?>


    As you can see from the example above, the way that we use patTemplate is fairly logical. Let's run through each part of the code shown above:

    // Include the patTemplate-file
    include("patTemplate.php");


    This step is pretty self-explanatory. All this code does is include the patTemplate file.

    // Initialize the patTemplate-class, and create an object
    $tmpl = new patTemplate();


    This code initializes the patTemplate class and create an object instance from it.

    // Set which directory contains the template-files.
    $tmpl->setBasedir("templates");


    The setBasedir() function tells patTemplate where it can find the files containing the templates.

    // Set which template-file to read.
    $tmpl->readTemplatesFromFile("example1.tmpl.html");


    The readTemplatesFromFile() function tells patTemplate which file it can find it's templates in.

    // Add variables to the template
    $tmpl->addVar("article", "HEADLINE", "This is the headline");
    $tmpl->addVar("article", "CONTENT", "And this is the content...");


    These two lines replace the placeholder variables with actual content.
    The addVar() function takes three parameters:

    addVar(template name, variable name and variable value)

    // Parse and display template
    $tmpl->displayParsedTemplate("article");


    This function parses and displays the template. You should now have a basic understanding on how patTemplate works. Let's go on to something more complicated.

    A template loop
    Another interesting aspect of patTemplate is its ability to loop a template. For example, this could be used with a page where we iterate through a mysql-result.

    Let's see an example:

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>Entering a loop</title>
    </head>
    <body>
    <table>
    <patTemplate:tmpl name="namelist">
    <tr>
    <td>{NAME}</td>
    </tr>
    </patTemplate:tmpl>
    </table>
    </body>
    </html>
    </patTemplate:tmpl>


    You've already been given an explanation of how the template files work, so I won't go there again. You should save this template-file as "example2.tmpl.html".

    Let's take a look at the PHP code, which is really the interesting part:

    <?PHP
    // Include the patTemplate-file
    include("includes/patTemplate.php");

    // Initialize the patTemplate-class, and create an object
    $tmpl = new patTemplate();

    // Set which directory contains the template-files.
    $tmpl->setBasedir("templates");

    // Set which template-file to read.
    $tmpl->readTemplatesFromFile("example2.tmpl.html");

    // Create an array to iterate through
    $names = array("Paul", "John", "Roger");

    // Iterate through the array
    foreach ($names as $name) {
    // Replace placeholder var
    $tmpl->addVar("namelist", "NAME", $name);
    $tmpl->parseTemplate("namelist", "a");
    }

    // Parse and display template
    $tmpl->displayParsedTemplate("body");
    ?>


    There are a couple of new code segments in the example cove above, so let's take a look at them and what they do before moving on:

    // Create an array to iterate through
    $names = array("Paul", "John", "Roger");


    We’ve just used PHP's array() function to create a new array called names so that we have something to iterate through.

    // Iterate through the array
    foreach ($names as $name) {
    // Replace placeholder var
    $tmpl->addVar("namelist", "NAME", $name);
    $tmpl->parseTemplate("namelist", "a");
    }


    This is where the real action is going on. There's nothing special about the addVar() function. The thing to note is the parseTemplate() function. The last parameter is "a", which stands for "append". This means that instead of just parsing the template all over and rewriting what's been parsed before, we actually append after what's already been parsed.

    // Parse and display template
    $tmpl->displayParsedTemplate("body");


    The template "namelist" is a child of the "body" template because it's inside of it. When we call the displayParsedTemplate() function on the parent, all child-templates within it are displayed too.

    More PHP Articles
    More By Havard Lindset


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT