Display Tutorials

  Home arrow Display Tutorials arrow Page 6 - Writing a Template System in PHP

Writing a Template System in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 69

    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



    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.

    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,
    "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

    blog comments powered by Disqus


    - Smart Auto Refresh
    - Writing a Template System in PHP

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap