Main
Download
Guide
FAQ
Contact
Other projects
- Spawn SQL
Wishlist/Bugs
Home
|
Note: This website has moved to mikealeonetti.com.
Chip PHP/Perl HTML Parsing Engine
Or rather the Chip Template Engine
A couple of years ago when I was working on an eCommerce like PHP application I had to invent
an easy way for developers to easy interact with all of the HTML code without having to deal
with any PHP. The answer: an HTML parsing engine. Over the years I had accustomed myself to
using my own HTML parsing engine, and by the time I was told about the Smarty template engine
I was in shock that somebody else had the same idea as I (a very small way of thinking).
So, like any other curious programmer, I tried the Smarty template engine.
But, as things don't always do, it didn't fit my needs the way I saw them. So using some of the
ideas and concepts from the Smarty template engines I re-wrote my own. This time the focus was on
giving more funcionality to the programmer and allowing for better arrangement of the template code.
The Chip HTML Parsing Engine is undoubtedly my findings. It comes in both PHP and Perl flavors.
Mike A. Leonetti
Update 4/28/2010
As you may have noticed, I haven't been maintaining this. This is mainly because I haven't been
programming much. But, as to not leave anybody high and dry if you find any bugs or you want to
request features you can always e-mail me at mikealeonetti at gmail.com.
08/28/2006: ChangeLog
More bugfixes and mod_perl workarounds on the Chip Template Engine Perl (version 0.32).
08/25/2006: ChangeLog
Small bugfixes on the Chip Template Engine Perl (version 0.31). Beta testers are still appreciated.
08/16/2006: ChangeLog
Introducing the Chip Template Engine Perl (version 0.3). Beta testers needed. Go fetch a copy.
08/15/2006: ChangeLog
New PHP version release (version 0.31) fixing the "include" tag.
08/14/2006 (later that day): ChangeLog
New PHP version release (version 0.3) implementing the "copy" tag.
08/14/2006: ChangeLog
New PHP version release (version 0.2) implementing the aliasing system and fixing file formatting bugs.
What it's designed to do
The Chip Template Engine is designed to ease the work off of the both the programmer and the
designer.On the programmer's end it allows HTML code to be loaded, parsed, concatenated, and
outputted with simple functions. While on the designer's end, it grants a simple set of easy to
recognize and remember commands to control code flow, and even perform more complex programming
tasks. What makes this so different? Well, with the Chip Template Engine instead of each HTML
page requiring its own template file, multiple HTML pages can be included all within one single
template file. That is because with the Chip Template Engine, each block of HTML code is assigned
to its own keyword (that more correctly resembles a function). Okay, so perhaps that sounds a bit
confusing. Let me elaborate.
Say you have a need for a document that has header and a footer HTML code that is shared by a few
documents.
- header.tpl
- <html><head>(heading code here)</head>
(menu code here)
- footer.tpl
- (ending code)</html>
So with these two files in place, the files themselves can be included in any other document with
the Smarty Template Engine's include system. However, you still have no choice but to create a new
.tpl file for all other HTML pages and includable HTML pages. That made me angry.
However, let's pose the same scenario with the Chip Template Engine system. Here is how it can be
handled with only one file even.
- page.gui
- <parser:new name="header">
<html><head>(heading code here)</head>
(menu code here)
</parser:new>
<parser:new name="footer">
(ending code)</html>
</parser:new>
<parser:new name="faqs">
<parser:link name="header">
(FAQ body code)
<parser:link name="footer">
</parser:new>
In this way one can embed as many pages as needed into one single file. However, one is not
limited to this method, since the link command is not limited to just templates within the
same file. It is possible to link in other templates from different files.
Coding samples
Variable Replacement
Variable replacement in the HTML code should be a snap. Let us say we needed to output a bunch
of data on my favorite chocolate and the times I like to eat it. In this example we shall focus
on the code portion as well. Note that all examples will have both a Perl and PHP coding example
when necessary.
- chocolate.gui
- <parser:new name="chocolate_display">
<b>Favorite chocolate</b>: {$type}<br>
<b>Favorite time to eat chocolate</b>: {$best_time}<br>
<b>Number of people to each chocolate with</b>: {$people_numbers}<br>
<b>What to do whilst eating chocolate</b>: {$simultaneous_action}<br>
<b>Best two friends in the whole wide world</b>: {$two_friends['one']}, {$two_friends['two']}
</parser:new>
- chocolate.php
- ... // Code containing the definition of Chip and all other initializations
// Set up the variables in Chip
$chip->variables['type'] = "Dark";
$chip->variables['best_time'] = "Around 11:30 PM";
$chip->variables['people_numbers'] = 3;
$chip->variables['simultaneous_action'] = "Doing something to keep my hands occupied so I'm not overstuffing myself";
// Arrays can also be used
$chip->variables['two_friends'] = array( "one"=>"Alex", "two"=>"Randy" );
// Output the completed template
$chip->output_template( "chocolate_display" );
... // Cleaning up code
- chocolate.pl
- ... # Code containing the definition of Chip and all other initializations
# Set up the variables in Chip
$chip->{'variables'}->{'type'} = "Dark";
$chip->{'variables'}->{'best_time'} = "Around 11:30 PM";
$chip->{'variables'}->{'people_numbers'} = 3;
$chip->{'variables'}->{'simultaneous_action'} = "Doing something to keep my hands occupied so I'm not overstuffing myself";
# Hash arrays can also be used
$chip->{'variables'}->{'two_friends'} = { "one"=>"Alex", "two"=>"Randy" };
# Output the completed template
$chip->output_template( "chocolate_display" );
... # Cleaning up code
Additionally the "set_var" function is available as of version 0.2.
- chocolate.php
- ... // Code containing the definition of Chip and all other initializations
// Set up the variables using set_var instead
$chip->set_var( "type", "Dark" );
$chip->set_var( "best_time", "Around 11:30 PM" );
$chip->set_var( "people_numbers", 3 );
$chip->set_var( "simultaneous_action", "Doing something to keep my hands occupied so I'm not overstuffing myself" );
// With arrays as well
$chip->set_var( "two_friends", array( "one"=>"Alex", "two"=>"Randy" ) );
// Outputting is the same
$chip->output_template( "chocolate_display" );
... // Cleaning up code
- chocolate.pl
- ... # Code containing the definition of Chip and all other initializations
# Now for Perl
$chip->set_var( "type", "Dark" );
$chip->set_var( "best_time", "Around 11:30 PM" );
$chip->set_var( "people_numbers", 3 );
$chip->set_var( "simultaneous_action", "Doing something to keep my hands occupied so I'm not overstuffing myself" );
# Now for some hash
$chip->set_var( "two_friends", { "one"=>"Alex", "two"=>"Randy" } );
# Output as normal
$chip->output_template( "chocolate_display" );
... # Cleaning up code
- output
- <b>Favorite chocolate</b>: Dark<br>
<b>Favorite time to eat chocolate</b>: Around 11:30 PM<br>
<b>Number of people to each chocolate with</b>: 3<br>
<b>What to do whilst eating chocolate</b>: Doing something to keep my hands occupied so I'm not overstuffing myself<br>
<b>Best two friends in the whole wide world</b>: Alex, Randy
Looping
Looping can be done one of two ways. Firstly, it can be done via the actual code by telling Chip
to parse the template again and again. It can also be done by using loop statements in the actual
Chip template code.First let's look at the code method. Let's loop a number's increasing.
- increase.gui
- <parser:new name="number_increase">
<b>New value</b>: {$number}<br>
</parser:new>
- increase.php
- ...
// Let's loop!
for( $chip->variables['number'] = 0;
$chip->variables['number']>5;
$chip->variables['number']++ )
// Parse the template and append data to the end
$chip->parse_template( "number_increase" );
// Finally, output the template
$chip->output_template( "number_increase" );
...
- increase.pl
- ...
# Perl loop time, recycling hash arrays
for( $chip->{'variables'}->{'number'} = 0;
$chip->{'variables'}->{'number'}>5;
$chip->{'variables'}->{'number'}++ )
# Parse the template and insert the data
$chip->parse_template( "number_increase" );
# Finally, output the template
$chip->output_template( "number_increase" );
...
- output
-
<b>New value</b>: 0<br>
<b>New value</b>: 1<br>
<b>New value</b>: 2<br>
<b>New value</b>: 3<br>
<b>New value</b>: 4<br>
Now let's say instead of putting the template data at the end, we wanted it at the top of the
previous template. A sort of insert type of method. Let's try again with the same template.
- increase.php
- ...
// Let's loop!
for( $chip->variables['number'] = 0;
$chip->variables['number']>5;
$chip->variables['number']++ )
// Parse the template and insert the data
$chip->parse_template( "number_increase", "i" );
// Finally, output the template
$chip->output_template( "number_increase" );
...
- increase.pl
- ...
# Perl loop time, recycling hash arrays
for( $chip->{'variables'}->{'number'} = 0;
$chip->{'variables'}->{'number'}>5;
$chip->{'variables'}->{'number'}++ )
# Parse the template and insert the data
$chip->parse_template( "number_increase", "i" );
# Finally, output the template
$chip->output_template( "number_increase" );
...
- output
-
<b>New value</b>: 4<br>
<b>New value</b>: 3<br>
<b>New value</b>: 2<br>
<b>New value</b>: 1<br>
<b>New value</b>: 0<br>
Now, this only demonstrates one way in which the Chip Template Engine can loop. Let us say we have
an array that we don't want to go through the bother of looping in the code. So, we pass it as
a regular variable to Chip and let the foreach command do all of the work.
- foreach_test.gui
- <parser:new name="output_array">
<parser:foreach array="$data_array" value="$data">
<b>Bunny name</b>: {$data}<br>
</parser:foreach>
</parser:new>
- increase.php
- ...
// Pass an array to Chip
$chip->variables['data_array'] = array( "Caesar", "Fred", "Richard", "Small Caesar" );
$chip->output_template( "output_array" );
...
- increase.pl
- ...
# Pass a regular array ref
$chip->{'variables'}->{'data_array'} = [ "Caesar", "Fred", "Richard", "Small Caesar" ];
$chip->output_template( "output_array" );
...
- output
-
<b>Bunny name</b>: Caesar<br>
<b>Bunny name</b>: Fred<br>
<b>Bunny name</b>: Richard<br>
<b>Bunny name</b>: Small Caesar<br>
|