PHP
  Home arrow PHP arrow Page 7 - Template-Based Web Development With pa...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      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

    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

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway