Home arrow PHP arrow Page 6 - PHPLib Templates

Nested blocks... - PHP

PHPLIB templates can grant you an amazing ability to abstract the manipulation of data (in the database as well as in PHP) from its final format, whether that format is HTML, XML, WML, or a formatted e-mail, and some of these ways will be explored here.

TABLE OF CONTENTS:
  1. PHPLib Templates
  2. Simplify
  3. Home is where the heart is
  4. Let's get started
  5. Dealing with blocks
  6. Nested blocks...
  7. Aren't we done YET?
  8. Closing
By: Benjamin D. Smith
Rating: starstarstarstarstar / 21
February 28, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: