Display Tutorials

  Home arrow Display Tutorials arrow Page 7 - 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 - Including Dynamic Files

    (Page 7 of 9 )

    At this point the class is sufficient to template plain text and HTML files, but a greater level of functionality might be desired. For example, what if the user wanted to insert a footer section that contains dynamic data such as the date or page address? It would definitely be nice if the templating script would make allowances for other script files.

    I modified replace_tags to pass data that validates as a file name off to a private function.

    function parse($file) {
    $buffer ob_get_contents();

    replace_tags($tags = array()) {
      if (
    sizeof($tags) &gt0)
        foreach (
    $tags as $tag =&gt$data) {
    $data = (file_exists($data)) ? $this-&gt;parse($data) : $data;
    $this-&gt;page eregi_replace("{" $tag "}"$data,
    "No tags designated for replacement.");

    Parse accepts the name of a file and includes its contents (include will processes any PHP directives found within the file). Output buffering is used to store the processed data so we can return it and prevents the included file from being sent to standard output prematurely.

    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