|
NAMEHTML::Chunks - A simple nested template engine for HTML, XML and XHTMLVERSION1.53DESCRIPTIONThis class implements a simple text-based template engine, originally intented to allow web applications to completely separate layout HTML from programming logic. However, the engine is flexible enough to be applied to other text-based situations where templates are helpful, such as generating email messages, XML data files, etc.SYNOPSISmy $engine = new HTML::Chunks(@chunkFiles); $engine->readChunkFile('morechunks.html'); $engine->addChunk($smallChunk, \$hugeChunk); $engine->addNamedChunk('myChunk', $chunk); $engine->output('myChunk', { firstName => 'Homer', lastName => 'Simpson', meals => \&outputMeals }, @extraData); my @names = $engine->getChunkNames(); my $chunk = $engine->getChunk('myChunk'); my $oldValue = $engine->setCrush(0); CHUNK SYNTAXThis template engine is based upon "chunks", which are merely named chunks of textual information such as HTML. Each chunk may be individually addressed by an application to produce output. A chunk definition may also contain data elements which will be replaced with dynamic data at runtime. A simple chunk definition looks like:<!-- BEGIN meal --> <tr> <td>##date##</td> <td>##food##</td> </tr> <!-- END --> This defines a chunk named meal. This chunk contains two data elements named date and food. These will both be replaced with real data by the application at runtime. The leading and trailing ## characters simply identify them as data elements and are not part of the actual names. Chunk definitions can even be embedded within one another. It's possible (and recommended!) to construct a definition file as a full HTML file that you can preview in a web browser. Embedding one chunk definition within another does not imply any association or positional placement between the two chunks. Things would turn out the same if you put the definitions in a straight list, one after another. Embedding is just a cool formatting convenience that you can choose to take advantage of -- or not. You may optionally surround a data element with HTML comment characters so it won't show up when previewing a chunk file in a browser. For this to work, the data element must be the only thing in the comment, such as: <!-- ##data## --> The entire comment will be replaced with the data value at run time, so the resulting data will NOT be within a comment. See the EXAMPLES section below for a sample. ROUTINES
EXAMPLESComplete separation of code and layout is fairly new to most people, and the chunky way of life does require a little shift in your thinking. However, once you dig it, this can be a very powerful and productive way to develop. A more complete example might help you get there.Consider the following chunk definition: <!-- begin mealPage --> <html> <head> <title>Meal List</title> </head> <body> <h1>Meal List for ##firstName##</h1> <table border="1"> <tr> <th>Date/Time</th> <th>Food</th> </tr> <!-- ##meals## --> <!-- BEGIN meal --> <tr> <td>##date##</td> <td>##food##</td> </tr> <!-- END meal --> </table> </body> </html> <!-- end MealPage --> Let's say this were in a file called meals.html. Because we embedded some chunk definitions within others, you could actually view it in a web browser and get a preview of things to come. Again, embedding one definition within another means nothing. It is simply a formatting convenience to let you construct chunk definition files that are also valid HTML files. Now, a fairly small (but commented) script to do something with it: use HTML::Chunks; use strict; # create a new engine and read our chunk definitions my $engine = new HTML::Chunks('meals.html'); # output the main 'mealPage' chunk. name information # is supplied with static text. the 'meals' data element # is handled by the 'outputMeals' routine. $engine->output('mealPage', { firstName => 'Homer', lastName => 'Simpson', meals => \&outputMeals }); # our first data element routine sub outputMeals { my ($engine, $element) = @_; # normally you would read this from a database but # this is easier for an example. my @meals = ( [ '2001-09-09 08:15', 'One dozen assorted donuts' ], [ '2001-09-09 11:45', 'One giant sub sandwich' ], [ '2001-09-09 14:22', 'One bag of gummy worms' ], [ '2001-09-09 18:34', 'Bucket of BBQ' ] ); # we output each meal using the 'meal' chunk. simple. foreach my $meal (@meals) { $engine->output('meal', { date => $meal->[0], food => $meal->[1] }); } } SEE ALSOFor the adventurous, there is HTML::Chunks::Super, a subclass of HTML::Chunks with enhanced features.CREDITSCreated, developed and maintained by Mark W Blythe and Dave Balmer, Jr. Contact dbalmer@cpan.org or mblythe@cpan.org for comments or questions.LICENSE(C)2001-2009 Mark W Blythe and Dave Balmer Jr, all rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |