Display Tutorials
  Home arrow Display Tutorials arrow Page 6 - Writing a Template System in PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
DISPLAY TUTORIALS

Writing a Template System in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2004-01-13

    Table of Contents:
  • Writing a Template System in PHP
  • The Implementation
  • The Template File
  • The Class Skeleton
  • Constructor and Output
  • Replacing Tags
  • Including Dynamic Files
  • The Final Code
  • Tricks

  • 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


    Writing a Template System in PHP - Replacing Tags


    (Page 6 of 9 )

    Next came time to write the workhorse method of the class, the replace_tags method.

    <?php
    function replace_tags($tags = array()) {
      if (
    sizeof($tags) &gt0)
        foreach (
    $tags as $tag =&gt$data) {
          
    $data = (file_exists($data)) ? join(""file($data)) : $data;
          
    $this-&gt;page eregi_replace("{" $tag "}"$data,
                        
    $this-&gt;page);
        }
      else
        die(
    "No tags designated for replacement.");
    }
    ?>

    The method accepts an incoming array of references to replacement data keyed by place holder. A foreach statement cycles through the array.

    With each iteration of the foreach, a check is made to see if the value is a filename. If it is then the file's contents is loaded as the replacement data; if it isn't then it's assumed the data was passed as straight text. This allows the method to be a bit more flexible for the end user.

    Once it's determined what the replacement data is, the placeholders (along with it's delimiters) are swapped out with the corresponding information. While ereg_replace might be more appropriate, I chose to allow for case insensitivity again as a convenience factor.

    More Display Tutorials Articles
    More By bluephoenix


       · Talk about simple, and easy to use. :) A very well explained tutorial.
       · Thank you! I'm glad you enjoyed reading it and I hope you find it useful.It...
       · is there a simple way to print this article?
       · I have read this tutorial online and it is very good however i would like to print...
       · Adding a button which will generate a full-length, printable version of the tutorial...
       · A nicely written tutorial.While I agree with the previous comments tegarding a...
       · but, was wondering how links would be worked in? i mean, how would i open a link to...
       · Each data file would be viewed within the context of a processed template.For...
       · I just prefer to leave it up to each author whether they want to include a zip file....
       · Sometimes the code is cut up, spread throughout the article. It can be difficult to...
       · On the last part of code on &quot;The Final code&quot; page there is a...
       · Here are my personal feelings on this issue:1) I agree that some tutorials can...
       · &gt;gasp!&lt;Thank you, Pests, for your thorough reading. I certainly...
       · It's been a while since I've visited the site, so I was please to see you've finally...
       · Reading the comment:&lt;&lt;&lt;There are several templating system...
       · Other templates weren't necessarily lacking... but were too cumbersome.The...
       · To include the template, would i just include std.tpl on each page, and would this...
       · As the tutorial explains, require_once('lib/template.php') includes the template...
       · If you get this &quot;Warning: Invalid content of \{\}&quot; in...
       · Hi!Is there a reason I don't see to use 'eregi_replace' instead of...
       · Hi, I was wondering if it was possible to contact Timothy directly ...?Tim, if...
       · It doesn't seem to work if you have passed variables and then u can the class and...
       · Duncan, I sent you a message. Hope I can answer your questions!! :)-Tim
       · Are you sure? I've templated an album script and the variables work fine.
       · You're right... str_replace() is more efficient. I was probably in a...
       · I have problem with that file name. I think .zip file can help me.
       · Thanks for a great tutorial Tim, appreciate it. Easy to understand and it works like...
       · Gr8 tutorial..!! something that was i really looking for .since i had not any idea...
       · Hey theres something i need to ask you aswell tim is it possible that we cna hookup...
       · Great little script. You can use it for mailing templates too. Add a function in the...
       · That's fine. I PM'ed you my contact info; drop me a line.-Tim
       · I've learned creating template by researching on net before.... but compaire to this...
       · Is there a possibility to use blocks (iterating parts) with this template-system? If...
       · No. See...
       · An extremely simple addittion to this script allows email templates to be fed to...
       · i like this and this works :)i have seen other template classes and they never...
       · I have re-implemented this code to allow replacement of repeatable regions/blocks...
       · Hi all!Nice code but it doesn't seem to work with parameters...
       · Hello everybody,My stringth in php is very weak but learning. problem:...
       · Very nice, this really helped me alot :Dkeep up the good work
       · I am wondering if there's a vital piece of this tutorial that I'm not understanding....
       · Hi!This template-system seemed good and so I decided to take it for my...
       · Hi!This template-system seemed good and so I decided to take it for my...
       · Well eg i have main.tplsomewere in main.tpl i have {FOO}{FOO} points at...
       · Well eg i have main.tplsomewere in main.tpl i have {FOO}{FOO} points at...
       · peace n hi,If somebody can help me out in that, I'd be gratefull, as the title...
       · peace n hi,If somebody can help me out in that, I'd be gratefull, as the title...
       · 
       · hi sorry but im new to coding im a graphical designer and work with flash. now i...
       · Well done, an elegant way to handle one of the most important features of PHP. For...
       · I am getting the following error messageWarning: eregi_replace(): Invalid...
       · i am php beginer , and i see this tutorial and i hoem is easy to chage and make the...
       · Thanks so much for this. It's so simple and will be a great help to my next big...
       · Do you have a solution for blocks created? How would I go about creating a block...
       · I added css code. it works fine on the template. However, when you open the actual...
       · it seems that i can only parse a limited amount of characters with this class..for...
       · hi tim:could you detail how do i use this template?thankyou for your...
       · After allot of hunting on the net, this guide did the job nicely.After making a...
       · think there is bug if you include slash in url and you use this templateie...
       · Yes I've noticed that if u have an embeded url on and image like www.mysite.com or...
       · Sorted my bug out now, and it works a treat.It was a silly mistake I made in the...
       · Thank you for the great code!I think I have it all figured out and have all my...
       · Now I am thinking my parsing error may be coming from theactual template file...
       · Okay, I feel dumb. I just did a find/replace with the "'s and >'s. I got it...
       · This tutorial's sample code is all corrupted by being entity-ised too many times -...
       · Hi there. I've also tried to email you and request the tutorial to print it out, but...
       · Can someone explain to me how this approach is better then just using php includes...
       · It seperates logic from design
       · The code is a bit hard to understand with ≶ and > and other errors displayed...
       · You will need to convert the html entities into the less than, greater than and...
       · I think its a good tutorial. but one thin is that, there is no file naming and...
       · this is really helpful, what i needed exactly !!thank you :)
       · I have been using this template system for several years now. I have always found it...
     

    DISPLAY TUTORIALS ARTICLES

    - Smart Auto Refresh
    - Writing a Template System in PHP





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek