PHP
  Home arrow PHP arrow Page 6 - PHPLib Templates
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

PHPLib Templates
By: Benjamin D. Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2001-02-28


    Table of Contents:
  • PHPLib Templates
  • Simplify
  • Home is where the heart is
  • Let's get started
  • Dealing with blocks
  • Nested blocks...
  • Aren't we done YET?
  • Closing

  • 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


    PHPLib Templates - Nested blocks...
    ( Page 6 of 8 )

    It's not hard to nest blocks inside other blocks. Using this methodology, you can have a single template file that contains any number of logical template 'pages'. You just have to remember to start from the innermost loop first.

    Take some HTML source:

    three.ihtml

    Contents of this book: <UL> <!-- BEGIN ChapterBlock --> <LI> Chapter {chapter} <OL> <!-- BEGIN PageBlock --> <LI> Page {page} <!-- END PageBlock --> </OL> <!-- END ChapterBlock --> </UL>

    We start by defining the file

    <? $T->set_file('input_three', 'three.ihtml'); ?>

    And then we need to break out the blocks, starting with the innermost block.

    <? $T->set_block('input_three', 'PageBlock', 'PBlock'); $T->set_block('input_three', 'ChapterBlock', 'CBlock'); ?>

    It is VERY VERY IMPORTANT that you always start by setting the innermost block, and work out from there! Otherwise, you've removed the block in question, and the next block assignment won't work.

    Let's say we have some arrays:

    <? $chapters=array( 0 => 'Beginning', 1 => 'End' ); $pages[0]=array( 0 => 'Getting Started', 1 => 'Setting it up', 2 => 'Positioning' ); $pages[1]=array( 0 => 'Wrapping it up', 1 => 'Clean up' ); ?>

    And we might handle these arrays like this:

    <? $T->set_file('input_three', 'three.ihtml'); $T->set_block('PageBlock', 'PBlock'); $T->set_block('ChapterBlock', 'CBlock'); for ($i=0; $i<sizeof($chapters); $i++) { $T->set_var('PBlock', ''); for ($j=0; $j<sizeof($pages[$i]); $j++) { $T->set_var('page', $pages[$i][$j]); $T->parse('PBlock', 'PageBlock', true); } $T->set_var('chapter', $chapters[$i]); $T->parse('CBlock', 'ChapterBlock', true); } $T->pparse('Output', 'input_three'); ?>

    Notice that just before we parse the chapters (the '$j' loop) that we set the 'PBlock' variable to ''. That's because we use the template block more than once, and if we don't clean the slate, the values from the previous loop will remain.

    To see this, just rem out this line and try the code again:

    $T->set_var('PBlock', '');




     
     
    >>> More PHP Articles          >>> More By Benjamin D. Smith
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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