Chip Template Engine
From Wikipedia, the free encyclopedia
Chip Template Engine | |
Developer: | Mike A. Leonetti |
---|---|
Latest release: | 0.31 / 16th August 2006 |
Use: | Template engine |
License: | LGPL |
Website: | code.divineaspirations.net/chip |
Chip is a template engine written for use with both PHP and Perl. It is designed to provide both the programmer with a flexible toolset for quick HTML parsing and output, and the designer with a simple parsing control syntax. Instead of completely separating the business logic end of the code from the HTML portion, Chip allows the design end to utalize a tag-like command set to control HTML code output.
With HTML code control tags that more resemble real HTML, Chip "GUI" files can be syntax highlighted with the same parameters as HTML.
GUI files, or rather the files containing the HTML, contain more than one template. Templates are created an included in a "new" tag, and can be linked or embedded in each other to create dependencies. In this way a single GUI file can be used either for an entire section (i.e. all pages associated with a shopping cart) or just a single page (i.e. a homepage). Each template has its own separate cache, to allow for particular code flow.
Looping in Chip can be managed as the programmer sees fit. Since the HTML code is split up into separate templates and each template cached, it is possible to parse the same template over and over again with different variables to produce a list-like output.
The second option is to use the built in foreach tag and to set only one GUI variable as an array. The rest will be handled by the GUI code.
Contents |
[edit] How it works
Chip uses Perl regular expressions to parse the GUI template creating another file containing the parsed PHP/Perl output that is then included into the currently running script. However, since a single GUI file needs to be parsed once, if the related GUI file is unmodified its PHP/Perl result will be recycled. This allows for sometimes large speed-ups.
After the file is included, actual HTML code is not parsed, nor are any of the variables replaced until parse_template(...) or output_template(...) are called.
In essence, each template is in effect like a C function or a Perl sub-routine. None of the code contained in the function or sub-routine is executed until the function is called. Templates behave in the same manner.
[edit] Coding examples
main.gui
<parser:new name="header"> <html><head><title>{$title}</head><body> <table width="100%"><tr><td> <parser:foreach array="$menu" value="$menu_item"> <a href="{$menu_item['link']}">{$menu_item['text']}</a><br> </parser:foreach> </td><td> </parser:new> <parser:new name="footer"> </td></tr></table></body></html> </parser:new>
cart.gui
<parser:new name="show_cart"> <parser:link name="header" src="main.gui"> <table><tr><td>Name</td><td>Quantity</td><td>Total Price</td></tr> <parser:new name="cart_item"> <tr> <td>{$item['name']}</td> <td>{$item['qty']}</td> <td>{$item['price']}</td> </tr> </parser:new> </table> <parser:link name="footer"> </parser:new>
Note in the example above main.gui has two templates in it.
Some set-up and looping examples. First the foreach loop method. Let's start with PHP also.
require_once( "chip_gui.php" ); $gui = new Chip; $gui->gui_directory = "gui/"; $gui->php_directory = "php/"; // .gui is optional $gui->parse_file( "main" ); $menu = array( array( "link"=>"http://www.page.com/main.php", "text"=>"Main" ), array( "link"=>"http://www.page.com/cart.php", "text"=>"Cart" ) ); $gui->variables['menu'] = $menu;
Now that the main template variables are set up, the cart gets parsed next using the second loop option that is not foreach.
$gui->parse_file( "cart" ); // Get the cart from the DB $query_result = mysql_query( "SELECT name, qty, price FROM carts WHERE customer_id={$customer_id}" ); while( $row = mysql_fetch_assoc( $query_result ) ) { // Set the price to the total price $row['price'] = $row['price'] * $row['qty']; // Let the $gui->variables['item'] array reflect $row $gui->variables['item'] = $row; // Parse the template appending the newly parsed data to the end of the cache $gui->parse_template( "cart_item" ); } mysql_free_result( $query_result ); // Show the template, the rest of the linked templates will follow $gui->output_template( "show_cart" );
The same GUI file can be parsed with Perl using a little bit of perl code.
require "chip_gui.pm"; my $gui = new Chip; $gui->{'gui_directory'} = "gui/"; $gui->{'pl_directory'} = "pl/"; # .gui is optional $gui->parse_file( "main" ); my $menu = { { "link"=>"http://www.page.com/main.php", "text"=>"Main" }, { "link"=>"http://www.page.com/cart.php", "text"=>"Cart" } }; $gui->{'variables'}->{'menu'} = $menu;
Onto the cart processing file.
$gui->parse_file( "cart" ); # Get the cart from the DB using DBI my $query_result = $mysql_dbi->prepare( "SELECT name, qty, price FROM carts WHERE customer_id={$customer_id}" ); while( my $row = $query_result->fetchrow_hashref() ) { # Set the price to the total price $row->{'price'} = $row->{'price'} * $row->{'qty'}; # Let the $gui->variables['item'] array reflect $row $gui->{'variables'}->{'item'} = $row; # Parse the template appending the newly parsed data to the end of the cache $gui->parse_template( "cart_item" ); } // Show the template, the rest of the linked templates will follow $gui->output_template( "show_cart" );
[edit] See also
- Smarty, the more popular template engine for PHP
- Category:Template engines