PHP
  Home arrow PHP arrow Page 5 - 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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 / 169
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Simple PHP Templates With PatTemplate - Template Visibility


    (Page 5 of 6 )

    There comes a time when you want to hide templates from being showed. This is done by adding the visbility attribute to a template tag. The visibility attribute takes two options, show (default) and hidden. Here is an example where the "content" template is hidden:

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>An invisible template</title>
    </head>
    <body>
    <patTemplate:tmpl name="content" visibility="hidden">
    This text isn't actually visible.
    </patTemplate:tmpl>
    </body>
    </html>
    </patTemplate:tmpl>


    This feature wouldn't be too useful if you only could change it at design-time. That's where the setAttribute() function comes in handy. Let's take a look at some PHP code:

    <?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("example9.tmpl.html");

    // Make the template "content" visible
    $tmpl->setAttribute("content", "visibility", "show");

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


    Try commenting out the setAttribute line and see what happens. This feature can be useful for error messages, etc.

    Template linking
    There are a lot of other useful features included in patTemplate. One of these features is the ability to link templates. Here is an example of template linking:

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>Template linking</title>
    </head>
    <body>
    <patTemplate:link src="content" />
    </body>
    </html>
    </patTemplate:tmpl>

    <patTemplate:tmpl name="content">
    This is the content. I will be displayed within the "body"-template.<br>
    Feel free to use variables inside this template too. Like this:<br><br>
    My name is {NAME}.
    </patTemplate:tmpl>


    The link within the "body" template links to the template "content", and includes it. Pretty easy, eh? Linking templates can make things easier when you’re editing large template files.

    Here is the PHP code I used:

    <?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("example6.tmpl.html");

    // Add the variable
    $tmpl->addVar("content", "NAME", "Maria");

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


    Another option is linking an external file, like I've done below:

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>Template linking</title>
    </head>
    <body>
    <patTemplate:tmpl name="content" src="example7content.tmpl.html" />
    </body>
    </html>
    </patTemplate:tmpl>


    Let's make another file:

    This is the content. I will be displayed within the "body"-template.<br>
    Feel free to use variables inside this template too. Like this:<br><br>

    My name is {NAME}.


    The PHP code that we can use is shown below:

    <?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("example7.tmpl.html");

    // Add the variable
    $tmpl->addVar("content", "NAME", "Maria");

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


    I usually split my templates up into three different files: Header, content and footer. This way I only have to update one file if I want to modify the header (instead of changing the header part in every single template-file).

    Global variables
    So far we've only looked at patTemplate using local variables. There are two different ways to add a variable with patTemplate: through the local scope or through the global scope. Here's an example of global variables in action:

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>Variable scope</title>
    </head>
    <body>
    This is a link to the image <a href="{IMAGEFILE}">{IMAGEFILE}</a><br><br>
    <patTemplate:tmpl name="new">
    It's also applied to this template.. look: {IMAGEFILE}
    </patTemplate:tmpl>
    </body>
    </html>
    </patTemplate:tmpl>


    And here's the PHP-code:

    <?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("example8.tmpl.html");

    // Add the global variable
    $tmpl->addGlobalVar("IMAGEFILE", "dummy-image.jpg");

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


    When you use the function addGlobalVar(), you add the value to all the matching variables in the loaded template-file.

    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 6 hosted by Hostway