PHP
  Home arrow PHP arrow Page 7 - Template-Based Web Development With patTemplate (part 1)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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

Template-Based Web Development With patTemplate (part 1)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2002-05-28


    Table of Contents:
  • Template-Based Web Development With patTemplate (part 1)
  • Hard Sell
  • Message In A Bottle
  • Slice And Dice
  • Music To Your Ears
  • Watching The Clock
  • A Bookworm In The Ointment
  • A Rose By Any Other Name...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Template-Based Web Development With patTemplate (part 1) - A Bookworm In The Ointment
    ( Page 7 of 8 )

    I'd like to wrap up this introductory article with a comprehensive example, which builds on what you've learned so far to demonstrate how easy it is to use patTemplate to quickly construct different types of page layouts.

    Let's suppose I wanted to generate a Web page for an item in an online book store, and let's further suppose that I wanted it to look like this:



    Here are the templates I plan to use:

    <!-- books.tmpl --> <!-- container page --> <patTemplate:tmpl name="main"> <html> <head> <basefont face="Arial"> </head> <body> <!-- header --> <img src="logo.gif" alt="Company logo"> <!-- content --> <table width="100%" cellspacing="5" cellpadding="5"> <tr> <td valign="top"> <patTemplate:link src="recommendations" /> </td> <td valign="top"> <patTemplate:link src="review" /> </td> </tr> </table> <!-- footer --> <hr> <center><font size=-2>All content copyright and proprietary <a href="http://www.melonfire.com/">Melonfire</a>, 2002. All rights reserved.</font></center> </body> </html> </patTemplate:tmpl> <!-- review section --> <patTemplate:tmpl name="review"> <h2>{TITLE}</h2> <p> <img src="{POSTER}" width="100" height="100" alt="Book jacket" align="left">{CONTENT} </patTemplate:tmpl> <!-- reco section --> <patTemplate:tmpl name="recommendations"> <font size="-1">If you liked this title, you might also like: <br> <ul> <patTemplate:link src="recommendations_list" /> </ul> </font> </patTemplate:tmpl> <!-- reco item section --> <patTemplate:tmpl name="recommendations_list"> <li><a href="story.php?id={ID}"><font size=-1>{ITEM}</font></a> <p> </patTemplate:tmpl>
    I'm going to use these four templates to generate the layout illustrated above.

    <?php /*** this entire section would come from a database ***/ // book details - title, content, poster $title = "On Ice"; $content = "A taut, well-written thriller, <i>On Ice</i> is a roller-coaster ride right from the opening paragraphs. Often surprising, never boring, David Ramus expertly guides his hero from one calamity to the next, deftly unveiling new parts of the puzzle until the pieces come together in the explosive showdown. While the pacing, especially in the novel's second half, may seem a little jagged, the first half of the novel, with its vivid characterization of prison life and snappy dialogue, is well worth a read.<p>Once more...<p>A taut, well-written thriller, <i>On Ice</i> is a roller-coaster ride right from the opening paragraphs. Often surprising, never boring, David Ramus expertly guides his hero from one calamity to the next, deftly unveiling new parts of the puzzle until the pieces come together in the explosive showdown. While the pacing, especially in the novel's second half, may seem a little jagged, the first half of the novel, with its vivid characterization of prison life and snappy dialogue, is well worth a read.<p>"; $image = "poster.gif"; // list of titles for recommendations $items = array(); $items[0] = "City Of Bones - Michael Connelly"; $items[1] = "Mortal Prey - John Sandford"; $items[2] = "Hostage - Robert Crais"; // corresponding review ids $ids = array(); $ids[0] = 23; $ids[1] = 124; $ids[2] = 65; /*** database action ends **/ // include the class include("include/patTemplate.php"); // initialize an object of the class $template = new patTemplate(); // set template location $template->setBasedir("templates"); // add templates to the template engine $template->readTemplatesFromFile("books.tmpl"); $template->AddVar("review", "TITLE", $title); $template->AddVar("review", "POSTER", $image); $template->AddVar("review", "CONTENT", $content); // iterate through array // assign values to template variables from array fields // iteratively build list for ($x=0; $x<sizeof($items); $x++) { $template->AddVar("recommendations_list", "ID", $ids[$x]); $template->AddVar("recommendations_list", "ITEM", $items[$x]); $template->parseTemplate("recommendations_list", "a"); } // parse and display the template $template->displayParsedTemplate("main"); ?>
    The first part of this script is focused solely on extracting information to display from a database - I've hard-coded the values here for demonstration purposes. Once the variables are set, the script initializes a patTemplate object and reads the four templates I plan to use into the template engine.

    Next, values are assigned to the {TITLE}, {POSTER} and {CONTENT} variables within the "review" template. Once that's done, the list of recommendations is iteratively built, using the data in the $items and $ids arrays.

    At the end of this process, the "recommendations_list" template stores a complete list of recommended books. This is then placed in the "recommendations" template, and, finally, both "recommendations" and "review" are transposed in the "main" template and printed to the browser.

    Here's what the output looks like:



     
     
    >>> More PHP Articles          >>> More By Team Melonfire, (c) Melonfire
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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