PHP
  Home arrow PHP arrow Page 3 - Web Development With PHP FastTemplate
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Web Development With PHP FastTemplate
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2001-09-05

    Table of Contents:
  • Web Development With PHP FastTemplate
  • Who Am I?
  • Proofing The Pudding
  • You've Got Mail
  • Repeat Customers
  • Flavour Of The Month
  • A Strict() Master
  • Musical Chairs
  • 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


    Web Development With PHP FastTemplate - Proofing The Pudding


    (Page 3 of 9 )

    Next, we need to tell FastTemplate about the template, assign values to the variables, and put the two together. Here's the script:

    <? // mypage.php - generate output using FastTemplate // include class file include("class.FastTemplate.php3"); // instantiate new object $obj = new FastTemplate("."); // assign names for template files // "index" now references the template "./mypage.tpl" $obj->define(array("index" => "mypage.tpl")); // assign values to FT variables within the template // this may also be set up as an associative array of key-value pairs $obj->assign("FNAME", "John"); $obj->assign("LNAME", "Doe"); $obj->assign("AGE", "36"); $obj->assign("TEL", "(12)-34-567 8912"); $obj->assign("EMAIL_ADDRESS", "jdoe@anonymous.com"); // parse template "index" and store in handler "result" $obj->parse(result, "index"); // print contents of handler "result" $obj->FastPrint(result); ?>
    A quick explanation is in order here.

    1. The first step when using the FastTemplate class is to create a new FastTemplate object, and tell it the location of the template files.

    <? include("class.FastTemplate.php3"); $obj = new FastTemplate("."); ?>
    In this case, there's only one template and it's in the current directory.

    2. Next, the define() method is used to assign names to the templates you plan to use through the script - these names are defined in an associative array, and will be used throughout the remainder of the script to refer to the respective template files. In this case, I've used the name "index" to refer to the template "mypage.tpl".

    <? $obj->define(array("index" => "mypage.tpl")); ?>
    3. Once FastTemplate knows which templates to use, and has names by which it can refer to them, the next step is to assign values to the variables in the templates. As you can see from "mypage.tpl" above, it contains five variables - the assign() method is used to assign values to these variables

    <? $obj->assign("FNAME", "John"); $obj->assign("LNAME", "Doe"); $obj->assign("AGE", "36"); $obj->assign("TEL", "(12)-34-567 8912"); $obj->assign("EMAIL_ADDRESS", "jdoe@anonymous.com"); ?>
    Variables may also be assign()ed values via an associative array containing key-value pairs - you'll see that technique in the next example.

    4. With the variables assigned values, it's time to put the two together. This is accomplished via FastTemplate's parse() method, which is easily the most important method in the object collection.

    The parse() method accepts two arguments - a variable name, and a template name. In this example, the variable is called RESULT, and the template is named "index" (which references the template "mypage.tpl").

    <? $obj->parse(result, "index"); ?>
    In English, the line of code above means "read the template referenced by the name "index", assign values to the variables found within it, and then assign the result, complete with substituted values, to the variable "result"." Whew!

    5. At this point, the variable "result" contains the final page output - all that remains is to print it to the browser.

    <? $obj->FastPrint(result); ?>
    The FastPrint() function prints the result of the last parse() function call - or you can specify the name of the variable to be printed, as above.

    Here's what it looks like:



    Since the HTML interface is in a separate template file, it's easy for designers with no programming experience to radically alter the interface without affecting the program code...so long as they remember to replace the placeholders when they're done. As an illustration, here's a completely reworked version of "mypage.tpl"

    <!-- begin: mypage.tpl --> <html> <head> </head> <body> <table border="1" cellpadding="5"> <tr> <td bgcolor=black><i><font color=white>First name</font></i></td> <td bgcolor=black><i><font color=white>Last name</font></i></td> <td bgcolor=black><i><font color=white>Tel</font></i></td> <td bgcolor=black><i><font color=white>Email address</font></i></td> <td bgcolor=black><i><font color=white>Age</font></i></td> </tr> <tr> <td>{FNAME}</td> <td>{LNAME}</td> <td>{TEL}</td> <td><a href="mailto:{EMAIL_ADDRESS}">{EMAIL_ADDRESS}</a></td> <td>{AGE}</td> </tr> </table> </body> </html> <!-- end: mypage.tpl -->
    which looks like this:



    By using FastTemplate, this interface modification was accomplished solely by modifying the template file, leaving the scripting routines untouched.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT