Home arrow PHP arrow Page 5 - PHPLib Templates

Dealing with 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.

  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


Now we get to dealing with blocks.

Really, it's just more of the same...

We define some variables with some stuff in them, and then we shuffle the variables around to get what we want.

Let's take a file called 'two.ihtml'...

A list of what I want for Christmas: <OL> <!-- BEGIN AccessBlock --> <LI> {item} <!-- END AccessBlock --> </OL>

Now, we have two types of buckets we want to manipulate text in: a variable, and a block.

First, we define the variable that contains the entire file...

<? $T->set_file('input_two', 'two.ihtml'); ?>

and then we want to break out the block...

<? $T->set_block('input_two', 'AccessBlock', 'ABlock'); ?>

What did we just do here?

We now have three PHPLIB template variables defined here:

1) 'AccessBlock', which contains the text '<LI> {item}'

2) 'ABlock', which is a place-holder for where AccessBlock USED to be, and is now a variable, dealt with like any other variable!

3) 'input_two', which contained the original text of file two.ihtml MINUS the block we've just removed, which has been replaced with the variable tag 'ABlock'.

When 'AccessBlock' was removed from 'input_two', the text of '<LI> {item}' was taken out of the 'input_two' variable and replaced with the 'ABlock' variable.

Now, let's assume that we have an array of stuff we want for Christmas:

$want_list=array( 0 => 'Baseball bat', 1 => 'Remote Control car', 2 => 'Wagon' );

We can make that Christmas list very easily here...

<? for ($i=0; $i<sizeof($want_list); $i++) { $T->set_var('item', $want_list[$i]); $T->Parse('ABlock', 'AccessBlock', true); } $T->pparse('Output', 'input_two'); ?>

Pretty slick, eh?

So long as you keep the template block definitions and variable names the same within the source (.ihtml) file, you can use table rows, ordered/unordered lists, BR tags, whatever suits your fancy.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: