diff options
| author | fukachan <fukachan> | 2002-02-17 06:30:31 +0000 |
|---|---|---|
| committer | fukachan <fukachan> | 2002-02-17 06:30:31 +0000 |
| commit | 69a861e69041ab37a7594f99b9112d7594d9845c (patch) | |
| tree | a41018ea2f15a84d241388a0a7374799e8ce0410 /cpan | |
| parent | 4d75b5d58363a8089f0f91a77c464bd3f65216df (diff) | |
| download | fml8-69a861e69041ab37a7594f99b9112d7594d9845c.tar.gz fml8-69a861e69041ab37a7594f99b9112d7594d9845c.tar.bz2 fml8-69a861e69041ab37a7594f99b9112d7594d9845c.zip | |
Initial revision
Diffstat (limited to 'cpan')
48 files changed, 8868 insertions, 0 deletions
diff --git a/cpan/dist/HTML-Template/ANNOUNCE b/cpan/dist/HTML-Template/ANNOUNCE new file mode 100644 index 00000000..fbd7e2dd --- /dev/null +++ b/cpan/dist/HTML-Template/ANNOUNCE @@ -0,0 +1,63 @@ +ANNOUNCEMENT: HTML::Template 2.5 + +HTML::Template - a Perl module to use HTML Templates + +CHANGES + +2.5 + +- Doc Fix: added reference to new HTML::Template website at + http://html-template.sourceforge.net + +- Bug Fix: global_vars fixed for loops within loops + +- Bug Fix: include paths were broken under Windows (David Ferrance) + +- Bug Fix: nested include path handling was wrong (Gyepi Sam) + +- Bug Fix: MD5 signatures for file cache corrected (Martin Schroth) + +- Bug Fix: print_to was broken for tied filehandles (Darren Chamberlain) + +- Doc Fix: added mailing-list archive URL to FAQ, added link to + tutorial, fixed typos and formatting + + +DESCRIPTION + + This module attempts to make using HTML templates simple and natural. It + extends standard HTML with a few new HTML-esque tags - <TMPL_VAR>, + <TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF>, <TMPL_ELSE> and <TMPL_UNLESS>. + The file written with HTML and these new tags is called a template. It + is usually saved separate from your script - possibly even created by + someone else! Using this module you fill in the values for the + variables, loops and branches declared in the template. This allows you + to separate design - the HTML - from the data, which you generate in the + Perl script. + + This module is licensed under the GPL. See the LICENSE section below for + more details. + +TUTORIAL + + If you're new to HTML::Template, I suggest you start with the + introductory article available on the HTML::Template website: + + http://html-template.sourceforge.net + +AVAILABILITY + + This module is available on SourceForge. Download it at: + + http://html-template.sourceforge.net + + The module is also available on CPAN. You can get it using + CPAN.pm or go to: + + http://www.cpan.org/authors/id/S/SA/SAMTREGAR/ + +CONTACT INFO + + This module was written by Sam Tregar (sam@tregar.com). You can join the + HTML::Template mailing-list by sending a blank message to + htmltmpl-subscribe@lists.vm.com. diff --git a/cpan/dist/HTML-Template/ARTISTIC b/cpan/dist/HTML-Template/ARTISTIC new file mode 100644 index 00000000..5f221241 --- /dev/null +++ b/cpan/dist/HTML-Template/ARTISTIC @@ -0,0 +1,131 @@ + + + + + The "Artistic License" + + Preamble + +The intent of this document is to state the conditions under which a +Package may be copied, such that the Copyright Holder maintains some +semblance of artistic control over the development of the package, +while giving the users of the package the right to use and distribute +the Package in a more-or-less customary fashion, plus the right to make +reasonable modifications. + +Definitions: + + "Package" refers to the collection of files distributed by the + Copyright Holder, and derivatives of that collection of files + created through textual modification. + + "Standard Version" refers to such a Package if it has not been + modified, or has been modified in accordance with the wishes + of the Copyright Holder as specified below. + + "Copyright Holder" is whoever is named in the copyright or + copyrights for the package. + + "You" is you, if you're thinking about copying or distributing + this Package. + + "Reasonable copying fee" is whatever you can justify on the + basis of media cost, duplication charges, time of people involved, + and so on. (You will not be required to justify it to the + Copyright Holder, but only to the computing community at large + as a market that must bear the fee.) + + "Freely Available" means that no fee is charged for the item + itself, though there may be fees involved in handling the item. + It also means that recipients of the item may redistribute it + under the same conditions they received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications +derived from the Public Domain or from the Copyright Holder. A Package +modified in such a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided +that you insert a prominent notice in each changed file stating how and +when you changed that file, and provided that you do at least ONE of the +following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or + an equivalent medium, or placing the modifications on a major archive + site such as uunet.uu.net, or by allowing the Copyright Holder to include + your modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict + with standard executables, which must also be provided, and provide + a separate manual page for each non-standard executable that clearly + documents how it differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or +executable form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where + to get the Standard Version. + + b) accompany the distribution with the machine-readable source of + the Package with your modifications. + + c) give non-standard executables non-standard names, and clearly + document the differences in manual pages (or equivalent), together + with instructions on where to get the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this +Package. You may not charge a fee for this Package itself. However, +you may distribute this Package in aggregate with other (possibly +commercial) programs as part of a larger (possibly commercial) software +distribution provided that you do not advertise this Package as a +product of your own. You may embed this Package's interpreter within +an executable of yours (by linking); this shall be construed as a mere +form of aggregation, provided that the complete Standard Version of the +interpreter is so embedded. + +6. The scripts and library files supplied as input to or produced as +output from the programs of this Package do not automatically fall +under the copyright of this Package, but belong to whoever generated +them, and may be sold commercially, and may be aggregated with this +Package. If such scripts or library files are aggregated with this +Package via the so-called "undump" or "unexec" methods of producing a +binary executable image, then distribution of such an image shall +neither be construed as a distribution of this Package nor shall it +fall under the restrictions of Paragraphs 3 and 4, provided that you do +not represent such an executable image as a Standard Version of this +Package. + +7. C subroutines (or comparably compiled subroutines in other +languages) supplied by you and linked into this Package in order to +emulate subroutines and variables of the language defined by this +Package shall not be considered part of this Package, but are the +equivalent of input as in Paragraph 6, provided these subroutines do +not change the language in any way that would cause it to fail the +regression tests for the language. + +8. Aggregation of this Package with a commercial distribution is always +permitted provided that the use of this Package is embedded; that is, +when no overt attempt is made to make this Package's interfaces visible +to the end user of the commercial distribution. Such use shall not be +construed as a distribution of this Package. + +9. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED +WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + + The End diff --git a/cpan/dist/HTML-Template/Changes b/cpan/dist/HTML-Template/Changes new file mode 100644 index 00000000..59c7f3f1 --- /dev/null +++ b/cpan/dist/HTML-Template/Changes @@ -0,0 +1,213 @@ +Revision history for Perl extension HTML::Template. + +0.00 Fri May 14 14:59:06 1999 + - original version; created by h2xs 1.18 + +0.01 Mon May 17 15:17:00 1999 + - added cacheing to module and perldoc + - moved .tmpl files used by 'make test' to /templates + - first release! + +0.02 Mon May 31 12:47:00 1999 + - die on multiple source parameters in new() + - tries to preserve newlines in loop body + - copies in array contents from array refs on param call + i.e. allows for reuse of scratch arrays on calling side + - Added a CREDITS section to the docs, inaugurated it with + Richard Chen for his many fixes. + - Added type => 'sometype', source => 'source' new() syntax. + - made "NAME=" in tags optional. Added a test.pl to check for + this. + +0.03 Fri June 11 17:37:00 1999 + - fixed a few irritating "undefined variable" errors in -w + - big speedup on large TMPL_LOOPs. They are at least one order of + magnitude faster now! + - die_on_bad_params => 0 never really worked! It does now. + +0.04 Fri June 18 12:00:00 1999 + - fixed cacheing - under certain conditions it was totally broken! + - changed {param} to {param_values} - some older perls complained. + - die_on_bad_params => 0 now also applied to loop body. + - added copious comments about how bad m//i is to avoid future + bug reports about [tT][hH][iI][sS]! + - added numerous bug fixes and optimizations submitted by Mike + Blazer, Adriano Nagelschmidt Rodrigues, Andrej Mikus and + Ilya Obshadko. Thanks! + +0.05 September 10th 1999 17:18 + - Added <TMPL_INCLUDE> functionality! + - Added associate_CGI() method - donated by Richard Dice + - Cleaned up internal access to new() options. + - more bug fixes from such notables as Kevin Puetz, and Steve + Reppucci. + +0.051 September 10th 1999 17:30 + - small problem with make test fixed. + +0.06 September 19th 1999 23:09 + - added associate parameter to new() and obsoleted + associate_CGI() (still supported for now) + - added support for HTML_TEMPLATE_ROOT environment variable + - small performance improvements using typeglobs + - added a performance testing script: time_trial.pl + +0.9 September 28th 1999 17:46 + - <TMPL_IF> and <TMPL_ELSE> + - near total rewrite - faster, smaller, more "compiler-esque" code. + - *MASSIVE* speedups in all modes - up to 10x faster!1 + - bug fix : param() wasn't returning names of LOOPs + - This really is version 0.9 so 1.0 is coming up. Get those + bug reports in! + +0.91 September 29th 1999 17:59 + - bug fix: possible loss of text after a </TMPL_IF> + (Thanks to Tom Huskins for alerting me to this one!) + +0.95 October 8th 1999 12:28 + - bug fix: some lines were getting chomped (Eric Zylberstejn) + - *NUMEROUS* bug fixes (David Glasser - Thanks a lot!) + - new FAQ section in documentation + - code cleanup and improved comments + +0.96 October 14th 1999 15:49 + - Added "ESCAPE=1" option to <TMPL_VAR> to HTML-escape + variable values. (Peter Marelas, thanks!) + - more bug fixes (David Glasses, James William Carlson - + thanks) + - even *more* code cleanup! + - new FAQ concerning pre-loading templates and mod_perl. + +1.0 Fri October 28 12:00:00 1999 + - An HTML::Template mailing-list! Send a blank message to + htmltmpl-subscribe@lists.vm.com to join. + - bug fixes + - improved docs + +1.1 Fri November 05 12:00:00 1999 + - Lifted requirement that <TMPL_INCLUDE>s be alone on a line + - Added "path" option to new() to manipulate search path for + templates. + - bug fixes + +1.2 Wed November 17 12:00:00 1999 + - Added multi-parameter and hash-ref syntax for param() calls. + - Added DTD-compliant <!-- TMPL_* --> syntax patch from + Matthew Wickline - Thanks! + - vanguard_compatibility_mode = 1 implies die_on_bad_params = 0 + +1.2.1 Wed November 17 12:00:01 1999 + - tiny bug fix + +1.3 Fri December 17 12:00:00 1999 + - Omnibus regex patch from Matthew Wickline: a faster and more + robust parse(). + - New tag: TMPL_UNLESS, the opposite of TMPL_IF. + - Numerous bug fixes: mixed-case filenames in includes, + recursive TMPL_INCLUDEs, reporting filename and line number + inside included files, better syntax-error detection. + - Optional loop context variables - __FIRST__, __LAST__ and + __INNER__. + +1.4 Sat January 8 12:00:00 2000 + - New feature: new() option 'shared_cache' enables experimental + IPC shared memory caching! + - TMPL_IF now works on TMPL_LOOP variables. + - Public CVS server available at www.sourceforge.net. + - Bug Fix from Doug Steinwand: loop_context_vars not working + on one-item loops. + +1.5 Mon February 17 12:00:00 2000 + - Shared cache now uses IPC::SharedCache and is much more stable. + - Fixed problem with mixed case associated CGI.pm parameters + - param() now accepts subroutine refs for TMPL_VARs. + +1.5.1 Wed February 23 12:00:00 2000 + - Bug Fix: 1.5 broke caching in some cases - thanks to Drew Taylor + for help solving this one. + +1.6 Sun March 05 12:00:00 2000 + - New Feature: double_cache combines shared memory and local memory + caching for twice the speedup! + - New feature: ESCAPE=URL (added by Tobias Brox) + - Bug Fix: allow for blessed objects in calls to param() + (thanks to Michael Lloyd and David Glasser) + - Bug Fix: _mtime was broken in 1.5, fixed a typo. + +1.7 Fri March 24 12:00:00 2000 + - New Feature: new method query() enables introspection into + loops (Chris Houser) + - New Feature: Better error handling with Carp + - Bug Fix: URLESCAPE was skipping some important escapes (Simran Gambhir) + - New FAQ: How can I execute a program from inside my template? + +1.8 Sun June 25 12:00:00 2000 + - New Feature: global_vars option makes outer variables visible + inside loops. + - Bug Fix: Use File::Spec to construct pathnames. This means + that HTML::Template should now work on VMS and MacOS. + (Larry Moore) + - Bug Fix: loop_context_vars were broken in an unusual case + (Todd Larason) + - Bug Fix: ESCAPE was broken in some cases. + - New License: switched to GPL/Artistic hybrid normally used + with Perl modules. + +2.0 Sun September 16 12:00:00 2000 + - New Feature: new 'search_path_on_include' option (Jody Biggs) + - New Feature: much requested variable __ODD__ added to set of + loop_context_vars. + - New Feature: new 'no_includes' option (Scott Guelich) + - Doc Addition: Added link to Japanese translation (Kawai Takanori) + - Bug Fix: loop_context_vars was mostly broken (T.J. Mather, + Martin Schroth and Dave Wolfe) + - Bug Fix: vanguard_compatibility_mode was broken on first line of + included files. (uchum) + +2.1 Sun December 18 12:00:00 2000 + - New Feature: new 'file_cache' and 'double_file_cache' options + provide a file based caching method (T.J. Mather) + - New Feature: new 'print_to' option for output() allows + output() to print to a filehandle as it runs. + (Chris Nokleberg) + - New Feature: new 'case_sensitive' option to allow template + variable names to be case sensitive. (Matthew Wickline) + - New Feature: new 'filter' option allows pre-parse filtering of + template files. + - Bug Fix: added single-quote escaping to HTML escaping code + (Ralph Corderoy) + - Bug Fix: fixed a noisy bug in param() when used with 'associate' + (William Ward) + - Doc Fix: broke out FAQ into separate file. + +2.2 Sat December 23 12:00:00 2000 + - Bug Fix: fixed memory leak in global_vars implementation + (Ade Olonoh) + - Bug Fix: fixed file_cache not reloading templates on changes + (T.J. Mather) + - Bug Fix: fixed broken error checking in param() (Mark Stosberg) + +2.3 Thu June 28 12:00:00 2001 + - New Feature: template tags can now span lines. (Roland Giersig) + - New Feature: new() option 'filehandle'. (Roland Giersig) + - Bug Fix: includes were broken in some cases using scalarref + templates. (Lance Thomas) + - Bug Fix: recursive include detection was broken for scalarref + templates. (Mark Stosberg) + - Bug Fix: cleaned up more 5.004 warnings. (Jere Julian) + + +2.4 Mon August 27 12:00:00 2001 + - Bug Fix: case_sensitive option broke loops (Peter Leonard) + - Bug Fix: code-ref params now work with IF and UNLESS + +2.5 Fri Feb 01 12:00:00 2002 + - Bug Fix: global_vars fixed for loops within loops + - Bug Fix: include paths were broken under Windows (David Ferrance) + - Bug Fix: nested include path handling was wrong (Gyepi Sam) + - Bug Fix: MD5 signatures for file cache corrected (Martin Schroth) + - Bug Fix: print_to was broken for tied filehandles (Darren Chamberlain) + - Doc Fix: added mailing-list archive URL to FAQ, added link to + tutorial, fixed typos and formatting + - Doc Fix: added reference to new HTML::Template website at + http://html-template.sourceforge.net/ diff --git a/cpan/dist/HTML-Template/FAQ b/cpan/dist/HTML-Template/FAQ new file mode 100644 index 00000000..1336258a --- /dev/null +++ b/cpan/dist/HTML-Template/FAQ @@ -0,0 +1,134 @@ +FREQUENTLY ASKED QUESTIONS + +In the interest of greater understanding I've started a FAQ section of +the perldocs. Please look in here before you send me email. + +1) Is there a place to go to discuss HTML::Template and/or get help? + +There's a mailing-list for HTML::Template at htmltmpl@lists.vm.com. +Send a blank message to htmltmpl-subscribe@lists.vm.com to join! + +2) I want support for <TMPL_XXX>! How about it? + +Maybe. I definitely encourage people to discuss their ideas for +HTML::Template on the mailing list. Please be ready to explain to me +how the new tag fits in with HTML::Template's mission to provide a +fast, lightweight system for using HTML templates. + +NOTE: Offering to program said addition and provide it in the form of +a patch to the most recent version of HTML::Template will definitely +have a softening effect on potential opponents! + +3) I found a bug, can you fix it? + +That depends. Did you send me the VERSION of HTML::Template, a test +script and a test template? If so, then almost certainly. + +If you're feeling really adventurous, HTML::Template has a publically +available CVS server. See below for more information in the PUBLIC +CVS SERVER section. + +4) <TMPL_VAR>s from the main template aren't working inside a <TMPL_LOOP>! Why? + +This is the intended behavior. <TMPL_LOOP> introduces a separate +scope for <TMPL_VAR>s much like a subroutine call in Perl introduces a +separate scope for "my" variables. + +If you want your <TMPL_VAR>s to be global you can set the +'global_vars' option when you call new(). See above for documentation +of the 'global_vars' new() option. + +5) Why do you use /[Tt]/ instead of /t/i? It's so ugly! + +Simple - the case-insensitive match switch is very inefficient. +According to _Mastering_Regular_Expressions_ from O'Reilly Press, +/[Tt]/ is faster and more space efficient than /t/i - by as much as +double against long strings. //i essentially does a lc() on the +string and keeps a temporary copy in memory. + +When this changes, and it is in the 5.6 development series, I will +gladly use //i. Believe me, I realize [Tt] is hideously ugly. + +6) How can I pre-load my templates using cache-mode and mod_perl? + +Add something like this to your startup.pl: + + use HTML::Template; + use File::Find; + + print STDERR "Pre-loading HTML Templates...\n"; + find( + sub { + return unless /\.tmpl$/; + HTML::Template->new( + filename => "$File::Find::dir/$_", + cache => 1, + ); + }, + '/path/to/templates', + '/another/path/to/templates/' + ); + +Note that you'll need to modify the "return unless" line to specify +the extension you use for your template files - I use .tmpl, as you +can see. You'll also need to specify the path to your template files. + +One potential problem: the "/path/to/templates/" must be EXACTLY the +same path you use when you call HTML::Template->new(). Otherwise the +cache won't know they're the same file and will load a new copy - +instead getting a speed increase, you'll double your memory usage. To +find out if this is happening set cache_debug => 1 in your application +code and look for "CACHE MISS" messages in the logs. + +7) What characters are allowed in TMPL_* NAMEs? + +Numbers, letters, '.', '/', '+', '-' and '_'. + +8) How can I execute a program from inside my template? + +Short answer: you can't. Longer answer: you shouldn't since this +violates the fundamental concept behind HTML::Template - that design +and code should be seperate. + +But, inevitably some people still want to do it. At times it has even +seemed that HTML::Template development might split over this issue, so +I will attempt a compromise. Here is a method you can use to allow +your template authors to evaluate arbitrary perl scripts from within +the template. + +First, tell all your designers that when they want to run a perl +script named "program.pl" they should use a tag like: + + <TMPL_VAR NAME="__execute_program.pl__"> + +Then, have all your programmers call this subroutine instead of +calling HTML::Template::new directly. They still use the same +parameters, but they also get the program execution. + + sub new_template { + # get the template object + my $template = HTML::Template->new(@_); + + # find program parameters and fill them in + my @params = $template->param(); + for my $param (@params) { + if ($param =~ /^__execute_(.*)__$/) { + $template->param($param, do($1)); + } + } + + # return the template object + return $template; + } + +The programs called in this way should return a string containing +their output. A more complicated subroutine could be written to +capture STDOUT from the scripts, but this one is simple enough to +include in the FAQ. Another improvement would be to use query() to +enable program execution inside loops. + +9) Can I get a copy of these docs in Japanese? + +Yes you can. See Kawai Takanori's translation at: + + http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm diff --git a/cpan/dist/HTML-Template/GPL b/cpan/dist/HTML-Template/GPL new file mode 100644 index 00000000..60549be5 --- /dev/null +++ b/cpan/dist/HTML-Template/GPL @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) 19yy <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/cpan/dist/HTML-Template/MANIFEST b/cpan/dist/HTML-Template/MANIFEST new file mode 100644 index 00000000..3cb6c867 --- /dev/null +++ b/cpan/dist/HTML-Template/MANIFEST @@ -0,0 +1,47 @@ +Changes +MANIFEST +Makefile.PL +Template.pm +test.pl +templates/simple.tmpl +templates/simple-loop.tmpl +templates/medium.tmpl +templates/simple-loop-nonames.tmpl +templates/long_loops.tmpl +templates/include.tmpl +templates/included.tmpl +templates/included2.tmpl +templates/other-loop.tmpl +templates/if.tmpl +templates/ifelse.tmpl +templates/double_loop.tmpl +templates/escape.tmpl +templates/unless.tmpl +templates/recursive.tmpl +templates/context.tmpl +templates/loop-if.tmpl +templates/urlescape.tmpl +templates/query-test.tmpl +templates/query-test2.tmpl +templates/globals.tmpl +templates/vanguard1.tmpl +templates/vanguard2.tmpl +templates/loop-context.tmpl +templates/searchpath/included.tmpl +templates/searchpath/two.tmpl +templates/searchpath/three.tmpl +templates/include_path/a.tmpl +templates/include_path/b.tmpl +templates/include_path/one.tmpl +templates/multiline_tags.tmpl +templates/newline_test1.tmpl +templates/newline_test2.tmpl +templates/case_loop.tmpl +templates/global-loops.tmpl +scripts/time_trial.pl +scripts/clean_shm.pl +README +GPL +ARTISTIC +ANNOUNCE +FAQ diff --git a/cpan/dist/HTML-Template/Makefile.PL b/cpan/dist/HTML-Template/Makefile.PL new file mode 100644 index 00000000..4ee3756e --- /dev/null +++ b/cpan/dist/HTML-Template/Makefile.PL @@ -0,0 +1,8 @@ +use ExtUtils::MakeMaker; +# See lib/ExtUtils/MakeMaker.pm for details of how to influence +# the contents of the Makefile that is written. +WriteMakefile( + 'NAME' => 'HTML::Template', + 'VERSION_FROM' => 'Template.pm', # finds $VERSION + 'PREREQ_PM' => {Carp => 0, 'File::Spec' => 0.82}, +); diff --git a/cpan/dist/HTML-Template/README b/cpan/dist/HTML-Template/README new file mode 100644 index 00000000..2f714d63 --- /dev/null +++ b/cpan/dist/HTML-Template/README @@ -0,0 +1,114 @@ +
+ HTML::Template - a Perl module to use HTML Templates
+
+
+DESCRIPTION
+
+This module attempts make using HTML templates simple and natural. It
+extends standard HTML with a few new HTML-esque tags - <TMPL_VAR>,
+<TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF> and <TMPL_ELSE>. The file
+written with HTML and these new tags is called a template. It is
+usually saved separate from your script - possibly even created by
+someone else! Using this module you fill in the values for the
+variables, loops and branches declared in the template. This allows
+you to seperate design - the HTML - from the data, which you generate
+in the Perl script.
+
+A Japanese translation of the documentation is available at:
+
+ http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm
+
+MOTIVATION
+
+It is true that there are a number of packages out there to do HTML
+templates. On the one hand you have things like HTML::Embperl which
+allows you freely mix Perl with HTML. On the other hand lie
+home-grown variable substitution solutions. Hopefully the module can
+find a place between the two.
+
+One advantage of this module over a full HTML::Embperl-esque solution
+is that it enforces an important divide - design and programming. By
+limiting the programmer to just using simple variables and loops in
+the HTML, the template remains accessible to designers and other
+non-perl people. The use of HTML-esque syntax goes further to make
+the format understandable to others. In the future this similarity
+could be used to extend existing HTML editors/analyzers to support
+this syntax.
+
+An advantage of this module over home-grown tag-replacement schemes is
+the support for loops. In my work I am often called on to produce
+tables of data in html. Producing them using simplistic HTML
+templates results in CGIs containing lots of HTML since the HTML
+itself could not represent loops. The introduction of loop statements
+in the HTML simplifies this situation considerably. The designer can
+layout a single row and the programmer can fill it in as many times as
+necessary - all they must agree on is the parameter names.
+
+For all that, I think the best thing about this module is that it does
+just one thing and it does it quickly and carefully. It doesn't try
+to replace Perl and HTML, it just augments them to interact a little
+better. And it's pretty fast.
+
+
+DOCUMENTATION
+
+The documentation is in Template.pm in the form of POD format
+perldocs. Even the above text might be out of date, so be sure to
+check the perldocs for the straight truth!
+
+
+INSTALLATION
+
+In case this is your first module, the usual methods are:
+
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+If you plan to use the shared memory cache you might want to test it
+before you install. Make sure you have an up-to-date copy of
+IPC::SharedCache installed (available from the same place you got
+HTML::Template, either CPAN or SourceForge) and then do:
+
+ TEST_SHARED_MEMORY=1 make test
+
+If you plan to use the file cache you might want to test it before you
+install. Make sure you have an up-to-date copy of Storable and
+Digest::MD5 (both available on CPAN) installed and then do:
+
+ TEST_FILE_CACHE=1 make test
+
+CONTACT INFO
+
+This module was written by Sam Tregar (sam@tregar.com) for Vanguard
+Media (http://www.vm.com). You can join the HTML::Template
+mailing-list by sending a blank message to
+htmltmpl-subscribe@lists.vm.com.
+
+LICENSE
+
+HTML::Template : A module for using HTML Templates with Perl
+Copyright (C) 2000 Sam Tregar (sam@tregar.com)
+
+This module is free software; you can redistribute it and/or modify it
+under the terms of either:
+
+a) the GNU General Public License as published by the Free Software
+Foundation; either version 1, or (at your option) any later version,
+or
+
+b) the "Artistic License" which comes with this module.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
+the GNU General Public License or the Artistic License for more details.
+
+You should have received a copy of the Artistic License with this
+module, in the file ARTISTIC. If not, I'll be glad to provide one.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+USA
diff --git a/cpan/dist/HTML-Template/Template.pm b/cpan/dist/HTML-Template/Template.pm new file mode 100644 index 00000000..b44802e5 --- /dev/null +++ b/cpan/dist/HTML-Template/Template.pm @@ -0,0 +1,3137 @@ +package HTML::Template; + +$HTML::Template::VERSION = '2.5'; + +=head1 NAME + +HTML::Template - Perl module to use HTML Templates from CGI scripts + +=head1 SYNOPSIS + +First you make a template - this is just a normal HTML file with a few +extra tags, the simplest being <TMPL_VAR> + +For example, test.tmpl: + + <html> + <head><title>Test Template</title> + <body> + My Home Directory is <TMPL_VAR NAME=HOME> + <p> + My Path is set to <TMPL_VAR NAME=PATH> + </body> + </html> + +Now create a small CGI program: + + #!/usr/bin/perl -w + use HTML::Template; + + # open the html template + my $template = HTML::Template->new(filename => 'test.tmpl'); + + # fill in some parameters + $template->param(HOME => $ENV{HOME}); + $template->param(PATH => $ENV{PATH}); + + # send the obligatory Content-Type and print the template output + print "Content-Type: text/html\n\n", $template->output; + +If all is well in the universe this should show something like this in +your browser when visiting the CGI: + + My Home Directory is /home/some/directory + My Path is set to /bin;/usr/bin + +=head1 DESCRIPTION + +This module attempts to make using HTML templates simple and natural. +It extends standard HTML with a few new HTML-esque tags - <TMPL_VAR>, +<TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF>, <TMPL_ELSE> and <TMPL_UNLESS>. +The file written with HTML and these new tags is called a template. +It is usually saved separate from your script - possibly even created +by someone else! Using this module you fill in the values for the +variables, loops and branches declared in the template. This allows +you to separate design - the HTML - from the data, which you generate +in the Perl script. + +This module is licensed under the GPL. See the LICENSE section +below for more details. + +=head1 TUTORIAL + +If you're new to HTML::Template, I suggest you start with the +introductory article available on the HTML::Template website: + + http://html-template.sourceforge.net + +=head1 MOTIVATION + +It is true that there are a number of packages out there to do HTML +templates. On the one hand you have things like HTML::Embperl which +allows you freely mix Perl with HTML. On the other hand lie +home-grown variable substitution solutions. Hopefully the module can +find a place between the two. + +One advantage of this module over a full HTML::Embperl-esque solution +is that it enforces an important divide - design and programming. By +limiting the programmer to just using simple variables and loops in +the HTML, the template remains accessible to designers and other +non-perl people. The use of HTML-esque syntax goes further to make +the format understandable to others. In the future this similarity +could be used to extend existing HTML editors/analyzers to support +HTML::Template. + +An advantage of this module over home-grown tag-replacement schemes is +the support for loops. In my work I am often called on to produce +tables of data in html. Producing them using simplistic HTML +templates results in CGIs containing lots of HTML since the HTML +itself cannot represent loops. The introduction of loop statements in +the HTML simplifies this situation considerably. The designer can +layout a single row and the programmer can fill it in as many times as +necessary - all they must agree on is the parameter names. + +For all that, I think the best thing about this module is that it does +just one thing and it does it quickly and carefully. It doesn't try +to replace Perl and HTML, it just augments them to interact a little +better. And it's pretty fast. + +=head1 THE TAGS + +=head2 TMPL_VAR + + <TMPL_VAR NAME="PARAMETER_NAME"> + +The <TMPL_VAR> tag is very simple. For each <TMPL_VAR> tag in the +template you call $template->param(PARAMETER_NAME => "VALUE"). When +the template is output the <TMPL_VAR> is replaced with the VALUE text +you specified. If you don't set a parameter it just gets skipped in +the output. + +Optionally you can use the "ESCAPE=HTML" option in the tag to indicate +that you want the value to be HTML-escaped before being returned from +output (the old ESCAPE=1 syntax is still supported). This means that +the ", <, >, and & characters get translated into ", <, > +and & respectively. This is useful when you want to use a +TMPL_VAR in a context where those characters would cause trouble. +Example: + + <input name=param type=text value="<TMPL_VAR NAME="PARAM">"> + +If you called param() with a value like sam"my you'll get in trouble +with HTML's idea of a double-quote. On the other hand, if you use +ESCAPE=HTML, like this: + + <input name=param type=text value="<TMPL_VAR ESCAPE=HTML NAME="PARAM">"> + +You'll get what you wanted no matter what value happens to be passed in for +param. You can also write ESCAPE="HTML", ESCAPE='HTML' and ESCAPE='1'. +Substitute a 0 for the HTML and you turn off escaping, which is the default +anyway. + +There is also the "ESCAPE=URL" option which may be used for VARs that +populate a URL. It will do URL escaping, like replacing ' ' with '+' +and '/' with '%2F'. + +=head2 TMPL_LOOP + + <TMPL_LOOP NAME="LOOP_NAME"> ... </TMPL_LOOP> + +The <TMPL_LOOP> tag is a bit more complicated than <TMPL_VAR>. The +<TMPL_LOOP> tag allows you to delimit a section of text and give it a +name. Inside this named loop you place <TMPL_VAR>s. Now you pass to +param() a list (an array ref) of parameter assignments (hash refs) for +this loop. The loop iterates over the list and produces output from +the text block for each pass. Unset parameters are skipped. Here's +an example: + + In the template: + + <TMPL_LOOP NAME=EMPLOYEE_INFO> + Name: <TMPL_VAR NAME=NAME> <br> + Job: <TMPL_VAR NAME=JOB> <p> + </TMPL_LOOP> + + + In the script: + + $template->param(EMPLOYEE_INFO => [ + { name => 'Sam', job => 'programmer' }, + { name => 'Steve', job => 'soda jerk' }, + ] + ); + print $template->output(); + + + The output in a browser: + + Name: Sam + Job: programmer + + Name: Steve + Job: soda jerk + +As you can see above the <TMPL_LOOP> takes a list of variable +assignments and then iterates over the loop body producing output. + +Often you'll want to generate a <TMPL_LOOP>'s contents +programmatically. Here's an example of how this can be done (many +other ways are possible!): + + # a couple of arrays of data to put in a loop: + my @words = qw(I Am Cool); + my @numbers = qw(1 2 3); + + my @loop_data = (); # initialize an array to hold your loop + + while (@words and @numbers) { + my %row_data; # get a fresh hash for the row data + + # fill in this row + $row_data{WORD} = shift @words; + $row_data{NUMBER} = shift @numbers; + + # the crucial step - push a reference to this row into the loop! + push(@loop_data, \%row_data); + } + + # finally, assign the loop data to the loop param, again with a + # reference: + $template->param(THIS_LOOP => \@loop_data); + +The above example would work with a template like: + + <TMPL_LOOP NAME="THIS_LOOP"> + Word: <TMPL_VAR NAME="WORD"> <br> + Number: <TMPL_VAR NAME="NUMBER"> <p> + </TMPL_LOOP> + +It would produce output like: + + Word: I + Number: 1 + + Word: Am + Number: 2 + + Word: Cool + Number: 3 + +<TMPL_LOOP>s within <TMPL_LOOP>s are fine and work as you would +expect. If the syntax for the param() call has you stumped, here's an +example of a param call with one nested loop: + + $template->param(LOOP => [ + { name => 'Bobby', + nicknames => [ + { name => 'the big bad wolf' }, + { name => 'He-Man' }, + ], + }, + ], + ); + +Basically, each <TMPL_LOOP> gets an array reference. Inside the array +are any number of hash references. These hashes contain the +name=>value pairs for a single pass over the loop template. + +Inside a <TMPL_LOOP>, the only variables that are usable are the ones +from the <TMPL_LOOP>. The variables in the outer blocks are not +visible within a template loop. For the computer-science geeks among +you, a <TMPL_LOOP> introduces a new scope much like a perl subroutine +call. If you want your variables to be global you can use +'global_vars' option to new() described below. + +=head2 TMPL_INCLUDE + + <TMPL_INCLUDE NAME="filename.tmpl"> + +This tag includes a template directly into the current template at the +point where the tag is found. The included template contents are used +exactly as if its contents were physically included in the master +template. + +The file specified can be an absolute path (beginning with a '/' under +Unix, for example). If it isn't absolute, the path to the enclosing +file is tried first. After that the path in the environment variable +HTML_TEMPLATE_ROOT is tried, if it exists. Next, the "path" option is +consulted. As a final attempt, the filename is passed to open() +directly. See below for more information on HTML_TEMPLATE_ROOT and +the "path" option to new(). + +As a protection against infinitly recursive includes, an arbitary +limit of 10 levels deep is imposed. You can alter this limit with the +"max_includes" option. See the entry for the "max_includes" option +below for more details. + +=head2 TMPL_IF + + <TMPL_IF NAME="PARAMETER_NAME"> ... </TMPL_IF> + +The <TMPL_IF> tag allows you to include or not include a block of the +template based on the value of a given parameter name. If the +parameter is given a value that is true for Perl - like '1' - then the +block is included in the output. If it is not defined, or given a +false value - like '0' - then it is skipped. The parameters are +specified the same way as with TMPL_VAR. + +Example Template: + + <TMPL_IF NAME="BOOL"> + Some text that only gets displayed if BOOL is true! + </TMPL_IF> + +Now if you call $template->param(BOOL => 1) then the above block will +be included by output. + +<TMPL_IF> </TMPL_IF> blocks can include any valid HTML::Template +construct - VARs and LOOPs and other IF/ELSE blocks. Note, however, +that intersecting a <TMPL_IF> and a <TMPL_LOOP> is invalid. + + Not going to work: + <TMPL_IF BOOL> + <TMPL_LOOP SOME_LOOP> + </TMPL_IF> + </TMPL_LOOP> + +If the name of a TMPL_LOOP is used in a TMPL_IF, the IF block will +output if the loop has at least one row. Example: + + <TMPL_IF LOOP_ONE> + This will output if the loop is not empty. + </TMPL_IF> + + <TMPL_LOOP LOOP_ONE> + .... + </TMPL_LOOP> + +WARNING: Much of the benefit of HTML::Template is in decoupling your +Perl and HTML. If you introduce numerous cases where you have +TMPL_IFs and matching Perl if()s, you will create a maintenance +problem in keeping the two synchronized. I suggest you adopt the +practice of only using TMPL_IF if you can do so without requiring a +matching if() in your Perl code. + +=head2 TMPL_ELSE + + <TMPL_IF NAME="PARAMETER_NAME"> ... <TMPL_ELSE> ... </TMPL_IF> + +You can include an alternate block in your TMPL_IF block by using +TMPL_ELSE. NOTE: You still end the block with </TMPL_IF>, not +</TMPL_ELSE>! + + Example: + + <TMPL_IF BOOL> + Some text that is included only if BOOL is true + <TMPL_ELSE> + Some text that is included only if BOOL is false + </TMPL_IF> + +=head2 TMPL_UNLESS + + <TMPL_UNLESS NAME="PARAMETER_NAME"> ... </TMPL_UNLESS> + +This tag is the opposite of <TMPL_IF>. The block is output if the +CONTROL_PARAMETER is set false or not defined. You can use +<TMPL_ELSE> with <TMPL_UNLESS> just as you can with <TMPL_IF>. + + Example: + + <TMPL_UNLESS BOOL> + Some text that is output only if BOOL is FALSE. + <TMPL_ELSE> + Some text that is output only if BOOL is TRUE. + </TMPL_UNLESS> + +If the name of a TMPL_LOOP is used in a TMPL_UNLESS, the UNLESS block +output if the loop has zero rows. + + <TMPL_UNLESS LOOP_ONE> + This will output if the loop is empty. + </TMPL_UNLESS> + + <TMPL_LOOP LOOP_ONE> + .... + </TMPL_LOOP> + +=cut + +=head2 NOTES + +HTML::Template's tags are meant to mimic normal HTML tags. However, +they are allowed to "break the rules". Something like: + + <img src="<TMPL_VAR IMAGE_SRC>"> + +is not really valid HTML, but it is a perfectly valid use and will +work as planned. + +The "NAME=" in the tag is optional, although for extensibility's sake I +recommend using it. Example - "<TMPL_LOOP LOOP_NAME>" is acceptable. + +If you're a fanatic about valid HTML and would like your templates +to conform to valid HTML syntax, you may optionally type template tags +in the form of HTML comments. This may be of use to HTML authors who +would like to validate their templates' HTML syntax prior to +HTML::Template processing, or who use DTD-savvy editing tools. + + <!-- TMPL_VAR NAME=PARAM1 --> + +In order to realize a dramatic savings in bandwidth, the standard +(non-comment) tags will be used throughout this documentation. + +=head1 METHODS + +=head2 new() + +Call new() to create a new Template object: + + my $template = HTML::Template->new( filename => 'file.tmpl', + option => 'value' + ); + +You must call new() with at least one name => value pair specifying how +to access the template text. You can use "filename => 'file.tmpl'" to +specify a filename to be opened as the template. Alternately you can +use: + + my $t = HTML::Template->new( scalarref => $ref_to_template_text, + option => 'value' + ); + +and + + my $t = HTML::Template->new( arrayref => $ref_to_array_of_lines , + option => 'value' + ); + + +These initialize the template from in-memory resources. In almost +every case you'll want to use the filename parameter. If you're +worried about all the disk access from reading a template file just +use mod_perl and the cache option detailed below. + +You can also read the template from an already opened filehandle, +either traditionally as a glob or as a FileHandle: + + my $t = HTML::Template->new( filehandle => *FH, option => 'value'); + +The four new() calling methods can also be accessed as below, if you +prefer. + + my $t = HTML::Template->new_file('file.tmpl', option => 'value'); + + my $t = HTML::Template->new_scalar_ref($ref_to_template_text, + option => 'value'); + + my $t = HTML::Template->new_array_ref($ref_to_array_of_lines, + option => 'value'); + + my $t = HTML::Template->new_filehandle($fh, + option => 'value'); + +And as a final option, for those that might prefer it, you can call new as: + + my $t = HTML::Template->new(type => 'filename', + source => 'file.tmpl'); + +Which works for all three of the source types. + +If the environment variable HTML_TEMPLATE_ROOT is set and your +filename doesn't begin with /, then the path will be relative to the +value of $HTML_TEMPLATE_ROOT. Example - if the environment variable +HTML_TEMPLATE_ROOT is set to "/home/sam" and I call +HTML::Template->new() with filename set to "sam.tmpl", the +HTML::Template will try to open "/home/sam/sam.tmpl" to access the +template file. You can also affect the search path for files with the +"path" option to new() - see below for more information. + +You can modify the Template object's behavior with new. These options +are available: + +=over 4 + +=item Error Detection Options + +=over 4 + +=item * + +die_on_bad_params - if set to 0 the module will let you call +$template->param(param_name => 'value') even if 'param_name' doesn't +exist in the template body. Defaults to 1. + +=item * + +strict - if set to 0 the module will allow things that look like they +might be TMPL_* tags to get by without dieing. Example: + + <TMPL_HUH NAME=ZUH> + +Would normally cause an error, but if you call new with strict => 0, +HTML::Template will ignore it. Defaults to 1. + +=item * + +vanguard_compatibility_mode - if set to 1 the module will expect to +see <TMPL_VAR>s that look like %NAME% in addition to the standard +syntax. Also sets die_on_bad_params => 0. If you're not at Vanguard +Media trying to use an old format template don't worry about this one. +Defaults to 0. + +=back + +=item Caching Options + +=over 4 + +=item * + +cache - if set to 1 the module will cache in memory the parsed +templates based on the filename parameter and modification date of the +file. This only applies to templates opened with the filename +parameter specified, not scalarref or arrayref templates. Caching +also looks at the modification times of any files included using +<TMPL_INCLUDE> tags, but again, only if the template is opened with +filename parameter. + +This is mainly of use in a persistent environment like +Apache/mod_perl. It has absolutely no benefit in a normal CGI +environment since the script is unloaded from memory after every +request. For a cache that does work for normal CGIs see the +'shared_cache' option below. + +Note that different new() parameter settings do not cause a cache +refresh, only a change in the modification time of the template will +trigger a cache refresh. For most usages this is fine. My simplistic +testing shows that using cache yields a 90% performance increase under +mod_perl. Cache defaults to 0. + +=item * + +shared_cache - if set to 1 the module will store its cache in shared +memory using the IPC::SharedCache module (available from CPAN). The +effect of this will be to maintain a single shared copy of each parsed +template for all instances of HTML::Template to use. This can be a +significant reduction in memory usage in a multiple server +environment. As an example, on one of our systems we use 4MB of +template cache and maintain 25 httpd processes - shared_cache results +in saving almost 100MB! Of course, some reduction in speed versus +normal caching is to be expected. Another difference between normal +caching and shared_cache is that shared_cache will work in a CGI +environment - normal caching is only useful in a persistent +environment like Apache/mod_perl. + +By default HTML::Template uses the IPC key 'TMPL' as a shared root +segment (0x4c504d54 in hex), but this can be changed by setting the +'ipc_key' new() parameter to another 4-character or integer key. +Other options can be used to affect the shared memory cache correspond +to IPC::SharedCache options - ipc_mode, ipc_segment_size and +ipc_max_size. See L<IPC::SharedCache> for a description of how these +work - in most cases you shouldn't need to change them from the +defaults. + +For more information about the shared memory cache system used by +HTML::Template see L<IPC::SharedCache>. + +=item * + +double_cache - if set to 1 the module will use a combination of +shared_cache and normal cache mode for the best possible caching. Of +course, it also uses the most memory of all the cache modes. All the +same ipc_* options that work with shared_cache apply to double_cache +as well. By default double_cache is off. + +=item * + +blind_cache - if set to 1 the module behaves exactly as with normal +caching but does not check to see if the file has changed on each +request. This option should be used with caution, but could be of use +on high-load servers. My tests show blind_cache performing only 1 to +2 percent faster than cache under mod_perl. + +NOTE: Combining this option with shared_cache can result in stale +templates stuck permanently in shared memory! + +=item * + +file_cache - if set to 1 the module will store its cache in a file +using the Storable module. It uses no additional memory, and my +simplistic testing shows that it yields a 50% performance advantage. +Like shared_cache, it will work in a CGI environment. Default is 0. + +If you set this option you must set the "file_cache_dir" option. See +below for details. + +NOTE: Storable using flock() to ensure safe access to cache files. +Using file_cache on a system or filesystem (NFS) without flock() +support is dangerous. + + +=item * + +file_cache_dir - sets the directory where the module will store the +cache files if file_cache is enabled. Your script will need write +permissions to this directory. You'll also need to make sure the +sufficient space is available to store the cache files. + +=item * + +file_cache_dir_mode - sets the file mode for newly created file_cache +directories and subdirectories. Defaults to 0700 for security but +this may be inconvenient if you do not have access to the account +running the webserver. + +=item * + +double_file_cache - if set to 1 the module will use a combination of +file_cache and normal cache mode for the best possible caching. The +file_cache_* options that work with file_cache apply to double_file_cache +as well. By default double_file_cache is 0. + +=back + +=item Filesystem Options + +=over 4 + +=item * + +path - you can set this variable with a list of paths to search for +files specified with the "filename" option to new() and for files +included with the <TMPL_INCLUDE> tag. This list is only consulted +when the filename is relative. The HTML_TEMPLATE_ROOT environment +variable is always tried first if it exists. In the case of a +<TMPL_INCLUDE> file, the path to the including file is also tried +before path is consulted. + +Example: + + my $template = HTML::Template->new( filename => 'file.tmpl', + path => [ '/path/to/templates', + '/alternate/path' + ] + ); + +NOTE: the paths in the path list must be expressed as UNIX paths, +separated by the forward-slash character ('/'). + +=item * + +search_path_on_include - if set to a true value the module will search +from the top of the array of paths specified by the path option on +every <TMPL_INCLUDE> and use the first matching template found. The +normal behavior is to look only in the current directory for a +template to include. Defaults to 0. + +=back + +=item Debugging Options + +=over 4 + +=item * + +debug - if set to 1 the module will write random debugging information +to STDERR. Defaults to 0. + +=item * + +stack_debug - if set to 1 the module will use Data::Dumper to print +out the contents of the parse_stack to STDERR. Defaults to 0. + +=item * + +cache_debug - if set to 1 the module will send information on cache +loads, hits and misses to STDERR. Defaults to 0. + +=item * + +shared_cache_debug - if set to 1 the module will turn on the debug +option in IPC::SharedCache - see L<IPC::SharedCache> for +details. Defaults to 0. + +=item * + +memory_debug - if set to 1 the module will send information on cache +memory usage to STDERR. Requires the GTop module. Defaults to 0. + +=back + +=item Miscellaneous Options + +=over 4 + +=item * + +associate - this option allows you to inherit the parameter values +from other objects. The only requirement for the other object is that +it have a param() method that works like HTML::Template's param(). A +good candidate would be a CGI.pm query object. Example: + + my $query = new CGI; + my $template = HTML::Template->new(filename => 'template.tmpl', + associate => $query); + +Now, $template->output() will act as though + + $template->param('FormField', $cgi->param('FormField')); + +had been specified for each key/value pair that would be provided by +the $cgi->param() method. Parameters you set directly take precedence +over associated parameters. + +You can specify multiple objects to associate by passing an anonymous +array to the associate option. They are searched for parameters in +the order they appear: + + my $template = HTML::Template->new(filename => 'template.tmpl', + associate => [$query, $other_obj]); + +The old associateCGI() call is still supported, but should be +considered obsolete. + +NOTE: The parameter names are matched in a case-insensitve manner. If +you have two parameters in a CGI object like 'NAME' and 'Name' one +will be chosen randomly by associate. This behavior can be changed by +the following option. + +=item * + +case_sensitive - setting this option to true causes HTML::Template to +treat template variable names case-sensitively. The following example +would only set one parameter without the "case_sensitive" option: + + my $template = HTML::Template->new(filename => 'template.tmpl', + case_sensitive => 1); + $template->param( + FieldA => 'foo', + fIELDa => 'bar', + ); + +This option defaults to off. + +=item * + +loop_context_vars - when this parameter is set to true (it is false by +default) four loop context variables are made available inside a loop: +__FIRST__, __LAST__, __INNER__, __ODD__. They can be used with +<TMPL_IF>, <TMPL_UNLESS> and <TMPL_ELSE> to control how a loop is +output. Example: + + <TMPL_LOOP NAME="FOO"> + <TMPL_IF NAME="__FIRST__"> + This only outputs on the first pass. + </TMPL_IF> + + <TMPL_IF NAME="__ODD__"> + This outputs every other pass, on the odd passes. + </TMPL_IF> + + <TMPL_UNLESS NAME="__ODD__"> + This outputs every other pass, on the even passes. + </TMPL_IF> + + <TMPL_IF NAME="__INNER__"> + This outputs on passes that are neither first nor last. + </TMPL_IF> + + <TMPL_IF NAME="__LAST__"> + This only outputs on the last pass. + <TMPL_IF> + </TMPL_LOOP> + +One use of this feature is to provide a "separator" similar in effect +to the perl function join(). Example: + + <TMPL_LOOP FRUIT> + <TMPL_IF __LAST__> and </TMPL_IF> + <TMPL_VAR KIND><TMPL_UNLESS __LAST__>, <TMPL_ELSE>.</TMPL_UNLESS> + </TMPL_LOOP> + +Would output (in a browser) something like: + + Apples, Oranges, Brains, Toes, and Kiwi. + +Given an appropriate param() call, of course. NOTE: A loop with only +a single pass will get both __FIRST__ and __LAST__ set to true, but +not __INNER__. + +=item * + +no_includes - set this option to 1 to disallow the <TMPL_INCLUDE> tag +in the template file. This can be used to make opening untrusted +templates B<slightly> less dangerous. Defaults to 0. + +=item * + +max_includes - set this variable to determine the maximum depth that +includes can reach. Set to 10 by default. Including files to a depth +greater than this value causes an error message to be displayed. Set +to 0 to disable this protection. + +=item * + +global_vars - normally variables declared outside a loop are not +available inside a loop. This option makes <TMPL_VAR>s like global +variables in Perl - they have unlimited scope. This option also +affects <TMPL_IF> and <TMPL_UNLESS>. + +Example: + + This is a normal variable: <TMPL_VAR NORMAL>.<P> + + <TMPL_LOOP NAME=FROOT_LOOP> + Here it is inside the loop: <TMPL_VAR NORMAL><P> + </TMPL_LOOP> + +Normally this wouldn't work as expected, since <TMPL_VAR NORMAL>'s +value outside the loop is not available inside the loop. + +The global_vars option also allows you to access the values of an +enclosing loop within an inner loop. For example, in this loop the +inner loop will have access to the value of OUTER_VAR in the correct +iteration: + + <TMPL_LOOP OUTER_LOOP> + OUTER: <TMPL_VAR OUTER_VAR> + <TMPL_LOOP INNER_LOOP> + INNER: <TMPL_VAR INNER_VAR> + INSIDE OUT: <TMPL_VAR OUTER_VAR> + </TMPL_LOOP> + </TMPL_LOOP> + +=item * + +filter - this option allows you to specify a filter for your template +files. A filter is a subroutine that will be called after +HTML::Template reads your template file but before it starts parsing +template tags. + +In the most simple usage, you simply assign a code reference to the +filter parameter. This subroutine will recieve a single arguement - a +reference to a string containing the template file text. Here is an +example that accepts templates with tags that look like "!!!ZAP_VAR +FOO!!!" and transforms them into HTML::Template tags: + + my $filter = sub { + my $text_ref = shift; + $$text_ref =~ s/!!!ZAP_(.*?)!!!/<TMPL_$1>/g; + }; + + # open zap.tmpl using the above filter + my $template = HTML::Template->new(filename => 'zap.tmpl', + filter => $filter); + +More complicated usages are possible. You can request that your +filter receieve the template text as an array of lines rather than as +a single scalar. To do that you need to specify your filter using a +hash-ref. In this form you specify the filter using the "sub" key and +the desired argument format using the "format" key. The available +formats are "scalar" and "array". Using the "array" format will incur +a performance penalty but may be more convenient in some situations. + + my $template = HTML::Template->new(filename => 'zap.tmpl', + filter => { sub => $filter, + format => 'array' }); + +You may also have multiple filters. This allows simple filters to be +combined for more elaborate functionality. To do this you specify an +array of filters. The filters are applied in the order they are +specified. + + my $template = HTML::Template->new(filename => 'zap.tmpl', + filter => [ + { sub => \&decompress, + format => 'scalar' }, + { sub => \&remove_spaces, + format => 'array' } + ]); + +The specified filters will be called for any TMPL_INCLUDEed files just +as they are for the main template file. + +=back + +=back 4 + +=cut + + +use integer; # no floating point math so far! +use strict; # and no funny business, either. + +use Carp; # generate better errors with more context +use File::Spec; # generate paths that work on all platforms + +# define accessor constants used to improve readability of array +# accesses into "objects". I used to use 'use constant' but that +# seems to cause occasional irritating warnings in older Perls. +package HTML::Template::LOOP; +sub TEMPLATE_HASH { 0; } +sub PARAM_SET { 1 }; + +package HTML::Template::COND; +sub VARIABLE { 0 }; +sub VARIABLE_TYPE { 1 }; +sub VARIABLE_TYPE_VAR { 0 }; +sub VARIABLE_TYPE_LOOP { 1 }; +sub JUMP_IF_TRUE { 2 }; +sub JUMP_ADDRESS { 3 }; +sub WHICH { 4 }; +sub WHICH_IF { 0 }; +sub WHICH_UNLESS { 1 }; + +# back to the main package scope. +package HTML::Template; + +# open a new template and return an object handle +sub new { + my $pkg = shift; + my $self; { my %hash; $self = bless(\%hash, $pkg); } + + # the options hash + my $options = {}; + $self->{options} = $options; + + # set default parameters in options hash + %$options = ( + debug => 0, + stack_debug => 0, + timing => 0, + search_path_on_include => 0, + cache => 0, + blind_cache => 0, + file_cache => 0, + file_cache_dir => '', + file_cache_dir_mode => 0700, + cache_debug => 0, + shared_cache_debug => 0, + memory_debug => 0, + die_on_bad_params => 1, + vanguard_compatibility_mode => 0, + associate => [], + path => [], + strict => 1, + loop_context_vars => 0, + max_includes => 10, + shared_cache => 0, + double_cache => 0, + double_file_cache => 0, + ipc_key => 'TMPL', + ipc_mode => 0666, + ipc_segment_size => 65536, + ipc_max_size => 0, + global_vars => 0, + no_includes => 0, + case_sensitive => 0, + filter => [], + ); + + # load in options supplied to new() + for (my $x = 0; $x <= $#_; $x += 2) { + defined($_[($x + 1)]) or croak("HTML::Template->new() called with odd number of option parameters - should be of the form option => value"); + $options->{lc($_[$x])} = $_[($x + 1)]; + } + + # blind_cache = 1 implies cache = 1 + $options->{blind_cache} and $options->{cache} = 1; + + # shared_cache = 1 implies cache = 1 + $options->{shared_cache} and $options->{cache} = 1; + + # file_cache = 1 implies cache = 1 + $options->{file_cache} and $options->{cache} = 1; + + # double_cache is a combination of shared_cache and cache. + $options->{double_cache} and $options->{cache} = 1; + $options->{double_cache} and $options->{shared_cache} = 1; + + # double_file_cache is a combination of file_cache and cache. + $options->{double_file_cache} and $options->{cache} = 1; + $options->{double_file_cache} and $options->{file_cache} = 1; + + # vanguard_compatibility_mode implies die_on_bad_params = 0 + $options->{vanguard_compatibility_mode} and + $options->{die_on_bad_params} = 0; + + # handle the "type", "source" parameter format (does anyone use it?) + if (exists($options->{type})) { + exists($options->{source}) or croak("HTML::Template->new() called with 'type' parameter set, but no 'source'!"); + ($options->{type} eq 'filename' or $options->{type} eq 'scalarref' or + $options->{type} eq 'arrayref' or $options->{type} eq 'filehandle') or + croak("HTML::Template->new() : type parameter must be set to 'filename', 'arrayref', 'scalarref' or 'filehandle'!"); + + $options->{$options->{type}} = $options->{source}; + delete $options->{type}; + delete $options->{source}; + } + + # associate should be an array of one element if it's not + # already an array. + if (ref($options->{associate}) ne 'ARRAY') { + $options->{associate} = [ $options->{associate} ]; + } + + # path should be an array if it's not already + if (ref($options->{path}) ne 'ARRAY') { + $options->{path} = [ $options->{path} ]; + } + + # filter should be an array if it's not already + if (ref($options->{filter}) ne 'ARRAY') { + $options->{filter} = [ $options->{filter} ]; + } + + # make sure objects in associate area support param() + foreach my $object (@{$options->{associate}}) { + defined($object->can('param')) or + croak("HTML::Template->new called with associate option, containing object of type " . ref($object) . " which lacks a param() method!"); + } + + # check for syntax errors: + my $source_count = 0; + exists($options->{filename}) and $source_count++; + exists($options->{filehandle}) and $source_count++; + exists($options->{arrayref}) and $source_count++; + exists($options->{scalarref}) and $source_count++; + if ($source_count != 1) { + croak("HTML::Template->new called with multiple (or no) template sources specified! A valid call to new() has exactly one filename => 'file' OR exactly one scalarref => \\\$scalar OR exactly one arrayref => \\\@array OR exactly one filehandle => \*FH"); + } + + # do some memory debugging - this is best started as early as possible + if ($options->{memory_debug}) { + # memory_debug needs GTop + eval { require GTop; }; + croak("Could not load GTop. You must have GTop installed to use HTML::Template in memory_debug mode. The error was: $@") + if ($@); + $self->{gtop} = GTop->new(); + $self->{proc_mem} = $self->{gtop}->proc_mem($$); + print STDERR "\n### HTML::Template Memory Debug ### START ", $self->{proc_mem}->size(), "\n"; + } + + if ($options->{file_cache}) { + # make sure we have a file_cache_dir option + croak("You must specify the file_cache_dir option if you want to use file_cache.") + unless defined $options->{file_cache_dir} and + length $options->{file_cache_dir}; + + # file_cache needs some extra modules loaded + eval { require Storable; }; + croak("Could not load Storable. You must have Storable installed to use HTML::Template in file_cache mode. The error was: $@") + if ($@); + eval { require Digest::MD5; }; + croak("Could not load Digest::MD5. You must have Digest::MD5 installed to use HTML::Template in file_cache mode. The error was: $@") + if ($@); + } + + if ($options->{shared_cache}) { + # shared_cache needs some extra modules loaded + eval { require IPC::SharedCache; }; + croak("Could not load IPC::SharedCache. You must have IPC::SharedCache installed to use HTML::Template in shared_cache mode. The error was: $@") + if ($@); + + # initialize the shared cache + my %cache; + tie %cache, 'IPC::SharedCache', + ipc_key => $options->{ipc_key}, + load_callback => [\&_load_shared_cache, $self], + validate_callback => [\&_validate_shared_cache, $self], + debug => $options->{shared_cache_debug}, + ipc_mode => $options->{ipc_mode}, + max_size => $options->{ipc_max_size}, + ipc_segment_size => $options->{ipc_segment_size}; + $self->{cache} = \%cache; + } + + print STDERR "### HTML::Template Memory Debug ### POST CACHE INIT ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + # initialize data structures + $self->_init; + + print STDERR "### HTML::Template Memory Debug ### POST _INIT CALL ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + # drop the shared cache - leaving out this step results in the + # template object evading garbage collection since the callbacks in + # the shared cache tie hold references to $self! This was not easy + # to find, by the way. + delete $self->{cache} if $options->{shared_cache}; + + return $self; +} + +# an internally used new that receives its parse_stack and param_map as input +sub _new_from_loop { + my $pkg = shift; + my $self; { my %hash; $self = bless(\%hash, $pkg); } + + # the options hash + my $options = {}; + $self->{options} = $options; + + # set default parameters in options hash - a subset of the options + # valid in a normal new(). Since _new_from_loop never calls _init, + # many options have no relevance. + %$options = ( + debug => 0, + stack_debug => 0, + die_on_bad_params => 1, + associate => [], + loop_context_vars => 0, + ); + + # load in options supplied to new() + for (my $x = 0; $x <= $#_; $x += 2) { + defined($_[($x + 1)]) or croak("HTML::Template->new() called with odd number of option parameters - should be of the form option => value"); + $options->{lc($_[$x])} = $_[($x + 1)]; + } + + $self->{param_map} = $options->{param_map}; + $self->{parse_stack} = $options->{parse_stack}; + delete($options->{param_map}); + delete($options->{parse_stack}); + + return $self; +} + +# a few shortcuts to new(), of possible use... +sub new_file { + my $pkg = shift; return $pkg->new('filename', @_); +} +sub new_filehandle { + my $pkg = shift; return $pkg->new('filehandle', @_); +} +sub new_array_ref { + my $pkg = shift; return $pkg->new('arrayref', @_); +} +sub new_scalar_ref { + my $pkg = shift; return $pkg->new('scalarref', @_); +} + +# initializes all the object data structures, either from cache or by +# calling the appropriate routines. +sub _init { + my $self = shift; + my $options = $self->{options}; + + if ($options->{double_cache}) { + # try the normal cache, return if we have it. + $self->_fetch_from_cache(); + return if (defined $self->{param_map} and defined $self->{parse_stack}); + + # try the shared cache + $self->_fetch_from_shared_cache(); + + # put it in the local cache if we got it. + $self->_commit_to_cache() + if (defined $self->{param_map} and defined $self->{parse_stack}); + } elsif ($options->{double_file_cache}) { + # try the normal cache, return if we have it. + $self->_fetch_from_cache(); + return if (defined $self->{param_map} and defined $self->{parse_stack}); + + # try the file cache + $self->_fetch_from_file_cache(); + + # put it in the local cache if we got it. + $self->_commit_to_cache() + if (defined $self->{param_map} and defined $self->{parse_stack}); + } elsif ($options->{shared_cache}) { + # try the shared cache + $self->_fetch_from_shared_cache(); + } elsif ($options->{file_cache}) { + # try the file cache + $self->_fetch_from_file_cache(); + } elsif ($options->{cache}) { + # try the normal cache + $self->_fetch_from_cache(); + } + + # if we got a cache hit, return + return if (defined $self->{param_map} and defined $self->{parse_stack}); + + # if we're here, then we didn't get a cached copy, so do a full + # init. + $self->_init_template(); + $self->_parse(); + + # now that we have a full init, cache the structures if cacheing is + # on. shared cache is already cool. + if($options->{file_cache}){ + $self->_commit_to_file_cache(); + } + $self->_commit_to_cache() if (($options->{cache} + and not $options->{shared_cache} + and not $options->{file_cache}) or + ($options->{double_cache}) or + ($options->{double_file_cache})); +} + +# Caching subroutines - they handle getting and validating cache +# records from either the in-memory or shared caches. + +# handles the normal in memory cache +use vars qw( %CACHE ); +sub _fetch_from_cache { + my $self = shift; + my $options = $self->{options}; + + # return if there's no cache entry for this filename + return unless exists($options->{filename}); + my $filepath = $self->_find_file($options->{filename}); + return unless (defined($filepath) and + exists $CACHE{$filepath}); + + $options->{filepath} = $filepath; + + # validate the cache + my $mtime = $self->_mtime($filepath); + if (defined $mtime) { + # return if the mtime doesn't match the cache + if (defined($CACHE{$filepath}{mtime}) and + ($mtime != $CACHE{$filepath}{mtime})) { + $options->{cache_debug} and + print STDERR "CACHE MISS : $filepath : $mtime\n"; + return; + } + + # if the template has includes, check each included file's mtime + # and return if different + if (exists($CACHE{$filepath}{included_mtimes})) { + foreach my $filename (keys %{$CACHE{$filepath}{included_mtimes}}) { + next unless + defined($CACHE{$filepath}{included_mtimes}{$filename}); + + my $included_mtime = (stat($filename))[9]; + if ($included_mtime != $CACHE{$filepath}{included_mtimes}{$filename}) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### CACHE MISS : $filepath : INCLUDE $filename : $included_mtime\n"; + + return; + } + } + } + } + + # got a cache hit! + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### CACHE HIT : $filepath\n"; + + $self->{param_map} = $CACHE{$filepath}{param_map}; + $self->{parse_stack} = $CACHE{$filepath}{parse_stack}; + exists($CACHE{$filepath}{included_mtimes}) and + $self->{included_mtimes} = $CACHE{$filepath}{included_mtimes}; + + # clear out values from param_map from last run + $self->_normalize_options(); + $self->clear_params(); +} + +sub _commit_to_cache { + my $self = shift; + my $options = $self->{options}; + + my $filepath = $options->{filepath}; + if (not defined $filepath) { + $filepath = $self->_find_file($options->{filename}); + confess("HTML::Template->new() : Cannot open included file $options->{filename} : file not found.") + unless defined($filepath); + $options->{filepath} = $filepath; + } + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### CACHE LOAD : $filepath\n"; + + $options->{blind_cache} or + $CACHE{$filepath}{mtime} = $self->_mtime($filepath); + $CACHE{$filepath}{param_map} = $self->{param_map}; + $CACHE{$filepath}{parse_stack} = $self->{parse_stack}; + exists($self->{included_mtimes}) and + $CACHE{$filepath}{included_mtimes} = $self->{included_mtimes}; +} + +# generates MD5 from filepath to determine filename for cache file +sub _get_cache_filename { + my ($self, $filepath) = @_; + + # hash the filename ... + my $hash = Digest::MD5::md5_hex($filepath); + + # ... and build a path out of it. Using the first two charcters + # gives us 255 buckets. This means you can have 255,000 templates + # in the cache before any one directory gets over a few thousand + # files in it. That's probably pretty good for this planet. If not + # then it should be configurable. + if (wantarray) { + return (substr($hash,0,2), substr($hash,2)) + } else { + return File::Spec->join($self->{options}{file_cache_dir}, + substr($hash,0,2), substr($hash,2)); + } +} + +# handles the file cache +sub _fetch_from_file_cache { + my $self = shift; + my $options = $self->{options}; + return unless exists($options->{filename}); + + # return if there's no cache entry for this filename + my $filepath = $self->_find_file($options->{filename}); + return unless defined $filepath; + my $cache_filename = $self->_get_cache_filename($filepath); + return unless -e $cache_filename; + + eval { + $self->{record} = Storable::lock_retrieve($cache_filename); + }; + croak("HTML::Template::new() - Problem reading cache file $cache_filename (file_cache => 1) : $@") + if $@; + croak("HTML::Template::new() - Problem reading cache file $cache_filename (file_cache => 1) : $!") + unless defined $self->{record}; + + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = @{$self->{record}}; + + $options->{filepath} = $filepath; + + # validate the cache + my $mtime = $self->_mtime($filepath); + if (defined $mtime) { + # return if the mtime doesn't match the cache + if (defined($self->{mtime}) and + ($mtime != $self->{mtime})) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### FILE CACHE MISS : $filepath : $mtime\n"; + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = (undef, undef, undef, undef); + return; + } + + # if the template has includes, check each included file's mtime + # and return if different + if (exists($self->{included_mtimes})) { + foreach my $filename (keys %{$self->{included_mtimes}}) { + next unless + defined($self->{included_mtimes}{$filename}); + + my $included_mtime = (stat($filename))[9]; + if ($included_mtime != $self->{included_mtimes}{$filename}) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### FILE CACHE MISS : $filepath : INCLUDE $filename : $included_mtime\n"; + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = (undef, undef, undef, undef); + return; + } + } + } + } + + # got a cache hit! + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### FILE CACHE HIT : $filepath\n"; + + # clear out values from param_map from last run + $self->_normalize_options(); + $self->clear_params(); +} + +sub _commit_to_file_cache { + my $self = shift; + my $options = $self->{options}; + + my $filepath = $options->{filepath}; + if (not defined $filepath) { + $filepath = $self->_find_file($options->{filename}); + confess("HTML::Template->new() : Cannot open included file $options->{filename} : file not found.") + unless defined($filepath); + $options->{filepath} = $filepath; + } + + my ($cache_dir, $cache_file) = $self->_get_cache_filename($filepath); + $cache_dir = File::Spec->join($options->{file_cache_dir}, $cache_dir); + if (not -d $cache_dir) { + if (not -d $options->{file_cache_dir}) { + mkdir($options->{file_cache_dir},$options->{file_cache_dir_mode}) + or croak("HTML::Template->new() : can't mkdir $options->{file_cache_dir} (file_cache => 1): $!"); + } + mkdir($cache_dir,$options->{file_cache_dir_mode}) + or croak("HTML::Template->new() : can't mkdir $cache_dir (file_cache => 1): $!"); + } + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### FILE CACHE LOAD : $options->{filepath}\n"; + + my $result; + eval { + $result = Storable::lock_store([ $self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack} ], + scalar File::Spec->join($cache_dir, $cache_file) + ); + }; + croak("HTML::Template::new() - Problem writing cache file $cache_dir/$cache_file (file_cache => 1) : $@") + if $@; + croak("HTML::Template::new() - Problem writing cache file $cache_dir/$cache_file (file_cache => 1) : $!") + unless defined $result; +} + +# Shared cache routines. +sub _fetch_from_shared_cache { + my $self = shift; + my $options = $self->{options}; + + my $filepath = $self->_find_file($options->{filename}); + return unless defined $filepath; + + # fetch from the shared cache. + $self->{record} = $self->{cache}{$filepath}; + + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = @{$self->{record}} + if defined($self->{record}); + + $options->{cache_debug} and defined($self->{record}) and print STDERR "### HTML::Template Cache Debug ### CACHE HIT : $filepath\n"; + # clear out values from param_map from last run + $self->_normalize_options(), $self->clear_params() + if (defined($self->{record})); + delete($self->{record}); + + return $self; +} + +sub _validate_shared_cache { + my ($self, $filename, $record) = @_; + my $options = $self->{options}; + + $options->{shared_cache_debug} and print STDERR "### HTML::Template Cache Debug ### SHARED CACHE VALIDATE : $filename\n"; + + return 1 if $options->{blind_cache}; + + my ($c_mtime, $included_mtimes, $param_map, $parse_stack) = @$record; + + # if the modification time has changed return false + my $mtime = $self->_mtime($filename); + if (defined $mtime and defined $c_mtime + and $mtime != $c_mtime) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### SHARED CACHE MISS : $filename : $mtime\n"; + return 0; + } + + # if the template has includes, check each included file's mtime + # and return false if different + if (defined $mtime and defined $included_mtimes) { + foreach my $fname (keys %$included_mtimes) { + next unless defined($included_mtimes->{$fname}); + if ($included_mtimes->{$fname} != (stat($fname))[9]) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### SHARED CACHE MISS : $filename : INCLUDE $fname\n"; + return 0; + } + } + } + + # all done - return true + return 1; +} + +sub _load_shared_cache { + my ($self, $filename) = @_; + my $options = $self->{options}; + my $cache = $self->{cache}; + + $self->_init_template(); + $self->_parse(); + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### SHARED CACHE LOAD : $options->{filepath}\n"; + + print STDERR "### HTML::Template Memory Debug ### END CACHE LOAD ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + return [ $self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack} ]; +} + +# utility function - given a filename performs documented search and +# returns a full path of undef if the file cannot be found. +sub _find_file { + my ($self, $filename, $extra_path) = @_; + my $options = $self->{options}; + my $filepath; + + # first check for a full path + return File::Spec->canonpath($filename) + if (File::Spec->file_name_is_absolute($filename) and (-e $filename)); + + # try the extra_path if one was specified + if (defined($extra_path)) { + $extra_path->[$#{$extra_path}] = $filename; + $filepath = File::Spec->canonpath(File::Spec->catfile(@$extra_path)); + return File::Spec->canonpath($filepath) if -e $filepath; + } + + # try pre-prending HTML_Template_Root + if (exists($ENV{HTML_TEMPLATE_ROOT})) { + $filepath = File::Spec->catfile($ENV{HTML_TEMPLATE_ROOT}, $filename); + return File::Spec->canonpath($filepath) if -e $filepath; + } + + # try "path" option list.. + foreach my $path (@{$options->{path}}) { + $filepath = File::Spec->canonpath(File::Spec->catfile($path, $filename)); + return File::Spec->canonpath($filepath) if -e $filepath; + } + + # try even a relative path from the current directory... + return File::Spec->canonpath($filename) if -e $filename; + + return undef; +} + +# utility function - computes the mtime for $filename +sub _mtime { + my ($self, $filepath) = @_; + my $options = $self->{options}; + + return(undef) if ($options->{blind_cache}); + + # make sure it still exists in the filesystem + (-r $filepath) or Carp::confess("HTML::Template : template file $filepath does not exist or is unreadable."); + + # get the modification time + return (stat(_))[9]; +} + +# utility function - enforces new() options across LOOPs that have +# come from a cache. Otherwise they would have stale options hashes. +sub _normalize_options { + my $self = shift; + my $options = $self->{options}; + + my @pstacks = ($self->{parse_stack}); + while(@pstacks) { + my $pstack = pop(@pstacks); + foreach my $item (@$pstack) { + next unless (ref($item) eq 'HTML::Template::LOOP'); + foreach my $template (values %{$item->[HTML::Template::LOOP::TEMPLATE_HASH]}) { + # must be the same list as the call to _new_from_loop... + $template->{options}{debug} = $options->{debug}; + $template->{options}{stack_debug} = $options->{stack_debug}; + $template->{options}{die_on_bad_params} = $options->{die_on_bad_params}; + $template->{options}{case_sensitive} = $options->{case_sensitive}; + + push(@pstacks, $template->{parse_stack}); + } + } + } +} + +# initialize the template buffer +sub _init_template { + my $self = shift; + my $options = $self->{options}; + + print STDERR "### HTML::Template Memory Debug ### START INIT_TEMPLATE ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + if (exists($options->{filename})) { + my $filepath = $options->{filepath}; + if (not defined $filepath) { + $filepath = $self->_find_file($options->{filename}); + confess("HTML::Template->new() : Cannot open included file $options->{filename} : file not found.") + unless defined($filepath); + # we'll need this for future reference - to call stat() for example. + $options->{filepath} = $filepath; + } + + confess("HTML::Template->new() : Cannot open included file $options->{filename} : $!") + unless defined(open(TEMPLATE, $filepath)); + $self->{mtime} = $self->_mtime($filepath); + + # read into scalar, note the mtime for the record + $self->{template} = ""; + while (read(TEMPLATE, $self->{template}, 10240, length($self->{template}))) {} + close(TEMPLATE); + + } elsif (exists($options->{scalarref})) { + # copy in the template text + $self->{template} = ${$options->{scalarref}}; + + delete($options->{scalarref}); + } elsif (exists($options->{arrayref})) { + # if we have an array ref, join and store the template text + $self->{template} = join("", @{$options->{arrayref}}); + + delete($options->{arrayref}); + } elsif (exists($options->{filehandle})) { + # just read everything in in one go + local $/ = undef; + $self->{template} = readline($options->{filehandle}); + + delete($options->{filehandle}); + } else { + confess("HTML::Template : Need to call new with filename, filehandle, scalarref or arrayref parameter specified."); + } + + print STDERR "### HTML::Template Memory Debug ### END INIT_TEMPLATE ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + # handle filters if necessary + $self->_call_filters(\$self->{template}) if @{$options->{filter}}; + + return $self; +} + +# handle calling user defined filters +sub _call_filters { + my $self = shift; + my $template_ref = shift; + my $options = $self->{options}; + + my ($format, $sub); + foreach my $filter (@{$options->{filter}}) { + croak("HTML::Template->new() : bad value set for filter parameter - must be a code ref or a hash ref.") + unless ref $filter; + + # translate into CODE->HASH + $filter = { 'format' => 'scalar', 'sub' => $filter } + if (ref $filter eq 'CODE'); + + if (ref $filter eq 'HASH') { + $format = $filter->{'format'}; + $sub = $filter->{'sub'}; + + # check types and values + croak("HTML::Template->new() : bad value set for filter parameter - hash must contain \"format\" key and \"sub\" key.") + unless defined $format and defined $sub; + croak("HTML::Template->new() : bad value set for filter parameter - \"format\" must be either 'array' or 'scalar'") + unless $format eq 'array' or $format eq 'scalar'; + croak("HTML::Template->new() : bad value set for filter parameter - \"sub\" must be a code ref") + unless ref $sub and ref $sub eq 'CODE'; + + # catch errors + eval { + if ($format eq 'scalar') { + # call + $sub->($template_ref); + } else { + # modulate + my @array = map { $_."\n" } split("\n", $$template_ref); + # call + $sub->(\@array); + # demodulate + $$template_ref = join("", @array); + } + }; + croak("HTML::Template->new() : fatal error occured during filter call: $@") if $@; + } else { + croak("HTML::Template->new() : bad value set for filter parameter - must be code ref or hash ref"); + } + } + # all done + return $template_ref; +} + +# _parse sifts through a template building up the param_map and +# parse_stack structures. +# +# The end result is a Template object that is fully ready for +# output(). +sub _parse { + my $self = shift; + my $options = $self->{options}; + + $options->{debug} and print STDERR "### HTML::Template Debug ### In _parse:\n"; + + # setup the stacks and maps - they're accessed by typeglobs that + # reference the top of the stack. They are masked so that a loop + # can transparently have its own versions. + use vars qw(@pstack %pmap @ifstack @ucstack %top_pmap); + local (*pstack, *ifstack, *pmap, *ucstack, *top_pmap); + + # the pstack is the array of scalar refs (plain text from the + # template file), VARs, LOOPs, IFs and ELSEs that output() works on + # to produce output. Looking at output() should make it clear what + # _parse is trying to accomplish. + my @pstacks = ([]); + *pstack = $pstacks[0]; + $self->{parse_stack} = $pstacks[0]; + + # the pmap binds names to VARs, LOOPs and IFs. It allows param() to + # access the right variable. NOTE: output() does not look at the + # pmap at all! + my @pmaps = ({}); + *pmap = $pmaps[0]; + *top_pmap = $pmaps[0]; + $self->{param_map} = $pmaps[0]; + + # the ifstack is a temporary stack containing pending ifs and elses + # waiting for a /if. + my @ifstacks = ([]); + *ifstack = $ifstacks[0]; + + # the ucstack is a temporary stack containing conditions that need + # to be bound to param_map entries when their block is finished. + # This happens when a conditional is encountered before any other + # reference to its NAME. Since a conditional can reference VARs and + # LOOPs it isn't possible to make the link right away. + my @ucstacks = ([]); + *ucstack = $ucstacks[0]; + + # the loopstack is another temp stack for closing loops. unlike + # those above it doesn't get scoped inside loops, therefore it + # doesn't need the typeglob magic. + my @loopstack = (); + + # the fstack is a stack of filenames and counters that keeps track + # of which file we're in and where we are in it. This allows + # accurate error messages even inside included files! + # fcounter, fmax and fname are aliases for the current file's info + use vars qw($fcounter $fname $fmax); + local (*fcounter, *fname, *fmax); + + my @fstack = ([$options->{filepath} || "/fake/path/for/non/file/template", + 1, + scalar @{[$self->{template} =~ m/(\n)/g]} + 1 + ]); + (*fname, *fcounter, *fmax) = \ ( @{$fstack[0]} ); + + my $NOOP = HTML::Template::NOOP->new(); + my $ESCAPE = HTML::Template::ESCAPE->new(); + my $URLESCAPE = HTML::Template::URLESCAPE->new(); + + # all the tags that need NAMEs: + my %need_names = map { $_ => 1 } + qw(TMPL_VAR TMPL_LOOP TMPL_IF TMPL_UNLESS TMPL_INCLUDE); + + # variables used below that don't need to be my'd in the loop + my ($name, $which, $escape); + + # handle the old vanguard format + $options->{vanguard_compatibility_mode} and + $self->{template} =~ s/%([-\w\/\.+]+)%/<TMPL_VAR NAME=$1>/g; + + # now split up template on '<', leaving them in + my @chunks = split(m/(?=<)/, $self->{template}); + + # all done with template + delete $self->{template}; + + # loop through chunks, filling up pstack + my $last_chunk = $#chunks; + CHUNK: for (my $chunk_number = 0; + $chunk_number <= $last_chunk; + $chunk_number++) { + next unless defined $chunks[$chunk_number]; + my $chunk = $chunks[$chunk_number]; + + # a general regex to match any and all TMPL_* tags + if ($chunk =~ /^< + (?:!--\s*)? + ( + \/?[Tt][Mm][Pp][Ll]_ + (?: + (?:[Vv][Aa][Rr]) + | + (?:[Ll][Oo][Oo][Pp]) + | + (?:[Ii][Ff]) + | + (?:[Ee][Ll][Ss][Ee]) + | + (?:[Uu][Nn][Ll][Ee][Ss][Ss]) + | + (?:[Ii][Nn][Cc][Ll][Uu][Dd][Ee]) + ) + ) # $1 => $which - start of the tag + + \s* + + # ESCAPE attribute + (?: + [Ee][Ss][Cc][Aa][Pp][Ee] + \s*=\s* + (?: + ( 0 | (?:"0") | (?:'0') ) # $2 => ESCAPE off + | + ( 1 | (?:"1") | (?:'1') | + (?:[Hh][Tt][Mm][Ll]) | + (?:"[Hh][Tt][Mm][Ll]") | + (?:'[Hh][Tt][Mm][Ll]') | + (?:[Uu][Rr][Ll]) | + (?:"[Uu][Rr][Ll]") | + (?:'[Uu][Rr][Ll]') | + ) # $3 => ESCAPE on + ) + )* # allow multiple ESCAPEs + + \s* + + # NAME attribute + (?: + (?: + [Nn][Aa][Mm][Ee] + \s*=\s* + )? + (?: + "([^">]*)" # $4 => double-quoted NAME value " + | + '([^'>]*)' # $5 => single-quoted NAME value + | + ([^\s=>]*) # $6 => unquoted NAME value + ) + )? + + \s* + + # ESCAPE attribute + (?: + [Ee][Ss][Cc][Aa][Pp][Ee] + \s*=\s* + (?: + ( 0 | (?:"0") | (?:'0') ) # $7 => ESCAPE off + | + ( 1 | (?:"1") | (?:'1') | + (?:[Hh][Tt][Mm][Ll]) | + (?:"[Hh][Tt][Mm][Ll]") | + (?:'[Hh][Tt][Mm][Ll]') | + (?:[Uu][Rr][Ll]) | + (?:"[Uu][Rr][Ll]") | + (?:'[Uu][Rr][Ll]') | + ) # $8 => ESCAPE on + ) + )* # allow multiple ESCAPEs + + \s* + + (?:--)?> + (.*) # $9 => $post - text that comes after the tag + $/sx) { + + $which = uc($1); # which tag is it + + $escape = $3 || $8; + $escape = 0 if $2 || $7; # ESCAPE=0 + $escape = 0 unless defined($escape); + + # what name for the tag? undef for a /tag at most, one of the + # following three will be defined + undef $name; + $name = $4 if defined($4); + $name = $5 if defined($5); + $name = $6 if defined($6); + + # allow mixed case in filenames, otherwise flatten + $name = lc($name) unless ($which eq 'TMPL_INCLUDE' or $options->{case_sensitive}); + + my $post = $9; # what comes after on the line + + # die if we need a name and didn't get one + die "HTML::Template->new() : No NAME given to a $which tag at $fname : line $fcounter." if (!defined($name) and $need_names{$which}); + + # die if we got an escape but can't use one + die "HTML::Template->new() : ESCAPE option invalid in a $which tag at $fname : line $fcounter." if ( $escape and ($which ne 'TMPL_VAR')); + + # take actions depending on which tag found + if ($which eq 'TMPL_VAR') { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : parsed VAR $name\n"; + + # if we already have this var, then simply link to the existing + # HTML::Template::VAR, else create a new one. + my $var; + if (exists $pmap{$name}) { + $var = $pmap{$name}; + (ref($var) eq 'HTML::Template::VAR') or + die "HTML::Template->new() : Already used param name $name as a TMPL_LOOP, found in a TMPL_VAR at $fname : line $fcounter."; + } else { + $var = HTML::Template::VAR->new(); + $pmap{$name} = $var; + $top_pmap{$name} = HTML::Template::VAR->new() + if $options->{global_vars} and not exists $top_pmap{$name}; + } + + # if ESCAPE was set, push an ESCAPE op on the stack before + # the variable. output will handle the actual work. + if ($escape) { + if ($escape =~ /^"?[Uu][Rr][Ll]"?$/) { + push(@pstack, $URLESCAPE); + } else { + push(@pstack, $ESCAPE); + } + } + + push(@pstack, $var); + + } elsif ($which eq 'TMPL_LOOP') { + # we've got a loop start + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : LOOP $name start\n"; + + # if we already have this loop, then simply link to the existing + # HTML::Template::LOOP, else create a new one. + my $loop; + if (exists $pmap{$name}) { + $loop = $pmap{$name}; + (ref($loop) eq 'HTML::Template::LOOP') or + die "HTML::Template->new() : Already used param name $name as a TMPL_VAR, TMPL_IF or TMPL_UNLESS, found in a TMP_LOOP at $fname : line $fcounter!"; + + } else { + # store the results in a LOOP object - actually just a + # thin wrapper around another HTML::Template object. + $loop = HTML::Template::LOOP->new(); + $pmap{$name} = $loop; + } + + # get it on the loopstack, pstack of the enclosing block + push(@pstack, $loop); + push(@loopstack, [$loop, $#pstack]); + + # magic time - push on a fresh pmap and pstack, adjust the typeglobs. + # this gives the loop a separate namespace (i.e. pmap and pstack). + push(@pstacks, []); + *pstack = $pstacks[$#pstacks]; + push(@pmaps, {}); + *pmap = $pmaps[$#pmaps]; + push(@ifstacks, []); + *ifstack = $ifstacks[$#ifstacks]; + push(@ucstacks, []); + *ucstack = $ucstacks[$#ucstacks]; + + # auto-vivify __FIRST__, __LAST__ and __INNER__ if + # loop_context_vars is set. Otherwise, with + # die_on_bad_params set output() will might cause errors + # when it tries to set them. + if ($options->{loop_context_vars}) { + $pmap{__first__} = HTML::Template::VAR->new(); + $pmap{__inner__} = HTML::Template::VAR->new(); + $pmap{__last__} = HTML::Template::VAR->new(); + $pmap{__odd__} = HTML::Template::VAR->new(); + } + + } elsif ($which eq '/TMPL_LOOP') { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : LOOP end\n"; + + my $loopdata = pop(@loopstack); + die "HTML::Template->new() : found </TMPL_LOOP> with no matching <TMPL_LOOP> at $fname : line $fcounter!" unless defined $loopdata; + + my ($loop, $starts_at) = @$loopdata; + + # resolve pending conditionals + foreach my $uc (@ucstack) { + my $var = $uc->[HTML::Template::COND::VARIABLE]; + if (exists($pmap{$var})) { + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } else { + $pmap{$var} = HTML::Template::VAR->new(); + $top_pmap{$var} = HTML::Template::VAR->new() + if $options->{global_vars} and not exists $top_pmap{$var}; + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } + if (ref($pmap{$var}) eq 'HTML::Template::VAR') { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_VAR; + } else { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_LOOP; + } + } + + # get pmap and pstack for the loop, adjust the typeglobs to + # the enclosing block. + my $param_map = pop(@pmaps); + *pmap = $pmaps[$#pmaps]; + my $parse_stack = pop(@pstacks); + *pstack = $pstacks[$#pstacks]; + + scalar(@ifstack) and die "HTML::Template->new() : Dangling <TMPL_IF> or <TMPL_UNLESS> in loop ending at $fname : line $fcounter."; + pop(@ifstacks); + *ifstack = $ifstacks[$#ifstacks]; + pop(@ucstacks); + *ucstack = $ucstacks[$#ucstacks]; + + # instantiate the sub-Template, feeding it parse_stack and + # param_map. This means that only the enclosing template + # does _parse() - sub-templates get their parse_stack and + # param_map fed to them already filled in. + $loop->[HTML::Template::LOOP::TEMPLATE_HASH]{$starts_at} + = HTML::Template->_new_from_loop( + parse_stack => $parse_stack, + param_map => $param_map, + debug => $options->{debug}, + die_on_bad_params => $options->{die_on_bad_params}, + loop_context_vars => $options->{loop_context_vars}, + case_sensitive => $options->{case_sensitive}, + ); + + } elsif ($which eq 'TMPL_IF' or $which eq 'TMPL_UNLESS' ) { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : $which $name start\n"; + + # if we already have this var, then simply link to the existing + # HTML::Template::VAR/LOOP, else defer the mapping + my $var; + if (exists $pmap{$name}) { + $var = $pmap{$name}; + } else { + $var = $name; + } + + # connect the var to a conditional + my $cond = HTML::Template::COND->new($var); + if ($which eq 'TMPL_IF') { + $cond->[HTML::Template::COND::WHICH] = HTML::Template::COND::WHICH_IF; + $cond->[HTML::Template::COND::JUMP_IF_TRUE] = 0; + } else { + $cond->[HTML::Template::COND::WHICH] = HTML::Template::COND::WHICH_UNLESS; + $cond->[HTML::Template::COND::JUMP_IF_TRUE] = 1; + } + + # push unconnected conditionals onto the ucstack for + # resolution later. Otherwise, save type information now. + if ($var eq $name) { + push(@ucstack, $cond); + } else { + if (ref($var) eq 'HTML::Template::VAR') { + $cond->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_VAR; + } else { + $cond->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_LOOP; + } + } + + # push what we've got onto the stacks + push(@pstack, $cond); + push(@ifstack, $cond); + + } elsif ($which eq '/TMPL_IF' or $which eq '/TMPL_UNLESS') { + $options->{debug} and print STDERR "### HTML::Template Debug ###$fname : line $fcounter : $which end\n"; + + my $cond = pop(@ifstack); + die "HTML::Template->new() : found </${which}> with no matching <TMPL_IF> at $fname : line $fcounter." unless defined $cond; + if ($which eq '/TMPL_IF') { + die "HTML::Template->new() : found </TMPL_IF> incorrectly terminating a <TMPL_UNLESS> (use </TMPL_UNLESS>) at $fname : line $fcounter.\n" + if ($cond->[HTML::Template::COND::WHICH] == HTML::Template::COND::WHICH_UNLESS); + } else { + die "HTML::Template->new() : found </TMPL_UNLESS> incorrectly terminating a <TMPL_IF> (use </TMPL_IF>) at $fname : line $fcounter.\n" + if ($cond->[HTML::Template::COND::WHICH] == HTML::Template::COND::WHICH_IF); + } + + # connect the matching to this "address" - place a NOOP to + # hold the spot. This allows output() to treat an IF in the + # assembler-esque "Conditional Jump" mode. + push(@pstack, $NOOP); + $cond->[HTML::Template::COND::JUMP_ADDRESS] = $#pstack; + + } elsif ($which eq 'TMPL_ELSE') { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : ELSE\n"; + + my $cond = pop(@ifstack); + die "HTML::Template->new() : found <TMPL_ELSE> with no matching <TMPL_IF> or <TMPL_UNLESS> at $fname : line $fcounter." unless defined $cond; + + + my $else = HTML::Template::COND->new($cond->[HTML::Template::COND::VARIABLE]); + $else->[HTML::Template::COND::WHICH] = $cond->[HTML::Template::COND::WHICH]; + $else->[HTML::Template::COND::JUMP_IF_TRUE] = not $cond->[HTML::Template::COND::JUMP_IF_TRUE]; + + # need end-block resolution? + if (defined($cond->[HTML::Template::COND::VARIABLE_TYPE])) { + $else->[HTML::Template::COND::VARIABLE_TYPE] = $cond->[HTML::Template::COND::VARIABLE_TYPE]; + } else { + push(@ucstack, $else); + } + + push(@pstack, $else); + push(@ifstack, $else); + + # connect the matching to this "address" - thus the if, + # failing jumps to the ELSE address. The else then gets + # elaborated, and of course succeeds. On the other hand, if + # the IF fails and falls though, output will reach the else + # and jump to the /if address. + $cond->[HTML::Template::COND::JUMP_ADDRESS] = $#pstack; + + } elsif ($which eq 'TMPL_INCLUDE') { + # handle TMPL_INCLUDEs + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : INCLUDE $name \n"; + + # no includes here, bub + $options->{no_includes} and croak("HTML::Template : Illegal attempt to use TMPL_INCLUDE in template file : (no_includes => 1)"); + + my $filename = $name; + + # look for the included file... + my $filepath; + if ($options->{search_path_on_include}) { + $filepath = $self->_find_file($filename); + } else { + $filepath = $self->_find_file($filename, + [File::Spec->splitdir($fstack[-1][0])] + ); + } + die "HTML::Template->new() : Cannot open included file $filename : file not found." + unless defined($filepath); + die "HTML::Template->new() : Cannot open included file $filename : $!" + unless defined(open(TEMPLATE, $filepath)); + + # read into the array + my $included_template = ""; + while(read(TEMPLATE, $included_template, 10240, length($included_template))) {} + close(TEMPLATE); + + # call filters if necessary + $self->_call_filters(\$included_template) if @{$options->{filter}}; + + if ($included_template) { # not empty + # handle the old vanguard format - this needs to happen here + # since we're not about to do a next CHUNKS. + $options->{vanguard_compatibility_mode} and + $included_template =~ s/%([-\w\/\.+]+)%/<TMPL_VAR NAME=$1>/g; + + # collect mtimes for included files + if ($options->{cache} and !$options->{blind_cache}) { + $self->{included_mtimes}{$filepath} = (stat($filepath))[9]; + } + + # adjust the fstack to point to the included file info + push(@fstack, [$filepath, 1, + scalar @{[$included_template =~ m/(\n)/g]} + 1]); + (*fname, *fcounter, *fmax) = \ ( @{$fstack[$#fstack]} ); + + # make sure we aren't infinitely recursing + die "HTML::Template->new() : likely recursive includes - parsed $options->{max_includes} files deep and giving up (set max_includes higher to allow deeper recursion)." if ($options->{max_includes} and (scalar(@fstack) > $options->{max_includes})); + + # stick the remains of this chunk onto the bottom of the + # included text. + $included_template .= $post; + $post = undef; + + # move the new chunks into place. + splice(@chunks, $chunk_number, 1, + split(m/(?=<)/, $included_template)); + + # recalculate stopping point + $last_chunk = $#chunks; + + # start in on the first line of the included text - nothing + # else to do on this line. + $chunk = $chunks[$chunk_number]; + + redo CHUNK; + } + } else { + # zuh!? + die "HTML::Template->new() : Unknown or unmatched TMPL construct at $fname : line $fcounter."; + } + # push the rest after the tag + if (defined($post)) { + if (ref($pstack[$#pstack]) eq 'SCALAR') { + ${$pstack[$#pstack]} .= $post; + } else { + push(@pstack, \$post); + } + } + } else { # just your ordinary markup + # make sure we didn't reject something TMPL_* but badly formed + if ($options->{strict}) { + die "HTML::Template->new() : Syntax error in <TMPL_*> tag at $fname : $fcounter." if ($chunk =~ /<(?:!--\s*)?\/?[Tt][Mm][Pp][Ll]_/); + } + + # push the rest and get next chunk + if (defined($chunk)) { + if (ref($pstack[$#pstack]) eq 'SCALAR') { + ${$pstack[$#pstack]} .= $chunk; + } else { + push(@pstack, \$chunk); + } + } + } + # count newlines in chunk and advance line count + $fcounter += scalar(@{[$chunk =~ m/(\n)/g]}); + # if we just crossed the end of an included file + # pop off the record and re-alias to the enclosing file's info + pop(@fstack), (*fname, *fcounter, *fmax) = \ ( @{$fstack[$#fstack]} ) + if ($fcounter > $fmax); + + } # next CHUNK + + # make sure we don't have dangling IF or LOOP blocks + scalar(@ifstack) and die "HTML::Template->new() : At least one <TMPL_IF> or <TMPL_UNLESS> not terminated at end of file!"; + scalar(@loopstack) and die "HTML::Template->new() : At least one <TMPL_LOOP> not terminated at end of file!"; + + # resolve pending conditionals + foreach my $uc (@ucstack) { + my $var = $uc->[HTML::Template::COND::VARIABLE]; + if (exists($pmap{$var})) { + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } else { + $pmap{$var} = HTML::Template::VAR->new(); + $top_pmap{$var} = HTML::Template::VAR->new() + if $options->{global_vars} and not exists $top_pmap{$var}; + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } + if (ref($pmap{$var}) eq 'HTML::Template::VAR') { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_VAR; + } else { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_LOOP; + } + } + + # want a stack dump? + if ($options->{stack_debug}) { + require 'Data/Dumper.pm'; + print STDERR "### HTML::Template _param Stack Dump ###\n\n", Data::Dumper::Dumper($self->{parse_stack}), "\n"; + } + + # get rid of filters - they cause runtime errors if Storable tries + # to store them. This can happen under global_vars. + delete $options->{filter}; +} + +# a recursive sub that associates each loop with the loops above +# (treating the top-level as a loop) +sub _globalize_vars { + my $self = shift; + + # associate with the loop (and top-level templates) above in the tree. + push(@{$self->{options}{associate}}, @_); + + # recurse down into the template tree, adding ourself to the end of + # list. + push(@_, $self); + map { $_->_globalize_vars(@_) } + map {values %{$_->[HTML::Template::LOOP::TEMPLATE_HASH]}} + grep { ref($_) eq 'HTML::Template::LOOP'} @{$self->{parse_stack}}; +} + +# method used to recursively un-hook associate +sub _unglobalize_vars { + my $self = shift; + + # disassociate + $self->{options}{associate} = undef; + + # recurse down into the template tree disassociating + map { $_->_unglobalize_vars() } + map {values %{$_->[HTML::Template::LOOP::TEMPLATE_HASH]}} + grep { ref($_) eq 'HTML::Template::LOOP'} @{$self->{parse_stack}}; +} + +=head2 param() + +param() can be called in a number of ways + +1) To return a list of parameters in the template : + + my @parameter_names = $self->param(); + + +2) To return the value set to a param : + + my $value = $self->param('PARAM'); + +3) To set the value of a parameter : + + # For simple TMPL_VARs: + $self->param(PARAM => 'value'); + + # with a subroutine reference that gets called to get the value + # of the scalar. The sub will recieve the template object as a + # parameter. + $self->param(PARAM => sub { return 'value' }); + + # And TMPL_LOOPs: + $self->param(LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ] + ); + +4) To set the value of a a number of parameters : + + # For simple TMPL_VARs: + $self->param(PARAM => 'value', + PARAM2 => 'value' + ); + + # And with some TMPL_LOOPs: + $self->param(PARAM => 'value', + PARAM2 => 'value', + LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ], + ANOTHER_LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ] + ); + +5) To set the value of a a number of parameters using a hash-ref : + + $self->param( + { + PARAM => 'value', + PARAM2 => 'value', + LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ], + ANOTHER_LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ] + } + ); + +=cut + + +sub param { + my $self = shift; + my $options = $self->{options}; + my $param_map = $self->{param_map}; + + # the no-parameter case - return list of parameters in the template. + return keys(%$param_map) unless scalar(@_); + + my $first = shift; + my $type = ref $first; + + # the one-parameter case - could be a parameter value request or a + # hash-ref. + if (!scalar(@_) and !length($type)) { + my $param = $options->{case_sensitive} ? $first : lc $first; + + # check for parameter existence + $options->{die_on_bad_params} and !exists($param_map->{$param}) and + croak("HTML::Template : Attempt to get nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params set => 1)"); + + return undef unless (exists($param_map->{$param}) and + defined($param_map->{$param})); + + return ${$param_map->{$param}} if + (ref($param_map->{$param}) eq 'HTML::Template::VAR'); + return $param_map->{$param}[HTML::Template::LOOP::PARAM_SET]; + } + + if (!scalar(@_)) { + croak("HTML::Template->param() : Single reference arg to param() must be a hash-ref! You gave me a $type.") + unless $type eq 'HASH' or + (ref($first) and UNIVERSAL::isa($first, 'HASH')); + push(@_, %$first); + } else { + unshift(@_, $first); + } + + croak("HTML::Template->param() : You gave me an odd number of parameters to param()!") + unless ((@_ % 2) == 0); + + # strangely, changing this to a "while(@_) { shift, shift }" type + # loop causes perl 5.004_04 to die with some nonsense about a + # read-only value. + for (my $x = 0; $x <= $#_; $x += 2) { + my $param = $options->{case_sensitive} ? $_[$x] : lc $_[$x]; + my $value = $_[($x + 1)]; + + # check that this param exists in the template + $options->{die_on_bad_params} and !exists($param_map->{$param}) and + croak("HTML::Template : Attempt to set nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1)"); + + # if we're not going to die from bad param names, we need to ignore + # them... + next unless (exists($param_map->{$param})); + + # figure out what we've got, taking special care to allow for + # objects that are compatible underneath. + my $value_type = ref($value); + if (defined($value_type) and length($value_type) and ($value_type eq 'ARRAY' or ((ref($value) !~ /^(CODE)|(HASH)|(SCALAR)$/) and $value->isa('ARRAY')))) { + (ref($param_map->{$param}) eq 'HTML::Template::LOOP') or + croak("HTML::Template::param() : attempt to set parameter '$param' with an array ref - parameter is not a TMPL_LOOP!"); + $param_map->{$param}[HTML::Template::LOOP::PARAM_SET] = [@{$value}]; + } else { + (ref($param_map->{$param}) eq 'HTML::Template::VAR') or + croak("HTML::Template::param() : attempt to set parameter '$param' with a scalar - parameter is not a TMPL_VAR!"); + ${$param_map->{$param}} = $value; + } + } +} + +=pod + +=head2 clear_params() + +Sets all the parameters to undef. Useful internally, if nowhere else! + +=cut + +sub clear_params { + my $self = shift; + my $type; + foreach my $name (keys %{$self->{param_map}}) { + $type = ref($self->{param_map}{$name}); + undef(${$self->{param_map}{$name}}) + if ($type eq 'HTML::Template::VAR'); + undef($self->{param_map}{$name}[HTML::Template::LOOP::PARAM_SET]) + if ($type eq 'HTML::Template::LOOP'); + } +} + + +# obsolete implementation of associate +sub associateCGI { + my $self = shift; + my $cgi = shift; + (ref($cgi) eq 'CGI') or + croak("Warning! non-CGI object was passed to HTML::Template::associateCGI()!\n"); + push(@{$self->{options}{associate}}, $cgi); + return 1; +} + + +=head2 output() + +output() returns the final result of the template. In most situations +you'll want to print this, like: + + print $template->output(); + +When output is called each occurrence of <TMPL_VAR NAME=name> is +replaced with the value assigned to "name" via param(). If a named +parameter is unset it is simply replaced with ''. <TMPL_LOOPS> are +evaluated once per parameter set, accumlating output on each pass. + +Calling output() is guaranteed not to change the state of the +Template object, in case you were wondering. This property is mostly +important for the internal implementation of loops. + +You may optionally supply a filehandle to print to automatically as +the template is generated. This may improve performance and lower +memory consumption. Example: + + $template->output(print_to => *STDOUT); + +The return value is undefined when using the "print_to" option. + +=cut + +use vars qw(%URLESCAPE_MAP); +sub output { + my $self = shift; + my $options = $self->{options}; + + croak("HTML::Template->output() : You gave me an odd number of parameters to output()!") + unless ((@_ % 2) == 0); + my %args = @_; + + print STDERR "### HTML::Template Memory Debug ### START OUTPUT ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + $options->{debug} and print STDERR "### HTML::Template Debug ### In output\n"; + + # want a stack dump? + if ($options->{stack_debug}) { + require 'Data/Dumper.pm'; + print STDERR "### HTML::Template output Stack Dump ###\n\n", Data::Dumper::Dumper($self->{parse_stack}), "\n"; + } + + # globalize vars - this happens here to localize the circular + # references created by global_vars. + $self->_globalize_vars() if ($options->{global_vars}); + + # support the associate magic, searching for undefined params and + # attempting to fill them from the associated objects. + if (scalar(@{$options->{associate}})) { + # prepare case-mapping hashes to do case-insensitive matching + # against associated objects. This allows CGI.pm to be + # case-sensitive and still work with asssociate. + my (%case_map, $lparam); + foreach my $associated_object (@{$options->{associate}}) { + # what a hack! This should really be optimized out for case_sensitive. + if ($options->{case_sensitive}) { + map { + $case_map{$associated_object}{$_} = $_ + } $associated_object->param(); + } else { + map { + $case_map{$associated_object}{lc($_)} = $_ + } $associated_object->param(); + } + } + + foreach my $param (keys %{$self->{param_map}}) { + unless (defined($self->param($param))) { + OBJ: foreach my $associated_object (reverse @{$options->{associate}}) { + $self->param($param, scalar $associated_object->param($case_map{$associated_object}{$param})), last OBJ + if (exists($case_map{$associated_object}{$param})); + } + } + } + } + + use vars qw($line @parse_stack); local(*line, *parse_stack); + + # walk the parse stack, accumulating output in $result + *parse_stack = $self->{parse_stack}; + my $result = ''; + + tie $result, 'HTML::Template::PRINTSCALAR', $args{print_to} + if defined $args{print_to} and not tied $args{print_to}; + + my $type; + my $parse_stack_length = $#parse_stack; + for (my $x = 0; $x <= $parse_stack_length; $x++) { + *line = \$parse_stack[$x]; + $type = ref($line); + + if ($type eq 'SCALAR') { + $result .= $$line; + } elsif ($type eq 'HTML::Template::VAR' and ref($$line) eq 'CODE') { + defined($$line) and $result .= $$line->($self); + } elsif ($type eq 'HTML::Template::VAR') { + defined($$line) and $result .= $$line; + } elsif ($type eq 'HTML::Template::LOOP') { + if (defined($line->[HTML::Template::LOOP::PARAM_SET])) { + eval { $result .= $line->output($x, $options->{loop_context_vars}); }; + croak("HTML::Template->output() : fatal error in loop output : $@") + if $@; + } + } elsif ($type eq 'HTML::Template::COND') { + if ($line->[HTML::Template::COND::JUMP_IF_TRUE]) { + if ($line->[HTML::Template::COND::VARIABLE_TYPE] == HTML::Template::COND::VARIABLE_TYPE_VAR) { + if (defined ${$line->[HTML::Template::COND::VARIABLE]}) { + if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if ${$line->[HTML::Template::COND::VARIABLE]}->($self); + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if ${$line->[HTML::Template::COND::VARIABLE]}; + } + } + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if + (defined $line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET] and + scalar @{$line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET]}); + } + } else { + if ($line->[HTML::Template::COND::VARIABLE_TYPE] == HTML::Template::COND::VARIABLE_TYPE_VAR) { + if (defined ${$line->[HTML::Template::COND::VARIABLE]}) { + if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless ${$line->[HTML::Template::COND::VARIABLE]}->($self); + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless ${$line->[HTML::Template::COND::VARIABLE]}; + } + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS]; + } + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if + (not defined $line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET] or + not scalar @{$line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET]}); + } + } + } elsif ($type eq 'HTML::Template::NOOP') { + next; + } elsif ($type eq 'HTML::Template::ESCAPE') { + $x++; + *line = \$parse_stack[$x]; + if (defined($$line)) { + my $toencode = $$line; + + # straight from the CGI.pm bible. + $toencode=~s/&/&/g; + $toencode=~s/\"/"/g; #" + $toencode=~s/>/>/g; + $toencode=~s/</</g; + $toencode=~s/'/'/g; #' + + $result .= $toencode; + } + next; + } elsif ($type eq 'HTML::Template::URLESCAPE') { + $x++; + *line = \$parse_stack[$x]; + if (defined($$line)) { + my $toencode = $$line; + # Build a char->hex map if one isn't already available + unless (exists($URLESCAPE_MAP{chr(1)})) { + for (0..255) { $URLESCAPE_MAP{chr($_)} = sprintf('%%%02X', $_); } + } + # do the translation (RFC 2396 ^uric) + $toencode =~ s!([^a-zA-Z0-9_.\-])!$URLESCAPE_MAP{$1}!g; + $result .= $toencode; + } + } else { + confess("HTML::Template::output() : Unknown item in parse_stack : " . $type); + } + } + + # undo the globalization circular refs + $self->_unglobalize_vars() if ($options->{global_vars}); + + print STDERR "### HTML::Template Memory Debug ### END OUTPUT ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + return undef if defined $args{print_to}; + return $result; +} + +=pod + +=head2 query() + +This method allow you to get information about the template structure. +It can be called in a number of ways. The simplest usage of query is +simply to check whether a parameter name exists in the template, using +the C<name> option: + + if ($template->query(name => 'foo')) { + # do something if a varaible of any type + # named FOO is in the template + } + +This same usage returns the type of the parameter. The type is the +same as the tag minus the leading 'TMPL_'. So, for example, a +TMPL_VAR parameter returns 'VAR' from query(). + + if ($template->query(name => 'foo') eq 'VAR') { + # do something if FOO exists and is a TMPL_VAR + } + +Note that the variables associated with TMPL_IFs and TMPL_UNLESSs will +be identified as 'VAR' unless they are also used in a TMPL_LOOP, in +which case they will return 'LOOP'. + +C<query()> also allows you to get a list of parameters inside a loop +(and inside loops inside loops). Example loop: + + <TMPL_LOOP NAME="EXAMPLE_LOOP"> + <TMPL_VAR NAME="BEE"> + <TMPL_VAR NAME="BOP"> + <TMPL_LOOP NAME="EXAMPLE_INNER_LOOP"> + <TMPL_VAR NAME="INNER_BEE"> + <TMPL_VAR NAME="INNER_BOP"> + </TMPL_LOOP> + </TMPL_LOOP> + +And some query calls: + + # returns 'LOOP' + $type = $template->query(name => 'EXAMPLE_LOOP'); + + # returns ('bop', 'bee', 'example_inner_loop') + @param_names = $template->query(loop => 'EXAMPLE_LOOP'); + + # both return 'VAR' + $type = $template->query(name => ['EXAMPLE_LOOP', 'BEE']); + $type = $template->query(name => ['EXAMPLE_LOOP', 'BOP']); + + # and this one returns 'LOOP' + $type = $template->query(name => ['EXAMPLE_LOOP', + 'EXAMPLE_INNER_LOOP']); + + # and finally, this returns ('inner_bee', 'inner_bop') + @inner_param_names = $template->query(loop => ['EXAMPLE_LOOP', + 'EXAMPLE_INNER_LOOP']); + + # for non existent parameter names you get undef + # this returns undef. + $type = $template->query(name => 'DWEAZLE_ZAPPA'); + + # calling loop on a non-loop parameter name will cause an error. + # this dies: + $type = $template->query(loop => 'DWEAZLE_ZAPPA'); + +As you can see above the C<loop> option returns a list of parameter +names and both C<name> and C<loop> take array refs in order to refer +to parameters inside loops. It is an error to use C<loop> with a +parameter that is not a loop. + +Note that all the names are returned in lowercase and the types are +uppercase. + +Just like C<param()>, C<query()> with no arguements returns all the +parameter names in the template at the top level. + +=cut + +sub query { + my $self = shift; + $self->{options}{debug} and print STDERR "### HTML::Template Debug ### query(", join(', ', @_), ")\n"; + + # the no-parameter case - return $self->param() + return $self->param() unless scalar(@_); + + croak("HTML::Template::query() : Odd number of parameters passed to query!") + if (scalar(@_) % 2); + croak("HTML::Template::query() : Wrong number of parameters passed to query - should be 2.") + if (scalar(@_) != 2); + + my ($opt, $path) = (lc shift, shift); + croak("HTML::Template::query() : invalid parameter ($opt)") + unless ($opt eq 'name' or $opt eq 'loop'); + + # make path an array unless it already is + $path = [$path] unless (ref $path); + + # find the param in question. + my @objs = $self->_find_param(@$path); + return undef unless scalar(@objs); + my ($obj, $type); + + # do what the user asked with the object + if ($opt eq 'name') { + # we only look at the first one. new() should make sure they're + # all the same. + ($obj, $type) = (shift(@objs), shift(@objs)); + return undef unless defined $obj; + return 'VAR' if $type eq 'HTML::Template::VAR'; + return 'LOOP' if $type eq 'HTML::Template::LOOP'; + croak("HTML::Template::query() : unknown object ($type) in param_map!"); + + } elsif ($opt eq 'loop') { + my %results; + while(@objs) { + ($obj, $type) = (shift(@objs), shift(@objs)); + croak("HTML::Template::query() : Search path [", join(', ', @$path), "] doesn't end in a TMPL_LOOP - it is an error to use the 'loop' option on a non-loop parameter. To avoid this problem you can use the 'name' option to query() to check the type first.") + unless ((defined $obj) and ($type eq 'HTML::Template::LOOP')); + + # SHAZAM! This bit extracts all the parameter names from all the + # loop objects for this name. + map {$results{$_} = 1} map { keys(%{$_->{'param_map'}}) } + values(%{$obj->[HTML::Template::LOOP::TEMPLATE_HASH]}); + } + # this is our loop list, return it. + return keys(%results); + } +} + +# a function that returns the object(s) corresponding to a given path and +# its (their) ref()(s). Used by query() in the obvious way. +sub _find_param { + my $self = shift; + my $spot = $self->{options}{case_sensitive} ? shift : lc shift; + + # get the obj and type for this spot + my $obj = $self->{'param_map'}{$spot}; + return unless defined $obj; + my $type = ref $obj; + + # return if we're here or if we're not but this isn't a loop + return ($obj, $type) unless @_; + return unless ($type eq 'HTML::Template::LOOP'); + + # recurse. this is a depth first seach on the template tree, for + # the algorithm geeks in the audience. + return map { $_->_find_param(@_) } + values(%{$obj->[HTML::Template::LOOP::TEMPLATE_HASH]}); +} + +# HTML::Template::VAR, LOOP, etc are *light* objects - their internal +# spec is used above. No encapsulation or information hiding is to be +# assumed. + +package HTML::Template::VAR; + +sub new { + my ($pkg) = @_; + my $value; + my $self = \$value; + bless($self, $pkg); + return $self; +} + +package HTML::Template::LOOP; + +sub new { + my ($pkg) = shift; + my $self = []; + bless($self, $pkg); + return $self; +} + +sub output { + my $self = shift; + my $index = shift; + my $loop_context_vars = shift; + my $template = $self->[TEMPLATE_HASH]{$index}; + my $value_sets_array = $self->[PARAM_SET]; + return unless defined($value_sets_array); + + my $result = ''; + my $count = 0; + my $odd = 0; + foreach my $value_set (@$value_sets_array) { + if ($loop_context_vars) { + if ($count == 0) { + @{$value_set}{qw(__first__ __inner__ __last__)} = (1,0,$#{$value_sets_array} == 0); + } elsif ($count == $#{$value_sets_array}) { + @{$value_set}{qw(__first__ __inner__ __last__)} = (0,0,1); + } else { + @{$value_set}{qw(__first__ __inner__ __last__)} = (0,1,0); + } + $odd = $value_set->{__odd__} = not $odd; + } + $template->param($value_set); + $result .= $template->output; + $template->clear_params; + @{$value_set}{qw(__first__ __last__ __inner__ __odd__)} = (0,0,0,0) + if ($loop_context_vars); + $count++; + } + + return $result; +} + +package HTML::Template::COND; + +sub new { + my $pkg = shift; + my $var = shift; + my $self = []; + $self->[VARIABLE] = $var; + + bless($self, $pkg); + return $self; +} + +package HTML::Template::NOOP; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + +package HTML::Template::ESCAPE; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + +package HTML::Template::URLESCAPE; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + +# scalar-tying package for output(print_to => *HANDLE) implementation +package HTML::Template::PRINTSCALAR; +use strict; + +sub TIESCALAR { bless \$_[1], $_[0]; } +sub FETCH { } +sub STORE { + my $self = shift; + local *FH = $$self; + print FH @_; +} +1; +__END__ + +=head1 FREQUENTLY ASKED QUESTIONS + +In the interest of greater understanding I've started a FAQ section of +the perldocs. Please look in here before you send me email. + +=over 4 + +=item 1 + +Q: Is there a place to go to discuss HTML::Template and/or get help? + +A: There's a mailing-list for HTML::Template at htmltmpl@lists.vm.com. +Send a blank message to htmltmpl-subscribe@lists.vm.com to join! + +=item 2 + +Q: Is there a searchable archive for the mailing-list? + +A: Yes, thanks to Sean P. Scanlon, there is: + + http://bluedot.net/mail/archive/ + +=item 3 + +Q: I want support for <TMPL_XXX>! How about it? + +A: Maybe. I definitely encourage people to discuss their ideas for +HTML::Template on the mailing list. Please be ready to explain to me +how the new tag fits in with HTML::Template's mission to provide a +fast, lightweight system for using HTML templates. + +NOTE: Offering to program said addition and provide it in the form of +a patch to the most recent version of HTML::Template will definitely +have a softening effect on potential opponents! + +=item 4 + +Q: I found a bug, can you fix it? + +A: That depends. Did you send me the VERSION of HTML::Template, a test +script and a test template? If so, then almost certainly. + +If you're feeling really adventurous, HTML::Template has a publically +available CVS server. See below for more information in the PUBLIC +CVS SERVER section. + +=item 5 + +Q: <TMPL_VAR>s from the main template aren't working inside a +<TMPL_LOOP>! Why? + +A: This is the intended behavior. <TMPL_LOOP> introduces a separate +scope for <TMPL_VAR>s much like a subroutine call in Perl introduces a +separate scope for "my" variables. + +If you want your <TMPL_VAR>s to be global you can set the +'global_vars' option when you call new(). See above for documentation +of the 'global_vars' new() option. + +=item 6 + +Q: Why do you use /[Tt]/ instead of /t/i? It's so ugly! + +A: Simple - the case-insensitive match switch is very inefficient. +According to _Mastering_Regular_Expressions_ from O'Reilly Press, +/[Tt]/ is faster and more space efficient than /t/i - by as much as +double against long strings. //i essentially does a lc() on the +string and keeps a temporary copy in memory. + +When this changes, and it is in the 5.6 development series, I will +gladly use //i. Believe me, I realize [Tt] is hideously ugly. + +=item 7 + +Q: How can I pre-load my templates using cache-mode and mod_perl? + +A: Add something like this to your startup.pl: + + use HTML::Template; + use File::Find; + + print STDERR "Pre-loading HTML Templates...\n"; + find( + sub { + return unless /\.tmpl$/; + HTML::Template->new( + filename => "$File::Find::dir/$_", + cache => 1, + ); + }, + '/path/to/templates', + '/another/path/to/templates/' + ); + +Note that you'll need to modify the "return unless" line to specify +the extension you use for your template files - I use .tmpl, as you +can see. You'll also need to specify the path to your template files. + +One potential problem: the "/path/to/templates/" must be EXACTLY the +same path you use when you call HTML::Template->new(). Otherwise the +cache won't know they're the same file and will load a new copy - +instead getting a speed increase, you'll double your memory usage. To +find out if this is happening set cache_debug => 1 in your application +code and look for "CACHE MISS" messages in the logs. + +=item 8 + +Q: What characters are allowed in TMPL_* NAMEs? + +A: Numbers, letters, '.', '/', '+', '-' and '_'. + +=item 9 + +Q: How can I execute a program from inside my template? + +A: Short answer: you can't. Longer answer: you shouldn't since this +violates the fundamental concept behind HTML::Template - that design +and code should be seperate. + +But, inevitably some people still want to do it. If that describes +you then you should take a look at +L<HTML::Template::Expr|HTML::Template::Expr>. Using +HTML::Template::Expr it should be easy to write a run_program() +function. Then you can do awful stuff like: + + <tmpl_var expr="run_program('foo.pl')"> + +Just, please, don't tell me about it. I'm feeling guilty enough just +for writing HTML::Template::Expr in the first place. + +=item 10 + +Q: Can I get a copy of these docs in Japanese? + +A: Yes you can. See Kawai Takanori's translation at: + + http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm + +=item 11 + +Q: What's the best way to create a <select> form element using +HTML::Template? + +A: There is much disagreement on this issue. My personal preference +is to use CGI.pm's excellent popup_menu() and scrolling_list() +functions to fill in a single <tmpl_var select_foo> variable. + +To some people this smacks of mixing HTML and code in a way that they +hoped HTML::Template would help them avoid. To them I'd say that HTML +is a violation of the principle of separating design from programming. +There's no clear separation between the programmatic elements of the +<form> tags and the layout of the <form> tags. You'll have to draw +the line somewhere - clearly the designer can't be entirely in charge +of form creation. + +It's a balancing act and you have to weigh the pros and cons on each side. +It is certainly possible to produce a <select> element entirely inside the +template. What you end up with is a rat's nest of loops and conditionals. +Alternately you can give up a certain amount of flexibility in return for +vastly simplifying your templates. I generally choose the latter. + +Another option is to investigate HTML::FillInForm which some have +reported success using to solve this problem. + +=back + +=head1 BUGS + +I am aware of no bugs - if you find one, join the mailing list and +tell us about it (htmltmpl@lists.vm.com). You can join the +HTML::Template mailing-list by sending a blank email to +htmltmpl-subscribe@lists.vm.com. Of course, you can still email me +directly (sam@tregar.com) with bugs, but I reserve the right to +forward bug reports to the mailing list. + +When submitting bug reports, be sure to include full details, +including the VERSION of the module, a test script and a test template +demonstrating the problem! + +If you're feeling really adventurous, HTML::Template has a publically +available CVS server. See below for more information in the PUBLIC +CVS SERVER section. + +=head1 CREDITS + +This module was the brain child of my boss, Jesse Erlbaum +( jesse@vm.com ) at Vanguard Media ( http://vm.com ) . The most original +idea in this module - the <TMPL_LOOP> - was entirely his. + +Fixes, Bug Reports, Optimizations and Ideas have been generously +provided by: + + Richard Chen + Mike Blazer + Adriano Nagelschmidt Rodrigues + Andrej Mikus + Ilya Obshadko + Kevin Puetz + Steve Reppucci + Richard Dice + Tom Hukins + Eric Zylberstejn + David Glasser + Peter Marelas + James William Carlson + Frank D. Cringle + Winfried Koenig + Matthew Wickline + Doug Steinwand + Drew Taylor + Tobias Brox + Michael Lloyd + Simran Gambhir + Chris Houser <chouser@bluweb.com> + Larry Moore + Todd Larason + Jody Biggs + T.J. Mather + Martin Schroth + Dave Wolfe + uchum + Kawai Takanori + Peter Guelich + Chris Nokleberg + Ralph Corderoy + William Ward + Ade Olonoh + Mark Stosberg + Lance Thomas + Roland Giersig + Jere Julian + Peter Leonard + Kenny Smith + Sean P. Scanlon + Martin Pfeffer + David Ferrance + Gyepi Sam + Darren Chamberlain + +Thanks! + +=head1 WEBSITE + +You can find information about HTML::Template and other related modules at: + + http://html-template.sourceforge.net + +=head1 PUBLIC CVS SERVER + +HTML::Template now has a publicly accessible CVS server provided by +SourceForge (www.sourceforge.net). You can access it by going to +http://sourceforge.net/cvs/?group_id=1075. Give it a try! + +=head1 AUTHOR + +Sam Tregar, sam@tregar.com (you can also find me on the mailing list +at htmltmpl@lists.vm.com - join it by sending a blank message to +htmltmpl-subscribe@lists.vm.com). + +=head1 LICENSE + + HTML::Template : A module for using HTML Templates with Perl + Copyright (C) 2000-2002 Sam Tregar (sam@tregar.com) + + This module is free software; you can redistribute it and/or modify it + under the terms of either: + + a) the GNU General Public License as published by the Free Software + Foundation; either version 1, or (at your option) any later version, + + or + + b) the "Artistic License" which comes with this module. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either + the GNU General Public License or the Artistic License for more details. + + You should have received a copy of the Artistic License with this + module, in the file ARTISTIC. If not, I'll be glad to provide one. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA + +=cut diff --git a/cpan/dist/HTML-Template/scripts/clean_shm.pl b/cpan/dist/HTML-Template/scripts/clean_shm.pl new file mode 100755 index 00000000..ece118fa --- /dev/null +++ b/cpan/dist/HTML-Template/scripts/clean_shm.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# this script deletes all shared memory segments accessible by the +# running user. Probably only works on Linux systems. + +open(IPCS, 'ipcs |') or die "Probs opening icps : $!"; + +my $mode = 0; +while(<IPCS>) { + if (/(Shared)|(Semaphore)|(Message)/) { + $mode++; + next; + } + next unless /^0x[0-9a-f]+\s(\d+)\s.*$/; + system("ipcrm shm $1") if ($mode == 1); + system("ipcrm sem $1") if ($mode == 2); + system("ipcrm msg $1") if ($mode == 3); +} diff --git a/cpan/dist/HTML-Template/scripts/time_trial.pl b/cpan/dist/HTML-Template/scripts/time_trial.pl new file mode 100755 index 00000000..0db6bd14 --- /dev/null +++ b/cpan/dist/HTML-Template/scripts/time_trial.pl @@ -0,0 +1,90 @@ +#!/usr/bin/perl -w + +# this is a little script I use to test the timing behavior of various +# options in HTML::Template;; + +# set this to wherever your test area is +require '../Template.pm'; + +# an array of template files to test against and params to fill in + my %templates = ( + simple => [ + '../templates/simple.tmpl', + {} + ], + include => [ + '../templates/include.tmpl' + ], + medium => [ + '../templates/medium.tmpl', + { + 'ALERT', 'I am alert.', + 'COMPANY_NAME', "MY NAME IS", + 'COMPANY_ID', "10001", + 'OFFICE_ID', "10103214", + 'NAME', 'SAM I AM', + 'ADDRESS', '101011 North Something Something', + 'CITY', 'NEW York', + 'STATE', 'NEw York', + 'ZIP','10014', + 'PHONE','212-929-4315', + 'PHONE2','', + 'SUBCATEGORIES','kfldjaldsf', + 'DESCRIPTION',"dsa;kljkldasfjkldsajflkjdsfklfjdsgkfld\nalskdjklajsdlkajfdlkjsfd\n\talksjdklajsfdkljdsf\ndsa;klfjdskfj", + 'WEBSITE','http://www.assforyou.com/', + 'INTRANET_URL','http://www.something.com', + 'REMOVE_BUTTON', "<INPUT TYPE=SUBMIT NAME=command VALUE=\"Remove Office\">", + 'COMPANY_ADMIN_AREA', "<A HREF=administrator.cgi?office_id=office_id&command=manage>Manage Office Administrators</A>", + 'CASESTUDIES_LIST', "adsfkljdskldszfgfdfdsgdsfgfdshghdmfldkgjfhdskjfhdskjhfkhdsakgagsfjhbvdsaj hsgbf jhfg sajfjdsag ffasfj hfkjhsdkjhdsakjfhkj kjhdsfkjhdskfjhdskjfkjsda kjjsafdkjhds kjds fkj skjh fdskjhfkj kj kjhf kjh sfkjhadsfkj hadskjfhkjhs ajhdsfkj akj fkj kj kj kkjdsfhk skjhadskfj haskjh fkjsahfkjhsfk ksjfhdkjh sfkjhdskjfhakj shiou weryheuwnjcinuc 3289u4234k 5 i 43iundsinfinafiunai saiufhiudsaf afiuhahfwefna uwhf u auiu uh weiuhfiuh iau huwehiucnaiuncianweciuninc iuaciun iucniunciunweiucniuwnciwe", + 'NUMBER_OF_CONTACTS', "aksfjdkldsajfkljds", + 'COUNTRY_SELECTOR', "klajslkjdsafkljds", + 'LOGO_LINK', "dsfpkjdsfkgljdsfkglj", + 'PHOTO_LINK', "lsadfjlkfjdsgkljhfgklhasgh", + } + ], + long_loop => [ '../templates/long_loops.tmpl', + {} + ], + loop_if => [ '../templates/loop-if.tmpl', + {LOOP_ONE => [{VAR => 'foo'}]} + ] + + ); + +# a hash of option hashes to test +my %options = ( + 'no cache' => {}, + #'simple cache' => { cache => 1 }, + # 'shared cache' => { shared_cache => 1, cache => 1 }, + 'file cache' => { file_cache => 1, + file_cache_dir => './file_cache' }, +# 'simple cache, no_includes' => { cache => 1, no_includes => 1}, +# 'blind cache' => { blind_cache => 1}, + ); + +# number of times over each template +my $n = 100; +#open(OUT, ">test.out"); + +foreach my $template (keys %templates) { + print "\nTESTING : $template : $n iterations\n\n"; + + foreach my $option (keys %options) { + my $start_time = (times)[0]; + for (my $x = 0; $x < $n; $x++) { + my $template = HTML::Template->new(filename => $templates{$template}->[0], + %{$options{$option}} + ); + foreach my $name (keys %{$templates{$template}->[1]}) { + $template->param($name => $templates{$template}->[1]->{$name}); + } + my $result = $template->output; + #print OUT $result; + #$template->output(print_to => *OUT); + } + + my $end_time = (times)[0]; + print "$option : average iteration in " . (($end_time - $start_time) / $n) . " seconds\n"; + } +} + diff --git a/cpan/dist/HTML-Template/templates/case_loop.tmpl b/cpan/dist/HTML-Template/templates/case_loop.tmpl new file mode 100644 index 00000000..93a6815e --- /dev/null +++ b/cpan/dist/HTML-Template/templates/case_loop.tmpl @@ -0,0 +1,3 @@ +<tmpl_loop name="loop"><tmpl_var foo> <tmpl_var FOO> +</tmpl_loop> + diff --git a/cpan/dist/HTML-Template/templates/context.tmpl b/cpan/dist/HTML-Template/templates/context.tmpl new file mode 100644 index 00000000..c19086e2 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/context.tmpl @@ -0,0 +1,3 @@ +<TMPL_LOOP FRUIT><TMPL_IF __LAST__>and </TMPL_IF><TMPL_VAR KIND><TMPL_UNLESS __LAST__>, <TMPL_ELSE>.</TMPL_UNLESS></TMPL_LOOP> + +<TMPL_LOOP PINGPONG><TMPL_IF __ODD__>ping<TMPL_ELSE>pong</TMPL_IF></TMPL_LOOP> diff --git a/cpan/dist/HTML-Template/templates/double_loop.tmpl b/cpan/dist/HTML-Template/templates/double_loop.tmpl new file mode 100644 index 00000000..c2e40f18 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/double_loop.tmpl @@ -0,0 +1,7 @@ +<tmpl_loop myloop> +One of David's numbers is <tmpl_var var>. +</tmpl_loop> + +<tmpl_loop myloop> +VAR TIME: <tmpl_var var> +</tmpl_loop> diff --git a/cpan/dist/HTML-Template/templates/escape.tmpl b/cpan/dist/HTML-Template/templates/escape.tmpl new file mode 100644 index 00000000..df1f3f93 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/escape.tmpl @@ -0,0 +1,5 @@ +This should be a bunch of HTML-escaped stuff: +<TMPL_VAR ESCAPE=1 STUFF> +<TMPL_VAR ESCAPE=HTML STUFF> +<TMPL_VAR ESCAPE="HTML" STUFF> +<TMPL_VAR ESCAPE="HtMl" STUFF> diff --git a/cpan/dist/HTML-Template/templates/global-loops.tmpl b/cpan/dist/HTML-Template/templates/global-loops.tmpl new file mode 100644 index 00000000..5f4342eb --- /dev/null +++ b/cpan/dist/HTML-Template/templates/global-loops.tmpl @@ -0,0 +1,9 @@ +global: <tmpl_var global> +<tmpl_loop outer_loop> + outer loop foo: <tmpl_var foo> + <tmpl_loop inner_loop> + inner loop bar: <tmpl_var bar> + inner loop foo: <tmpl_var foo> + inner loop global: <tmpl_var global> + </tmpl_loop> +</tmpl_loop> diff --git a/cpan/dist/HTML-Template/templates/globals.tmpl b/cpan/dist/HTML-Template/templates/globals.tmpl new file mode 100644 index 00000000..3f8c34ca --- /dev/null +++ b/cpan/dist/HTML-Template/templates/globals.tmpl @@ -0,0 +1,11 @@ + +Some global var: <TMPL_VAR GLOBAL> + +Some Loop. +<TMPL_LOOP OUTER_LOOP> + <TMPL_LOOP LOOP> + Some local data <TMPL_VAR LOCAL><TMPL_VAR GLOBAL> + Something global, but hidden <TMPL_IF HIDDEN_GLOBAL>Hidden!</TMPL_IF> + </TMPL_LOOP> +</TMPL_LOOP> + diff --git a/cpan/dist/HTML-Template/templates/if.tmpl b/cpan/dist/HTML-Template/templates/if.tmpl new file mode 100644 index 00000000..69bbb48f --- /dev/null +++ b/cpan/dist/HTML-Template/templates/if.tmpl @@ -0,0 +1,7 @@ +This is a line outside the if. +<TMPL_IF BOOL> +INSIDE the if +</TMPL_IF> +<TMPL_UNLESS BOOL> +unless +</tmpl_unless> diff --git a/cpan/dist/HTML-Template/templates/ifelse.tmpl b/cpan/dist/HTML-Template/templates/ifelse.tmpl new file mode 100644 index 00000000..1aff9a0f --- /dev/null +++ b/cpan/dist/HTML-Template/templates/ifelse.tmpl @@ -0,0 +1,6 @@ +This is a line outside the if. +<TMPL_IF BOOL> +INSIDE IF +<TMPL_ELSE> +INSIDE ELSE +</TMPL_IF> diff --git a/cpan/dist/HTML-Template/templates/include.tmpl b/cpan/dist/HTML-Template/templates/include.tmpl new file mode 100644 index 00000000..67d2f293 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/include.tmpl @@ -0,0 +1,14 @@ +1 +1 +1 +2 +2 +2 +3 +3 +3 +<TMPL_INCLUDE NAME="included.tmpl"> +6 +6 +6 + diff --git a/cpan/dist/HTML-Template/templates/include_path/a.tmpl b/cpan/dist/HTML-Template/templates/include_path/a.tmpl new file mode 100644 index 00000000..3a7a685e --- /dev/null +++ b/cpan/dist/HTML-Template/templates/include_path/a.tmpl @@ -0,0 +1,2 @@ +Foo +<TMPL_INCLUDE "b.tmpl"> diff --git a/cpan/dist/HTML-Template/templates/include_path/b.tmpl b/cpan/dist/HTML-Template/templates/include_path/b.tmpl new file mode 100644 index 00000000..ebd7525b --- /dev/null +++ b/cpan/dist/HTML-Template/templates/include_path/b.tmpl @@ -0,0 +1 @@ +Bar diff --git a/cpan/dist/HTML-Template/templates/include_path/one.tmpl b/cpan/dist/HTML-Template/templates/include_path/one.tmpl new file mode 100644 index 00000000..ace8e23f --- /dev/null +++ b/cpan/dist/HTML-Template/templates/include_path/one.tmpl @@ -0,0 +1,2 @@ +ONE +<tmpl_include ../searchpath/two.tmpl> diff --git a/cpan/dist/HTML-Template/templates/included.tmpl b/cpan/dist/HTML-Template/templates/included.tmpl new file mode 100644 index 00000000..160f237f --- /dev/null +++ b/cpan/dist/HTML-Template/templates/included.tmpl @@ -0,0 +1,4 @@ +4 +4 +4 +<TMPL_INCLUDE NAME="included2.tmpl"> diff --git a/cpan/dist/HTML-Template/templates/included2.tmpl b/cpan/dist/HTML-Template/templates/included2.tmpl new file mode 100644 index 00000000..f6ba75da --- /dev/null +++ b/cpan/dist/HTML-Template/templates/included2.tmpl @@ -0,0 +1,3 @@ +5 +5 +5 diff --git a/cpan/dist/HTML-Template/templates/long_loops.tmpl b/cpan/dist/HTML-Template/templates/long_loops.tmpl new file mode 100644 index 00000000..5fc7f93a --- /dev/null +++ b/cpan/dist/HTML-Template/templates/long_loops.tmpl @@ -0,0 +1,307 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<HTML>
+<HEAD>
+<TITLE>WPP Hotel Directory Vistor Comments: <TMPL_VAR PROPNAME></TITLE>
+
+
+<SCRIPT LANGUAGE = "JavaScript">
+
+
+<!--
+
+// Browser detect
+
+
+
+ bName = navigator.appName;
+
+ bVer = parseInt(navigator.appVersion.charAt(0));
+
+
+
+ if ((bName == "Netscape" && bVer >= 3) || (bName == "Microsoft Internet Explorer" && bVer >= 4)) br = "n3";
+
+ else if(bName == "Netscape" && bVer >= 2) br = "n2";
+
+ else if(bName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf('3.')!=-1) br = "ie3";
+
+ else br = "n0"
+
+
+ if (document.images) {
+
+ img1on = new Image();
+ img1on.src = "/images/hotel/nav_wpp_2.gif"; // Active Inside wpp
+ img1off = new Image();
+ img1off.src = "/images/hotel/nav_wpp_1.gif"; // Inactive inside wpp
+
+ img2on = new Image();
+ img2on.src = "/images/hotel/nav_newsearch_2.gif"; // Active new search
+ img2off = new Image();
+ img2off.src = "/images/hotel/nav_newsearch_1.gif"; // Inactive new search
+
+ img3on = new Image();
+ img3on.src = "/images/hotel/nav_list_2.gif"; // Active return to hotel list
+ img3off = new Image();
+ img3off.src = "/images/hotel/nav_list_1.gif"; // Inactive return to hotel list
+
+ img4on = new Image();
+ img4on.src = "/images/hotel/hotel_door_info_2.gif"; // Active return to hotel info
+ img4off = new Image();
+ img4off.src = "/images/hotel/hotel_door_info_1.gif"; // Inactive return to hotel info
+
+ img5on = new Image();
+ img5on.src = "/images/hotel/hotel_door_rate_2.gif"; // Active return to hotel rate
+ img5off = new Image();
+ img5off.src = "/images/hotel/hotel_door_rate_1.gif"; // Inactive return to hotel rate
+
+ img6on = new Image();
+ img6on.src = "/images/hotel/hotel_door_fac_2.gif"; // Active return to hotel facilities
+ img6off = new Image();
+ img6off.src = "/images/hotel/hotel_door_fac_1.gif"; // Inactive return to hotel facilities
+
+ img7on = new Image();
+ img7on.src = "/images/hotel/hotel_door_pol_2.gif"; // Active return to hotel policies
+ img7off = new Image();
+ img7off.src = "/images/hotel/hotel_door_pol_1.gif"; // Inactive return to hotel policies
+
+ img8on = new Image();
+ img8on.src = "/images/hotel/hotel_door_area_2.gif"; // Active return to hotel area
+ img8off = new Image();
+ img8off.src = "/images/hotel/hotel_door_area_1.gif"; // Inactive return to hotel area
+
+ img9on = new Image();
+ img9on.src = "/images/hotel/hotel_info_navaddress_2.gif"; // Active adress and contact info
+ img9off = new Image();
+ img9off.src = "/images/hotel/hotel_info_navaddress_1.gif"; // Inactive adress and contact info
+
+ img10on = new Image();
+ img10on.src = "/images/hotel/hotel_info_navdescrip_2.gif"; // Active
+ img10off = new Image();
+ img10off.src = "/images/hotel/hotel_info_navdescrip_1.gif"; // Inactive
+
+ img11on = new Image();
+ img11on.src = "/images/hotel/hotel_info_navrating_2.gif"; // Active
+ img11off = new Image();
+ img11off.src = "/images/hotel/hotel_info_navrating_1.gif"; // Inactive
+
+ img12on = new Image();
+ img12on.src = "/images/hotel/hotel_info_navairport_2.gif"; // Active
+ img12off = new Image();
+ img12off.src = "/images/hotel/hotel_info_navairport_1.gif"; // Inactive
+
+ img13on = new Image();
+ img13on.src = "/images/hotel/hotel_info_navcity_2.gif"; // Active
+ img13off = new Image();
+ img13off.src = "/images/hotel/hotel_info_navcity_1.gif"; // Inactive
+
+ img14on = new Image();
+ img14on.src = "/images/hotel/hotel_info_viewmap_2.gif"; // Active
+ img14off = new Image();
+ img14off.src = "/images/hotel/hotel_info_viewmap_1.gif"; // Inactive
+
+ imgCOMon = new Image();
+ imgCOMon.src = "/images/hotel/hotel_comments_2.gif"; // Active
+ imgCOMoff = new Image();
+ imgCOMoff.src = "/images/hotel/hotel_comments_1.gif"; // Inactive
+
+ imgTopAon = new Image();
+ imgTopAon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopAoff = new Image();
+ imgTopAoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ imgTopBon = new Image();
+ imgTopBon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopBoff = new Image();
+ imgTopBoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ imgTopCon = new Image();
+ imgTopCon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopCoff = new Image();
+ imgTopCoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ imgTopDon = new Image();
+ imgTopDon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopDoff = new Image();
+ imgTopDoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ imgTopEon = new Image();
+ imgTopEon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopEoff = new Image();
+ imgTopEoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ imgTopFon = new Image();
+ imgTopFon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopFoff = new Image();
+ imgTopFoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ imgTopGon = new Image();
+ imgTopGon.src = "/images/hotel/hotel_top_2.gif"; // Active return to top
+ imgTopGoff = new Image();
+ imgTopGoff.src = "/images/hotel/hotel_top_1.gif"; // Inactive return to top
+
+ }
+
+
+ function imgOn(imgName) {
+
+ if (document.images) {
+
+ document[imgName].src = eval(imgName + "on.src");
+
+ }
+
+ }
+
+
+ function imgOff(imgName) {
+
+ if (document.images) {
+
+ document[imgName].src = eval(imgName + "off.src");
+
+ }
+
+ }
+
+<TMPL_VAR JAVASCRIPT>
+
+// -->
+
+
+</SCRIPT>
+
+</HEAD>
+
+<BODY BGCOLOR="#FFFFFF" LINK="#3366CC" ALINK="#003399" VLINK="#993399">
+
+<FORM ACTION=browser.cgi METHOD=POST>
+
+<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="613">
+
+ <TR>
+ <TD WIDTH="230" ALIGN=LEFT VALIGN=TOP><a href="/" onmouseover = "imgOn('img1')" onmouseout = "imgOff('img1')"><IMG SRC="/images/hotel/nav_wpp_1.gif" NAME="img1" WIDTH="50" HEIGHT="35" BORDER="0" ALT="Inside WPP"></A><a href="javascript:submit_form('search')" onmouseover = "imgOn('img2')" onmouseout = "imgOff('img2')"><IMG SRC="/images/hotel/nav_newsearch_1.gif" NAME="img2" ALT="New search" WIDTH="67" HEIGHT="35" BORDER="0"></a><a href="javascript:submit_form('results')" onmouseover = "imgOn('img3')" onmouseout = "imgOff('img3')"><IMG SRC="/images/hotel/nav_list_1.gif" NAME="img3" ALT="Return to hotel list" WIDTH="113" HEIGHT="35" BORDER="0"></a></TD>
+ <TD WIDTH="383" ALIGN=RIGHT VALIGN=TOP><a name="top"><IMG SRC="/images/hotel/hd_logo.gif" ALT="WPP Hotel Directory" WIDTH="207" HEIGHT="35" BORDER="0"></A></TD>
+ </TR>
+
+</TABLE>
+
+
+<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="613">
+
+ <TR>
+ <TD WIDTH="72" ALIGN="left" ROWSPAN="2"><IMG SRC="/images/hotel/hotel_door_sidetop.gif" ALT="WPP Hotel Directory" WIDTH="72" HEIGHT="18" BORDER="0"><IMG SRC="/images/hotel/hotel_door_sidebottom.gif" ALT="WPP Hotel Directory" WIDTH="72" HEIGHT="99" BORDER="0"></TD>
+ <TD WIDTH="540" ALIGN="left" COLSPAN="2"><FONT FACE="ARIAL, GENEVA" SIZE="4" COLOR="#993300"><B><TMPL_VAR PROPNAME></B></FONT></TD>
+ </TR>
+
+ <TR>
+ <TD ALIGN=LEFT VALIGN=TOP><IMG SRC="/images/hotel/hotel_door_info_3.gif" NAME="img4" WIDTH="50" HEIGHT="99" BORDER="0" ALT="General Info"><a href="javascript:submit_form('hotel_rate')" onmouseover = "imgOn('img5')" onmouseout = "imgOff('img5')"><IMG SRC="/images/hotel/hotel_door_rate_1.gif" NAME="img5" ALT="Rates and Charges" WIDTH="50" HEIGHT="99" BORDER="0"></a><a href="javascript:submit_form('hotel_fac')" onmouseover = "imgOn('img6')" onmouseout = "imgOff('img6')"><IMG SRC="/images/hotel/hotel_door_fac_1.gif" NAME="img6" ALT="Facilities and Amenities" WIDTH="56" HEIGHT="99" BORDER="0"></a><a href="javascript:submit_form('hotel_pol')" onmouseover = "imgOn('img7')" onmouseout = "imgOff('img7')"><IMG SRC="/images/hotel/hotel_door_pol_1.gif" NAME="img7" ALT="Policies" WIDTH="47" HEIGHT="99" BORDER="0"></a><a href="javascript:submit_form('hotel_area')" onmouseover = "imgOn('img8')" onmouseout = "imgOff('img8')"><IMG SRC="/images/hotel/hotel_door_area_1.gif" NAME="img8" ALT="Also in the area" WIDTH="57" HEIGHT="99" BORDER="0"></a><a href="comments.cgi?hotelkey=<TMPL_VAR HOTEL_KEY>" onmouseover = "imgOn('imgCOM')" onmouseout = "imgOff('imgCOM')"><IMG SRC="/images/hotel/hotel_comments_1.gif" NAME="imgCOM" ALT="Comments" WIDTH="63" HEIGHT="99" BORDER="0"></a></TD>
+
+ </TR>
+</TABLE>
+
+</FORM>
+
+<FORM ACTION=comments.cgi METHOD=POST>
+<INPUT TYPE=HIDDEN NAME=rm VALUE=command>
+<INPUT TYPE=HIDDEN NAME=hotelkey VALUE=<TMPL_VAR HOTEL_KEY>>
+
+<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="613">
+
+<TR>
+ <TD WIDTH="200" ALIGN=LEFT VALIGN=TOP><a href="#top" onmouseover = "imgOn('imgTopA')" onmouseout = "imgOff('imgTopA')"><IMG SRC="/images/hotel/hotel_top_1.gif" NAME="imgTopA" ALT="Back to the top" WIDTH="31" HEIGHT="29" BORDER="0"></A><FONT SIZE="+1" COLOR="#000000" FACE="ARIAL, GENEVA">Vistor Comments<TMPL_VAR ALERT></FONT></TD>
+ <TD ALIGN=RIGHT><TMPL_VAR NEW_COMMENT_BUTTON></TD>
+</TR>
+
+</TABLE>
+
+<TMPL_LOOP NAME=COMMENTS>
+ <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="613">
+ <TR><TD COLSPAN=2>
+ <TABLE BGCOLOR=#993300 BORDER=0 CELLSPACING=1 WIDTH=100%>
+
+ <TR><TD><TABLE WIDTH=100% CELLSPACING=0>
+
+ <TR>
+
+ <TD BGCOLOR=<TMPL_VAR COLOR>><FONT SIZE="-1" COLOR="#000000" FACE="ARIAL, GENEVA">
+ <B>Posted by:</B> <TMPL_VAR NAME=posted_by> <TMPL_VAR NAME=posted_on>
+ </TD>
+
+ <TD BGCOLOR=<TMPL_VAR COLOR>><FONT SIZE="-1" COLOR="#000000" FACE="ARIAL, GENEVA">
+ <B>Rating:</B> <TMPL_VAR NAME=stars>
+ </TD>
+ </TR>
+
+ <TR>
+ <TD BGCOLOR=<TMPL_VAR COLOR> COLSPAN=2><FONT SIZE="-1" COLOR="#000000" FACE="ARIAL, GENEVA">
+ <B>Comment</B><BR>
+ <TMPL_VAR NAME=comment><BR>
+ <TMPL_VAR NAME=approval_button><TMPL_VAR NAME=edit_button><TMPL_VAR NAME=delete_button><BR>
+ </TD>
+ </TR>
+ </TABLE></TD></TR><BR><FONT SIZE=-1>
+ </TABLE> </TD></TR>
+ </TABLE>
+</TMPL_LOOP>
+
+<TMPL_LOOP NAME=COMMENT_EDITOR>
+<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="613">
+ <TR><TD COLSPAN=2>
+ <TABLE BGCOLOR=#993300 BORDER=0 CELLSPACING=1 WIDTH=100%>
+ <TR><TD><TABLE WIDTH=100% CELLSPACING=0>
+
+ <TR>
+
+ <TD BGCOLOR=<TMPL_VAR COLOR>><FONT SIZE="-1" COLOR="#000000" FACE="ARIAL, GENEVA">
+ <B>Posted by:</B> <TMPL_VAR NAME=posted_by> <TMPL_VAR NAME=posted_on>
+ </TD>
+
+ <TD BGCOLOR=<TMPL_VAR COLOR>><FONT SIZE="-1" COLOR="#000000" FACE="ARIAL, GENEVA">
+ <B>Rating:</B> <TMPL_VAR NAME=stars>
+ </TD>
+ </TR>
+
+ <TR>
+ <TD BGCOLOR=<TMPL_VAR COLOR> COLSPAN=2><FONT SIZE="-1" COLOR="#000000" FACE="ARIAL, GENEVA">
+ <B>Comment</B><BR>
+ <TMPL_VAR NAME=comment><BR>
+ <TMPL_VAR NAME=ok_button><TMPL_VAR NAME=cancel_button><BR>
+ </TD>
+ </TR>
+ </TABLE></TD></TR><BR><FONT SIZE=-1>
+ </TABLE> </TD></TR>
+</TABLE>
+</TMPL_LOOP>
+
+<TMPL_LOOP NAME=COMMENT_CREATED>
+<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="613">
+ <TR><TD COLSPAN=2>
+ <TABLE BGCOLOR=#993300 BORDER=0 CELLSPACING=1 WIDTH=100%>
+ <TR><TD> <TABLE WIDTH=100% CELLSPACING=0>
+
+ <TR>
+
+ <TD BGCOLOR=#FFFFFF><FONT COLOR="#000000" FACE="ARIAL, GENEVA">
+ Thank you for you comment <TMPL_VAR NAME=posted_by>!<BR>
+ The moderators have been notified of your new comment - they will review it as soon as possible.<P>
+<INPUT TYPE=SUBMIT NAME=list VALUE="Return to the Vistor Comments List"><BR>
+
+ </TD>
+ </TR>
+ </TABLE></TD></TR><BR><FONT SIZE=-1>
+
+ </TABLE> </TD></TR>
+</TABLE>
+</TMPL_LOOP>
+
+</TABLE>
+
+</TABLE>
+
+<BR>
+</FORM>
+
+</BODY>
+</HTML>
+
diff --git a/cpan/dist/HTML-Template/templates/loop-context.tmpl b/cpan/dist/HTML-Template/templates/loop-context.tmpl new file mode 100644 index 00000000..e3aa2abc --- /dev/null +++ b/cpan/dist/HTML-Template/templates/loop-context.tmpl @@ -0,0 +1,2 @@ +<TMPL_LOOP TEST_LOOP><TMPL_VAR NUM>:<TMPL_IF __FIRST__>FIRST</TMPL_IF>:<TMPL_IF __INNER__>INNER</TMPL_IF>:<TMPL_IF __LAST__>LAST</TMPL_IF>:<TMPL_IF __ODD__>ODD</TMPL_IF> +</TMPL_LOOP> diff --git a/cpan/dist/HTML-Template/templates/loop-if.tmpl b/cpan/dist/HTML-Template/templates/loop-if.tmpl new file mode 100644 index 00000000..c8f05401 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/loop-if.tmpl @@ -0,0 +1,9 @@ +<TMPL_IF LOOP_ONE> + <TMPL_LOOP LOOP_ONE> + <TMPL_IF VAR> + <TMPL_VAR VAR> + </TMPL_IF> + </TMPL_LOOP> +<TMPL_ELSE> + Loop not filled in! +</TMPL_IF> diff --git a/cpan/dist/HTML-Template/templates/medium.tmpl b/cpan/dist/HTML-Template/templates/medium.tmpl new file mode 100644 index 00000000..dab4f8a6 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/medium.tmpl @@ -0,0 +1,217 @@ +<HTML>
+<HEAD>
+<TITLE> Global Navigator Administration : Office Editor </TITLE>
+</HEAD>
+<BODY BGCOLOR=#FFFFFF TEXT=#000000>
+
+
+<TABLE BGCOLOR=#FFFFFF BORDER=0 CELLSPACING=10 CELLPADING=10 ALIGN=CENTER WIDTH=100%>
+<TR><TD>
+
+<FONT FACE="Arial, Helvetica, Sans-Serif" SIZE=+1>
+Office Editor
+<TMPL_VAR NAME=ALERT>
+</FONT>
+</TD></TR>
+
+<TR><TD>
+<TABLE BORDER=0 BGCOLOR=#000000 WIDTH=100% CELLSPACING=10>
+<TR><TD COLSPAN=2>
+<FONT FACE="Arial, Helvetica, Sans-Serif" COLOR=#FFFFFF> Office Data
+</TD></TR>
+<TR><TD BGCOLOR=#FFFFFF><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <FORM ACTION=office.cgi METHOD=POST ENCTYPE=multipart/form-data>
+ <INPUT TYPE=HIDDEN NAME=command VALUE=updateOffice>
+ <INPUT TYPE=HIDDEN NAME=office_id VALUE=<TMPL_VAR NAME=OFFICE_ID>>
+ <INPUT TYPE=HIDDEN NAME=company_id VALUE=<TMPL_VAR NAME=COMPANY_ID>>
+<TABLE BORDER=2>
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Company Name
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <A HREF=company.cgi?command=Go&company_id=<TMPL_VAR NAME=COMPANY_ID>><TMPL_VAR NAME=COMPANY_NAME></A>
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Office Name
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=name VALUE="<TMPL_VAR NAME=NAME>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Address
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TEXTAREA ROWS=2 COLS=40 NAME=address><TMPL_VAR NAME=ADDRESS></TEXTAREA>
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ City
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=city VALUE="<TMPL_VAR NAME=CITY>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ State
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=state VALUE="<TMPL_VAR NAME=STATE>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Country
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TMPL_VAR NAME=COUNTRY_SELECTOR>
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Postal Code
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=zip VALUE="<TMPL_VAR NAME=ZIP>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Phone Number
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=phone VALUE="<TMPL_VAR NAME=PHONE>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Secondary Phone Number
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=phone2 VALUE="<TMPL_VAR NAME=PHONE2>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Email
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=20 NAME=email VALUE="<TMPL_VAR NAME=EMAIL>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Website
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=40 NAME=website VALUE="<TMPL_VAR NAME=WEBSITE>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Intranet URL
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=TEXT SIZE=40 NAME=intranet_url VALUE="<TMPL_VAR NAME=INTRANET_URL>">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Logo
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TMPL_VAR NAME=LOGO_LINK>
+ Upload New Logo: <INPUT TYPE=FILE SIZE=10 NAME=new_logo>
+ <INPUT TYPE=SUBMIT NAME=logoUpload VALUE="Upload">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Photo
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TMPL_VAR NAME=PHOTO_LINK>
+ Upload New Photo: <INPUT TYPE=FILE SIZE=10 NAME=new_photo>
+ <INPUT TYPE=SUBMIT NAME=photoUpload VALUE="Upload">
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Subcategories
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TEXTAREA NAME=subcategories ROWS=4 COLS=40><TMPL_VAR NAME=SUBCATEGORIES></TEXTAREA>
+ </TD>
+ </TR>
+
+ <TR><TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ Description
+ </TD>
+ <TD><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TEXTAREA NAME=description ROWS=4 COLS=40><TMPL_VAR NAME=DESCRIPTION></TEXTAREA>
+ </TD>
+ </TR>
+ <TR><TD COLSPAN=2 ALIGN=CENTER><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <INPUT TYPE=SUBMIT NAME=unused VALUE=Update>
+ </FORM>
+ <FORM ACTION=office.cgi METHOD=POST>
+ <INPUT TYPE=HIDDEN NAME=office_id VALUE=<TMPL_VAR NAME=OFFICE_ID>>
+ <INPUT TYPE=HIDDEN NAME=company_id VALUE=<TMPL_VAR NAME=COMPANY_ID>>
+ <TMPL_VAR NAME=REMOVE_BUTTON>
+ </FORM>
+ </TD>
+ </TR>
+</TABLE>
+</TD></TR>
+
+<TR><TD COLSPAN=2>
+<FONT FACE="Arial, Helvetica, Sans-Serif" COLOR=#FFFFFF> Case Study
+</TD></TR>
+<TR><TD BGCOLOR = #FFFFFF><FONT FACE="Arial, Helvetica, Sans-Serif">
+ <TABLE BORDER=1 BGCOLOR=#FFFFFF WIDTH=100%>
+ <TR>
+ <TD></TD>
+ <TD>
+ <FONT FACE="Arial, Helvetica, Sans-Serif"> <B> Client </B>
+ </TD>
+ <TD>
+ <FONT FACE="Arial, Helvetica, Sans-Serif"> <B> Brands </B>
+ </TD>
+ <TD>
+ <FONT FACE="Arial, Helvetica, Sans-Serif"> <B> Account Manager </B>
+ </TD>
+ </TR>
+<TMPL_VAR NAME=CASESTUDIES_LIST>
+<TR><TD></TD><TD COLSPAN=3><FONT FACE="Arial, Helvetica, Sans-Serif">
+<A HREF=casestudy.cgi?command=Create&office_id=<TMPL_VAR NAME=OFFICE_ID>> Add a Case Study </A>
+</TD>
+</TABLE>
+
+</TD></TR>
+<TR><TD COLSPAN=2>
+<FONT FACE="Arial, Helvetica, Sans-Serif" COLOR=#FFFFFF> Contacts
+</TD></TR>
+<TR><TD BGCOLOR = #FFFFFF><FONT FACE="Arial, Helvetica, Sans-Serif">
+<FORM ACTION=contact.cgi METHOD=POST>
+<INPUT TYPE=HIDDEN NAME=command value=manage>
+<INPUT TYPE=HIDDEN NAME=office_id value=<TMPL_VAR NAME=OFFICE_ID>>
+This office has <TMPL_VAR NAME=NUMBER_OF_CONTACTS> Contacts.<BR> <INPUT TYPE=Submit NAME=unused VALUE="Manage Contacts">
+</FORM>
+</TD>
+</TABLE>
+
+</TD></TR>
+
+</TABLE>
+</TD></TR>
+
+<TMPL_VAR NAME=COMPANY_ADMIN_AREA>
+
+</TABLE>
+</BODY>
+</HTML>
diff --git a/cpan/dist/HTML-Template/templates/multiline_tags.tmpl b/cpan/dist/HTML-Template/templates/multiline_tags.tmpl new file mode 100644 index 00000000..3a9b4435 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/multiline_tags.tmpl @@ -0,0 +1,7 @@ +<TMPL_VAR +FOO> +<TMPL_LOOP + +BAR><TMPL_VAR FOO><TMPL_VAR +FOO> +</TMPL_LOOP> diff --git a/cpan/dist/HTML-Template/templates/newline_test1.tmpl b/cpan/dist/HTML-Template/templates/newline_test1.tmpl new file mode 100644 index 00000000..9abc2d55 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/newline_test1.tmpl @@ -0,0 +1 @@ +START<TMPL_INCLUDE newline_test2.tmpl>END diff --git a/cpan/dist/HTML-Template/templates/newline_test2.tmpl b/cpan/dist/HTML-Template/templates/newline_test2.tmpl new file mode 100644 index 00000000..2996fba3 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/newline_test2.tmpl @@ -0,0 +1 @@ +include
\ No newline at end of file diff --git a/cpan/dist/HTML-Template/templates/other-loop.tmpl b/cpan/dist/HTML-Template/templates/other-loop.tmpl new file mode 100644 index 00000000..35bcf18c --- /dev/null +++ b/cpan/dist/HTML-Template/templates/other-loop.tmpl @@ -0,0 +1,7 @@ +OUTSIDE + +<TMPL_LOOP LOOP> +INSIDE <TMPL_VAR NAME=IN> +</TMPL_LOOP> + +<TMPL_VAR NAME=OUT> diff --git a/cpan/dist/HTML-Template/templates/query-test.tmpl b/cpan/dist/HTML-Template/templates/query-test.tmpl new file mode 100644 index 00000000..25ea400d --- /dev/null +++ b/cpan/dist/HTML-Template/templates/query-test.tmpl @@ -0,0 +1,21 @@ + + + <TMPL_LOOP NAME="EXAMPLE_LOOP"> + <TMPL_VAR NAME="BEE"> + </TMPL_LOOP> + + <TMPL_LOOP NAME="EXAMPLE_LOOP"> + <TMPL_VAR NAME="BOP"> + <TMPL_LOOP NAME="EXAMPLE_INNER_LOOP"> + <TMPL_VAR NAME="INNER_BEE"> + <TMPL_VAR NAME="INNER_BOP"> + </TMPL_LOOP> + </TMPL_LOOP> + + <TMPL_LOOP NAME="EXAMPLE_LOOP"> + <TMPL_VAR NAME="BOP"> + <TMPL_VAR NAME="BEE"> + </TMPL_LOOP> + + + <TMPL_VAR NAME=VAR> diff --git a/cpan/dist/HTML-Template/templates/query-test2.tmpl b/cpan/dist/HTML-Template/templates/query-test2.tmpl new file mode 100644 index 00000000..2419870c --- /dev/null +++ b/cpan/dist/HTML-Template/templates/query-test2.tmpl @@ -0,0 +1,12 @@ +<TMPL_LOOP NAME="LOOP_FOO"> + <TMPL_LOOP NAME="LOOP_BAR"> + <TMPL_VAR NAME="FOO"> + <TMPL_VAR NAME="BAR"> + </TMPL_LOOP> +</TMPL_LOOP> + +<TMPL_LOOP NAME="LOOP_FOO"> + <TMPL_LOOP NAME="LOOP_BAR"> + <TMPL_VAR NAME="BASH"> + </TMPL_LOOP> +</TMPL_LOOP> diff --git a/cpan/dist/HTML-Template/templates/recursive.tmpl b/cpan/dist/HTML-Template/templates/recursive.tmpl new file mode 100644 index 00000000..4fcadb08 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/recursive.tmpl @@ -0,0 +1,2 @@ +I am infinity. +<TMPL_INCLUDE NAME="recursive.tmpl"> diff --git a/cpan/dist/HTML-Template/templates/searchpath/included.tmpl b/cpan/dist/HTML-Template/templates/searchpath/included.tmpl new file mode 100644 index 00000000..7cd4d9bf --- /dev/null +++ b/cpan/dist/HTML-Template/templates/searchpath/included.tmpl @@ -0,0 +1,4 @@ +9 +9 +9 +<TMPL_INCLUDE NAME="included2.tmpl"> diff --git a/cpan/dist/HTML-Template/templates/searchpath/three.tmpl b/cpan/dist/HTML-Template/templates/searchpath/three.tmpl new file mode 100644 index 00000000..0aa1a365 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/searchpath/three.tmpl @@ -0,0 +1 @@ +THREE diff --git a/cpan/dist/HTML-Template/templates/searchpath/two.tmpl b/cpan/dist/HTML-Template/templates/searchpath/two.tmpl new file mode 100644 index 00000000..c92e2718 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/searchpath/two.tmpl @@ -0,0 +1,2 @@ +TWO +<tmpl_include three.tmpl> diff --git a/cpan/dist/HTML-Template/templates/simple-loop-nonames.tmpl b/cpan/dist/HTML-Template/templates/simple-loop-nonames.tmpl new file mode 100644 index 00000000..d7b7336b --- /dev/null +++ b/cpan/dist/HTML-Template/templates/simple-loop-nonames.tmpl @@ -0,0 +1,13 @@ +<HTML>
+<HEAD>
+<TITLE>
+Simple Template
+</TITLE>
+<BODY>
+I am a
+<TMPL_LOOP ADJECTIVE_LOOP>
+<TMPL_VAR "ADJECTIVE">
+</TMPL_LOOP>
+simple loop template.
+</BODY>
+</HTML>
diff --git a/cpan/dist/HTML-Template/templates/simple-loop.tmpl b/cpan/dist/HTML-Template/templates/simple-loop.tmpl new file mode 100644 index 00000000..3b902062 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/simple-loop.tmpl @@ -0,0 +1,12 @@ +<HTML> +<HEAD> +<TITLE> +Simple Template +</TITLE> +<BODY> +I am a +<TMPL_LOOP NAME=ADJECTIVE_LOOP><TMPL_VAR NAME="ADJECTIVE"> +</TMPL_LOOP> +simple loop template. +</BODY> +</HTML> diff --git a/cpan/dist/HTML-Template/templates/simple.tmpl b/cpan/dist/HTML-Template/templates/simple.tmpl new file mode 100644 index 00000000..35aaa922 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/simple.tmpl @@ -0,0 +1,9 @@ +<HTML>
+<HEAD>
+<TITLE>
+Simple Template
+</TITLE>
+<BODY>
+IIII am a <TMPL_VAR NAME="ADJECTIVE"> simple template.
+</BODY>
+</HTML>
diff --git a/cpan/dist/HTML-Template/templates/unless.tmpl b/cpan/dist/HTML-Template/templates/unless.tmpl new file mode 100644 index 00000000..7c2b0056 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/unless.tmpl @@ -0,0 +1,5 @@ +<TMPL_UNLESS BOOL> + INSIDE UNLESS +<TMPL_ELSE> + INSIDE ELSE +</TMPL_UNLESS> diff --git a/cpan/dist/HTML-Template/templates/urlescape.tmpl b/cpan/dist/HTML-Template/templates/urlescape.tmpl new file mode 100644 index 00000000..eeafca21 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/urlescape.tmpl @@ -0,0 +1,3 @@ +Some URL escaped stuff: +<TMPL_VAR ESCAPE=URL STUFF> + diff --git a/cpan/dist/HTML-Template/templates/vanguard1.tmpl b/cpan/dist/HTML-Template/templates/vanguard1.tmpl new file mode 100644 index 00000000..d87d5c02 --- /dev/null +++ b/cpan/dist/HTML-Template/templates/vanguard1.tmpl @@ -0,0 +1,2 @@ +%foo% +<TMPL_INCLUDE vanguard2.tmpl> diff --git a/cpan/dist/HTML-Template/templates/vanguard2.tmpl b/cpan/dist/HTML-Template/templates/vanguard2.tmpl new file mode 100644 index 00000000..001045fe --- /dev/null +++ b/cpan/dist/HTML-Template/templates/vanguard2.tmpl @@ -0,0 +1,3 @@ +%baz% +sdfsdgsdhsgf + diff --git a/cpan/dist/HTML-Template/test.pl b/cpan/dist/HTML-Template/test.pl new file mode 100644 index 00000000..c9bf32ca --- /dev/null +++ b/cpan/dist/HTML-Template/test.pl @@ -0,0 +1,719 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +use strict; +use Test; +BEGIN { plan tests => 55 }; + +use HTML::Template; +ok(1); + +my ($output, $template, $result); + +# test a simple template +$template = HTML::Template->new( + path => 'templates', + filename => 'simple.tmpl', + debug => 0 + ); + +$template->param('ADJECTIVE', 'very'); +$output = $template->output; +ok($output !~ /ADJECTIVE/ and $template->param('ADJECTIVE') eq 'very'); + +# try something a bit larger +$template = HTML::Template->new( + path => 'templates', + filename => 'medium.tmpl', + # debug => 1, + ); +$template->param('ALERT', 'I am alert.'); +$template->param('COMPANY_NAME', "MY NAME IS"); +$template->param('COMPANY_ID', "10001"); +$template->param('OFFICE_ID', "10103214"); +$template->param('NAME', 'SAM I AM'); +$template->param('ADDRESS', '101011 North Something Something'); +$template->param('CITY', 'NEW York'); +$template->param('STATE', 'NEw York'); +$template->param('ZIP','10014'); +$template->param('PHONE','212-929-4315'); +$template->param('PHONE2',''); +$template->param('SUBCATEGORIES','kfldjaldsf'); +$template->param('DESCRIPTION',"dsa;kljkldasfjkldsajflkjdsfklfjdsgkfld\nalskdjklajsdlkajfdlkjsfd\n\talksjdklajsfdkljdsf\ndsa;klfjdskfj"); +$template->param('WEBSITE','http://www.assforyou.com/'); +$template->param('INTRANET_URL','http://www.something.com'); +$template->param('REMOVE_BUTTON', "<INPUT TYPE=SUBMIT NAME=command VALUE=\"Remove Office\">"); +$template->param('COMPANY_ADMIN_AREA', "<A HREF=administrator.cgi?office_id={office_id}&command=manage>Manage Office Administrators</A>"); +$template->param('CASESTUDIES_LIST', "adsfkljdskldszfgfdfdsgdsfgfdshghdmfldkgjfhdskjfhdskjhfkhdsakgagsfjhbvdsaj hsgbf jhfg sajfjdsag ffasfj hfkjhsdkjhdsakjfhkj kjhdsfkjhdskfjhdskjfkjsda kjjsafdkjhds kjds fkj skjh fdskjhfkj kj kjhf kjh sfkjhadsfkj hadskjfhkjhs ajhdsfkj akj fkj kj kj kkjdsfhk skjhadskfj haskjh fkjsahfkjhsfk ksjfhdkjh sfkjhdskjfhakj shiou weryheuwnjcinuc 3289u4234k 5 i 43iundsinfinafiunai saiufhiudsaf afiuhahfwefna uwhf u auiu uh weiuhfiuh iau huwehiucnaiuncianweciuninc iuaciun iucniunciunweiucniuwnciwe"); +$template->param('NUMBER_OF_CONTACTS', "aksfjdkldsajfkljds"); +$template->param('COUNTRY_SELECTOR', "klajslkjdsafkljds"); +$template->param('LOGO_LINK', "dsfpkjdsfkgljdsfkglj"); +$template->param('PHOTO_LINK', "lsadfjlkfjdsgkljhfgklhasgh"); + +$output = $template->output; +ok($output !~ /<TMPL_VAR/); + +# test a simple loop template +$template = HTML::Template->new( + path => 'templates', + filename => 'simple-loop.tmpl', + # debug => 1, + ); +$template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + +$output = $template->output; +ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s); + +# test a simple loop template +$template = HTML::Template->new( + path => 'templates', + filename => 'simple-loop-nonames.tmpl', + # debug => 1, + ); +$template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + +$output = $template->output; +ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s); + +# test a long loop template - mostly here to use timing on. +$template = HTML::Template->new( + path => 'templates', + filename => 'long_loops.tmpl', + # debug => 1, + ); +$output = $template->output; +ok(1); + +# test a template with TMPL_INCLUDE +$template = HTML::Template->new( + path => 'templates', + filename => 'include.tmpl', + # debug => 1, + ); +$output = $template->output; +ok(not (!($output =~ /5/) || !($output =~ /6/))); + +# test a template with TMPL_INCLUDE and cacheing. +$template = HTML::Template->new( + path => 'templates', + filename => 'include.tmpl', + cache => 1, + # cache_debug => 1, + # debug => 1, + ); +$output = $template->output; +ok(not (!($output =~ /5/) || !($output =~ /6/))); + +# stimulates a cache miss +# system('touch templates/included2.tmpl'); + +my $template2 = HTML::Template->new( + path => 'templates', + filename => 'include.tmpl', + cache => 1, + # cache_debug => 1, + # debug => 1, + ); +$output = $template->output; +ok(not (!($output =~ /5/) || !($output =~ /6/))); + +# test associate +my $template_one = HTML::Template->new( + path => 'templates', + filename => 'simple.tmpl', + # debug => 1, + ); +$template_one->param('ADJECTIVE', 'very'); + +my $template_two = HTML::Template->new ( + path => 'templates', + filename => 'simple.tmpl', + associate => $template_one, + # debug => 1, + ); + +$output = $template_two->output; +ok($output !~ /ADJECTIVE/ and $output =~ /very/); + +# test a simple loop template +my $template_l = HTML::Template->new( + path => 'templates', + filename => 'other-loop.tmpl', + # debug => 1, + ); +# $template_l->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + +$output = $template_l->output; +ok($output !~ /INSIDE/); + +# test a simple if template +my $template_i = HTML::Template->new( + path => 'templates', + filename => 'if.tmpl', + # debug => 1, + ); +# $template_l->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + +$output = $template_i->output; +ok($output !~ /INSIDE/); + +# test a simple if template +my $template_i2 = HTML::Template->new( + path => 'templates', + filename => 'if.tmpl', + # debug => 1, + ); +$template_i2->param(BOOL => 1); + +$output = $template_i2->output; +ok($output =~ /INSIDE/); + +# test a simple if/else template +my $template_ie = HTML::Template->new( + path => 'templates', + filename => 'ifelse.tmpl', + # debug => 1, + ); + +$output = $template_ie->output; +ok ($output =~ /INSIDE ELSE/); + +# test a simple if/else template +my $template_ie2 = HTML::Template->new( + path => 'templates', + filename => 'ifelse.tmpl', + # debug => 1, + ); +$template_ie2->param(BOOL => 1); + +$output = $template_ie2->output; +ok($output =~ /INSIDE IF/ and $output !~ /INSIDE ELSE/); + +# test a bug involving two loops with the same name +$template = HTML::Template->new( + path => 'templates', + filename => 'double_loop.tmpl', + # debug => 1, + ); +$template->param('myloop', [ + { var => 'first'}, + { var => 'second' }, + { var => 'third' } + ] + ); +$output = $template->output; +ok($output =~ /David/); + +# test escapeing +$template = HTML::Template->new( + path => 'templates', + filename => 'escape.tmpl', + # debug => 1, + ); +$template->param(STUFF => '<>"\''); #" +$output = $template->output; +ok($output !~ /[<>"']/); #" + +# test a simple template, using new param() call format +$template = HTML::Template->new( + path => 'templates', + filename => 'simple.tmpl', + # debug => 1, + ); + +$template->param( + { + 'ADJECTIVE' => 'very' + } + ); +$output = $template->output; +ok($output !~ /ADJECTIVE/ and $output =~ /very/); + +# test a recursively including template +eval { + $template = HTML::Template->new( + path => 'templates', + filename => 'recursive.tmpl', + ); + + $output = $template->output; +}; + +ok(defined($@) and ($@ =~ /recursive/)); + +# test a template using unless +$template = HTML::Template->new( + path => 'templates', + filename => 'unless.tmpl', + # debug => 1 + ); +$template->param(BOOL => 1); + +$output = $template->output; +ok($output !~ /INSIDE UNLESS/ and $output =~ /INSIDE ELSE/); + +# test a template using unless +$template = HTML::Template->new( + path => 'templates', + filename => 'unless.tmpl', + #debug => 1, + #debug_stack => 1 + ); +$template->param(BOOL => 0); + +$output = $template->output; +ok($output =~ /INSIDE UNLESS/ and $output !~ /INSIDE ELSE/); + +# test a template using loop_context_vars +$template = HTML::Template->new( + path => 'templates', + filename => 'context.tmpl', + loop_context_vars => 1, + #debug => 1, + #debug_stack => 1 + ); +$template->param(FRUIT => [ + {KIND => 'Apples'}, + {KIND => 'Oranges'}, + {KIND => 'Brains'}, + {KIND => 'Toes'}, + {KIND => 'Kiwi'} + ]); +$template->param(PINGPONG => [ {}, {}, {}, {}, {}, {} ]); + +$output = $template->output; +ok($output =~ /Apples, Oranges, Brains, Toes, and Kiwi./ and $output =~ /pingpongpingpongpingpong/); + +$template = HTML::Template->new( + path => 'templates', + filename => 'loop-if.tmpl', + #debug => 1, + #debug_stack => 1 + ); +$output = $template->output; +ok($output =~ /Loop not filled in/); + +$template = HTML::Template->new( + path => 'templates', + filename => 'loop-if.tmpl', + #debug => 1, + #debug_stack => 1 + ); +$template->param(LOOP_ONE => [{VAR => "foo"}]); +$output = $template->output; +ok($output !~ /Loop not filled in/); + +# test shared memory - enable by setting the environment variable +# TEST_SHARED_MEMORY to 1. +if (!exists($ENV{TEST_SHARED_MEMORY}) or !$ENV{TEST_SHARED_MEMORY}) { + print "Skipping shared memory cache test. See README to enable\n"; + skip(1, 0); + skip(1, 0); +} else { + require 'IPC/SharedCache.pm'; + my $template_prime = HTML::Template->new( + filename => 'simple-loop.tmpl', + path => ['templates/'], + shared_cache => 1, + ipc_key => 'TEST', + #cache_debug => 1, + ); + + my $template = HTML::Template->new( + filename => 'simple-loop.tmpl', + path => ['templates/'], + shared_cache => 1, + ipc_key => 'TEST', + #cache_debug => 1, + ); + $template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + $output = $template->output; + ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s); + + my $template_prime2 = HTML::Template->new( + filename => 'simple-loop.tmpl', + path => ['templates/'], + double_cache => 1, + ipc_key => 'TEST', + #cache_debug => 1, + ); + + my $template2 = HTML::Template->new( + filename => 'simple-loop.tmpl', + path => ['templates/'], + double_cache => 1, + ipc_key => 'TEST', + #cache_debug => 1, + ); + $template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + $output = $template->output; + ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s); + + IPC::SharedCache::remove('TEST'); +} + +# test CGI associate bug +eval { require 'CGI.pm'; }; +if ($@) { + print "Skipping associate tests, need CGI.pm to test associate\n"; + skip(1, 0); +} else { + my $query = CGI->new(''); + $query->param('AdJecTivE' => 'very'); + $template = HTML::Template->new( + path => 'templates', + filename => 'simple.tmpl', + debug => 0, + associate => $query, + ); + $output = $template->output; + ok($output =~ /very/); +} + +# test subroutine as VAR +$template = HTML::Template->new( + path => 'templates', + filename => 'simple.tmpl', + debug => 0, + ); +$template->param(ADJECTIVE => sub { return 'v' . '1e' . '2r' . '3y'; }); +$output = $template->output; +ok($output =~ /v1e2r3y/); + +# test cache - non automated, requires turning on debug watching STDERR! +$template = HTML::Template->new( + path => ['templates/'], + filename => 'simple.tmpl', + cache => 1, + # cache_debug => 1, + debug => 0, + ); +$template->param(ADJECTIVE => sub { return 'v' . '1e' . '2r' . '3y'; }); +$output = $template->output; +$template = HTML::Template->new( + path => ['templates/'], + filename => 'simple.tmpl', + cache => 1, + # cache_debug => 1, + debug => 0, + ); +ok($output =~ /v1e2r3y/); + +# test URL escapeing +$template = HTML::Template->new( + path => 'templates', + filename => 'urlescape.tmpl', + # debug => 1, + # stack_debug => 1, + ); +$template->param(STUFF => '<>"; %FA'); #" +$output = $template->output; +ok($output !~ /[<>"]/); #" + +# test query() +$template = HTML::Template->new( + path => 'templates', + filename => 'query-test.tmpl', + ); +my %params = map {$_ => 1} $template->query(loop => 'EXAMPLE_LOOP'); + +my @result; +eval { + @result = $template->query(loop => ['EXAMPLE_LOOP', 'BEE']); +}; + +ok($@ =~ /error/ and + $template->query(name => 'var') eq 'VAR' and + $template->query(name => 'EXAMPLE_LOOP') eq 'LOOP' and + exists $params{bee} and + exists $params{bop} and + exists $params{example_inner_loop} and + $template->query(name => ['EXAMPLE_LOOP', 'EXAMPLE_INNER_LOOP']) eq 'LOOP' + ); + +# test query() +$template = HTML::Template->new( + path => 'templates', + filename => 'query-test2.tmpl', + ); +my %p = map {$_ => 1} $template->query(loop => ['LOOP_FOO', 'LOOP_BAR']); +ok(exists $p{foo} and exists $p{bar} and exists $p{bash}); + +# test global_vars +$template = HTML::Template->new( + path => 'templates', + filename => 'globals.tmpl', + global_vars => 1, + ); +$template->param(outer_loop => [{loop => [{'LOCAL' => 'foo'}]}]); +$template->param(global => 'bar'); +$template->param(hidden_global => 'foo'); +$result = $template->output(); +ok($result =~ /foobar/); + +$template = HTML::Template->new( + path => 'templates', + filename => 'vanguard1.tmpl', + vanguard_compatibility_mode => 1, + ); +$template->param(FOO => 'bar'); +$template->param(BAZ => 'bop'); +$result = $template->output(); +ok($result =~ /bar/ and $result =~ /bop/); + +$template = HTML::Template->new( + path => 'templates', + filename => 'loop-context.tmpl', + loop_context_vars => 1, + ); +$template->param(TEST_LOOP => [ { NUM => 1 } ]); +$result = $template->output(); +ok($result =~ /1:FIRST::LAST:ODD/); + +# test a TMPL_INCLUDE from a later path directory back up to an earlier one +# when using the search_path_on_include option +$template = HTML::Template->new( + path => ['templates/searchpath/','templates/'], + search_path_on_include => 1, + filename => 'include.tmpl', + ); +$output = $template->output; +ok($output =~ /9/ and $output =~ /6/); + +# test no_includes +eval { + $template = HTML::Template->new( + path => ['templates/'], + filename => 'include.tmpl', + no_includes => 1, + ); +}; +ok(defined $@ and $@ =~ /no_includes/); + +# test file cache - non automated, requires turning on debug watching STDERR! +if (!exists($ENV{TEST_FILE_CACHE}) or !$ENV{TEST_FILE_CACHE}) { + print "Skipping file cache test. See README to enable.\n"; + skip(1,0); + skip(1,0); +} else { + $template = HTML::Template->new( + path => ['templates/'], + filename => 'simple.tmpl', + file_cache_dir => './blib/temp_cache_dir', + file_cache => 1, + #cache_debug => 1, + #debug => 0, + ); + $template->param(ADJECTIVE => sub { "3y"; }); + $output = $template->output; + $template = HTML::Template->new( + path => ['templates/'], + filename => 'simple.tmpl', + file_cache_dir => './blib/temp_cache_dir', + file_cache => 1, + #cache_debug => 1, + #debug => 0, + ); + ok($output =~ /3y/); + + my $x; + $template = HTML::Template->new(filename => 'templates/simple-loop.tmpl', + filter => { + sub => sub { + $x = 1; + for (@{$_[0]}) { + $_ = "$x : $_"; + $x++; + } + }, + format => 'array', + }, + file_cache_dir => './blib/temp_cache_dir', + file_cache => 1, + global_vars => 1, + ); +$template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] ); + $output = $template->output; + ok($output =~ /very/); +} + +$template = HTML::Template->new(filename => './templates/include_path/a.tmpl'); +$output = $template->output; +ok($output =~ /Bar/); + +open(OUT, ">blib/test.out") or die $!; +$template = HTML::Template->new(filename => './templates/include_path/a.tmpl'); +$template->output(print_to => *OUT); +close(OUT); +open(OUT, "blib/test.out") or die $!; +my $output = join('',<OUT>); +close(OUT); +ok($output =~ /Bar/); + +my $test = 39; # test with case sensitive params on +my $template_source = <<END_OF_TMPL; + I am a <TMPL_VAR NAME="adverb"> <TMPL_VAR NAME="ADVERB"> simple template. +END_OF_TMPL +$template = HTML::Template->new( + scalarref => \$template_source, + case_sensitive => 1, + debug => 0 + ); + +$template->param('adverb', 'very'); +$template->param('ADVERB', 'painfully'); +$output = $template->output; +ok($output !~ /ADVERB/i and + $template->param('ADVERB') eq 'painfully' and + $template->param('adverb') eq 'very' and + $output =~ /very painfully/); + +# test with case sensitive params off +$template_source = <<END_OF_TMPL; + I am a <TMPL_VAR NAME="adverb"> <TMPL_VAR NAME="ADVERB"> simple template. +END_OF_TMPL +$template = HTML::Template->new( + scalarref => \$template_source, + case_sensitive => 0, + debug => 0 + ); + +$template->param('adverb', 'very'); +$template->param('ADVERB', 'painfully'); +$output = $template->output; +ok($output !~ /ADVERB/i and + $template->param('ADVERB') eq 'painfully' and + $template->param('adverb') eq 'painfully' and + $output =~ /painfully painfully/); + +$template = HTML::Template->new(filename => './templates/include_path/a.tmpl', + filter => sub { + ${$_[0]} =~ s/Bar/Zanzabar/g; + } + ); +$output = $template->output; +ok($output =~ /Zanzabar/); + +$template = HTML::Template->new(filename => './templates/include_path/a.tmpl', + filter => [ + { + sub => sub { + ${$_[0]} =~ s/Bar/Zanzabar/g; + }, + format => 'scalar' + }, + { + sub => sub { + ${$_[0]} =~ s/bar/bar!!!/g; + }, + format => 'scalar' + } + ] + ); +$output = $template->output; +ok($output =~ /Zanzabar!!!/); + +my $x; +$template = HTML::Template->new(filename => './templates/include_path/a.tmpl', + filter => { + sub => sub { + $x = 1; + for (@{$_[0]}) { + $_ = "$x : $_"; + $x++; + } + }, + format => 'array', + } + ); +$output = $template->output; +ok($output =~ /1 : Foo/); + +$template = HTML::Template->new( + scalarref => \ "\n<TMPL_INCLUDE templates/simple.tmpl>", + ); +$template->param(ADJECTIVE => "3y"); +$output = $template->output(); +ok($output =~ /3y/); + +$template = HTML::Template->new(path => ['templates'], + filename => 'newline_test1.tmpl', + filter => sub {}, + ); +$output = $template->output(); +ok($output =~ /STARTincludeEND/); + +# test multiline tags +$template = HTML::Template->new(path => ['templates'], + filename => 'multiline_tags.tmpl', + global_vars => 1, + ); +$template->param(FOO => 'foo!', bar => [{}, {}]); +$output = $template->output(); +ok($output =~ /foo!\n/ && $output =~ /foo!foo!\nfoo!foo!/); + +# test new() from filehandle +open(TEMPLATE, "templates/simple.tmpl"); +$template = HTML::Template->new(filehandle => *TEMPLATE); + +$template->param('ADJECTIVE', 'very'); +$output = $template->output; +ok($output !~ /ADJECTIVE/ and $template->param('ADJECTIVE') eq 'very'); +close(TEMPLATE); + +# test new_() from filehandle +open(TEMPLATE, "templates/simple.tmpl"); +$template = HTML::Template->new_filehandle(*TEMPLATE); + +$template->param('ADJECTIVE', 'very'); +$output = $template->output; +ok($output !~ /ADJECTIVE/ and $template->param('ADJECTIVE') eq 'very'); +close(TEMPLATE); + +# test case sensitive loop variables +$template = HTML::Template->new(path => ['templates'], + filename => 'case_loop.tmpl', + case_sensitive => 1, + ); +$template->param(loop => [ { foo => 'bar', FOO => 'BAR' } ]); +$output = $template->output(); +ok($output =~ /bar BAR/); + +# test ifs with code refd +$template = HTML::Template->new(path => ['templates'], + filename => 'if.tmpl'); +$template->param(bool => sub { 0 }); +$output = $template->output(); +ok($output !~ /INSIDE/ and $output =~ /unless/); + +# test global_vars for loops within loops +$template = HTML::Template->new(path => ['templates'], + filename => 'global-loops.tmpl', + global_vars => 1); +$template->param(global => "global val"); +$template->param(outer_loop => [ + { + foo => 'foo val 1', + inner_loop => [ + { bar => 'bar val 1' }, + { bar => 'bar val 2' }, + ], + }, + { + foo => 'foo val 2', + inner_loop => [ + { bar => 'bar val 3' }, + { bar => 'bar val 4' }, + ], + } + ]); +$output = $template->output; +ok($output =~ /inner loop foo: foo val 1/ and + $output =~ /inner loop foo: foo val 2/); + + +# test nested include path handling +my $template = HTML::Template->new(path => ['templates'], + filename => 'include_path/one.tmpl'); +$output = $template->output; +ok($output =~ /ONE/ and $output =~ /TWO/ and $output =~ /THREE/); diff --git a/cpan/lib/HTML/Template.pm b/cpan/lib/HTML/Template.pm new file mode 100644 index 00000000..b44802e5 --- /dev/null +++ b/cpan/lib/HTML/Template.pm @@ -0,0 +1,3137 @@ +package HTML::Template; + +$HTML::Template::VERSION = '2.5'; + +=head1 NAME + +HTML::Template - Perl module to use HTML Templates from CGI scripts + +=head1 SYNOPSIS + +First you make a template - this is just a normal HTML file with a few +extra tags, the simplest being <TMPL_VAR> + +For example, test.tmpl: + + <html> + <head><title>Test Template</title> + <body> + My Home Directory is <TMPL_VAR NAME=HOME> + <p> + My Path is set to <TMPL_VAR NAME=PATH> + </body> + </html> + +Now create a small CGI program: + + #!/usr/bin/perl -w + use HTML::Template; + + # open the html template + my $template = HTML::Template->new(filename => 'test.tmpl'); + + # fill in some parameters + $template->param(HOME => $ENV{HOME}); + $template->param(PATH => $ENV{PATH}); + + # send the obligatory Content-Type and print the template output + print "Content-Type: text/html\n\n", $template->output; + +If all is well in the universe this should show something like this in +your browser when visiting the CGI: + + My Home Directory is /home/some/directory + My Path is set to /bin;/usr/bin + +=head1 DESCRIPTION + +This module attempts to make using HTML templates simple and natural. +It extends standard HTML with a few new HTML-esque tags - <TMPL_VAR>, +<TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF>, <TMPL_ELSE> and <TMPL_UNLESS>. +The file written with HTML and these new tags is called a template. +It is usually saved separate from your script - possibly even created +by someone else! Using this module you fill in the values for the +variables, loops and branches declared in the template. This allows +you to separate design - the HTML - from the data, which you generate +in the Perl script. + +This module is licensed under the GPL. See the LICENSE section +below for more details. + +=head1 TUTORIAL + +If you're new to HTML::Template, I suggest you start with the +introductory article available on the HTML::Template website: + + http://html-template.sourceforge.net + +=head1 MOTIVATION + +It is true that there are a number of packages out there to do HTML +templates. On the one hand you have things like HTML::Embperl which +allows you freely mix Perl with HTML. On the other hand lie +home-grown variable substitution solutions. Hopefully the module can +find a place between the two. + +One advantage of this module over a full HTML::Embperl-esque solution +is that it enforces an important divide - design and programming. By +limiting the programmer to just using simple variables and loops in +the HTML, the template remains accessible to designers and other +non-perl people. The use of HTML-esque syntax goes further to make +the format understandable to others. In the future this similarity +could be used to extend existing HTML editors/analyzers to support +HTML::Template. + +An advantage of this module over home-grown tag-replacement schemes is +the support for loops. In my work I am often called on to produce +tables of data in html. Producing them using simplistic HTML +templates results in CGIs containing lots of HTML since the HTML +itself cannot represent loops. The introduction of loop statements in +the HTML simplifies this situation considerably. The designer can +layout a single row and the programmer can fill it in as many times as +necessary - all they must agree on is the parameter names. + +For all that, I think the best thing about this module is that it does +just one thing and it does it quickly and carefully. It doesn't try +to replace Perl and HTML, it just augments them to interact a little +better. And it's pretty fast. + +=head1 THE TAGS + +=head2 TMPL_VAR + + <TMPL_VAR NAME="PARAMETER_NAME"> + +The <TMPL_VAR> tag is very simple. For each <TMPL_VAR> tag in the +template you call $template->param(PARAMETER_NAME => "VALUE"). When +the template is output the <TMPL_VAR> is replaced with the VALUE text +you specified. If you don't set a parameter it just gets skipped in +the output. + +Optionally you can use the "ESCAPE=HTML" option in the tag to indicate +that you want the value to be HTML-escaped before being returned from +output (the old ESCAPE=1 syntax is still supported). This means that +the ", <, >, and & characters get translated into ", <, > +and & respectively. This is useful when you want to use a +TMPL_VAR in a context where those characters would cause trouble. +Example: + + <input name=param type=text value="<TMPL_VAR NAME="PARAM">"> + +If you called param() with a value like sam"my you'll get in trouble +with HTML's idea of a double-quote. On the other hand, if you use +ESCAPE=HTML, like this: + + <input name=param type=text value="<TMPL_VAR ESCAPE=HTML NAME="PARAM">"> + +You'll get what you wanted no matter what value happens to be passed in for +param. You can also write ESCAPE="HTML", ESCAPE='HTML' and ESCAPE='1'. +Substitute a 0 for the HTML and you turn off escaping, which is the default +anyway. + +There is also the "ESCAPE=URL" option which may be used for VARs that +populate a URL. It will do URL escaping, like replacing ' ' with '+' +and '/' with '%2F'. + +=head2 TMPL_LOOP + + <TMPL_LOOP NAME="LOOP_NAME"> ... </TMPL_LOOP> + +The <TMPL_LOOP> tag is a bit more complicated than <TMPL_VAR>. The +<TMPL_LOOP> tag allows you to delimit a section of text and give it a +name. Inside this named loop you place <TMPL_VAR>s. Now you pass to +param() a list (an array ref) of parameter assignments (hash refs) for +this loop. The loop iterates over the list and produces output from +the text block for each pass. Unset parameters are skipped. Here's +an example: + + In the template: + + <TMPL_LOOP NAME=EMPLOYEE_INFO> + Name: <TMPL_VAR NAME=NAME> <br> + Job: <TMPL_VAR NAME=JOB> <p> + </TMPL_LOOP> + + + In the script: + + $template->param(EMPLOYEE_INFO => [ + { name => 'Sam', job => 'programmer' }, + { name => 'Steve', job => 'soda jerk' }, + ] + ); + print $template->output(); + + + The output in a browser: + + Name: Sam + Job: programmer + + Name: Steve + Job: soda jerk + +As you can see above the <TMPL_LOOP> takes a list of variable +assignments and then iterates over the loop body producing output. + +Often you'll want to generate a <TMPL_LOOP>'s contents +programmatically. Here's an example of how this can be done (many +other ways are possible!): + + # a couple of arrays of data to put in a loop: + my @words = qw(I Am Cool); + my @numbers = qw(1 2 3); + + my @loop_data = (); # initialize an array to hold your loop + + while (@words and @numbers) { + my %row_data; # get a fresh hash for the row data + + # fill in this row + $row_data{WORD} = shift @words; + $row_data{NUMBER} = shift @numbers; + + # the crucial step - push a reference to this row into the loop! + push(@loop_data, \%row_data); + } + + # finally, assign the loop data to the loop param, again with a + # reference: + $template->param(THIS_LOOP => \@loop_data); + +The above example would work with a template like: + + <TMPL_LOOP NAME="THIS_LOOP"> + Word: <TMPL_VAR NAME="WORD"> <br> + Number: <TMPL_VAR NAME="NUMBER"> <p> + </TMPL_LOOP> + +It would produce output like: + + Word: I + Number: 1 + + Word: Am + Number: 2 + + Word: Cool + Number: 3 + +<TMPL_LOOP>s within <TMPL_LOOP>s are fine and work as you would +expect. If the syntax for the param() call has you stumped, here's an +example of a param call with one nested loop: + + $template->param(LOOP => [ + { name => 'Bobby', + nicknames => [ + { name => 'the big bad wolf' }, + { name => 'He-Man' }, + ], + }, + ], + ); + +Basically, each <TMPL_LOOP> gets an array reference. Inside the array +are any number of hash references. These hashes contain the +name=>value pairs for a single pass over the loop template. + +Inside a <TMPL_LOOP>, the only variables that are usable are the ones +from the <TMPL_LOOP>. The variables in the outer blocks are not +visible within a template loop. For the computer-science geeks among +you, a <TMPL_LOOP> introduces a new scope much like a perl subroutine +call. If you want your variables to be global you can use +'global_vars' option to new() described below. + +=head2 TMPL_INCLUDE + + <TMPL_INCLUDE NAME="filename.tmpl"> + +This tag includes a template directly into the current template at the +point where the tag is found. The included template contents are used +exactly as if its contents were physically included in the master +template. + +The file specified can be an absolute path (beginning with a '/' under +Unix, for example). If it isn't absolute, the path to the enclosing +file is tried first. After that the path in the environment variable +HTML_TEMPLATE_ROOT is tried, if it exists. Next, the "path" option is +consulted. As a final attempt, the filename is passed to open() +directly. See below for more information on HTML_TEMPLATE_ROOT and +the "path" option to new(). + +As a protection against infinitly recursive includes, an arbitary +limit of 10 levels deep is imposed. You can alter this limit with the +"max_includes" option. See the entry for the "max_includes" option +below for more details. + +=head2 TMPL_IF + + <TMPL_IF NAME="PARAMETER_NAME"> ... </TMPL_IF> + +The <TMPL_IF> tag allows you to include or not include a block of the +template based on the value of a given parameter name. If the +parameter is given a value that is true for Perl - like '1' - then the +block is included in the output. If it is not defined, or given a +false value - like '0' - then it is skipped. The parameters are +specified the same way as with TMPL_VAR. + +Example Template: + + <TMPL_IF NAME="BOOL"> + Some text that only gets displayed if BOOL is true! + </TMPL_IF> + +Now if you call $template->param(BOOL => 1) then the above block will +be included by output. + +<TMPL_IF> </TMPL_IF> blocks can include any valid HTML::Template +construct - VARs and LOOPs and other IF/ELSE blocks. Note, however, +that intersecting a <TMPL_IF> and a <TMPL_LOOP> is invalid. + + Not going to work: + <TMPL_IF BOOL> + <TMPL_LOOP SOME_LOOP> + </TMPL_IF> + </TMPL_LOOP> + +If the name of a TMPL_LOOP is used in a TMPL_IF, the IF block will +output if the loop has at least one row. Example: + + <TMPL_IF LOOP_ONE> + This will output if the loop is not empty. + </TMPL_IF> + + <TMPL_LOOP LOOP_ONE> + .... + </TMPL_LOOP> + +WARNING: Much of the benefit of HTML::Template is in decoupling your +Perl and HTML. If you introduce numerous cases where you have +TMPL_IFs and matching Perl if()s, you will create a maintenance +problem in keeping the two synchronized. I suggest you adopt the +practice of only using TMPL_IF if you can do so without requiring a +matching if() in your Perl code. + +=head2 TMPL_ELSE + + <TMPL_IF NAME="PARAMETER_NAME"> ... <TMPL_ELSE> ... </TMPL_IF> + +You can include an alternate block in your TMPL_IF block by using +TMPL_ELSE. NOTE: You still end the block with </TMPL_IF>, not +</TMPL_ELSE>! + + Example: + + <TMPL_IF BOOL> + Some text that is included only if BOOL is true + <TMPL_ELSE> + Some text that is included only if BOOL is false + </TMPL_IF> + +=head2 TMPL_UNLESS + + <TMPL_UNLESS NAME="PARAMETER_NAME"> ... </TMPL_UNLESS> + +This tag is the opposite of <TMPL_IF>. The block is output if the +CONTROL_PARAMETER is set false or not defined. You can use +<TMPL_ELSE> with <TMPL_UNLESS> just as you can with <TMPL_IF>. + + Example: + + <TMPL_UNLESS BOOL> + Some text that is output only if BOOL is FALSE. + <TMPL_ELSE> + Some text that is output only if BOOL is TRUE. + </TMPL_UNLESS> + +If the name of a TMPL_LOOP is used in a TMPL_UNLESS, the UNLESS block +output if the loop has zero rows. + + <TMPL_UNLESS LOOP_ONE> + This will output if the loop is empty. + </TMPL_UNLESS> + + <TMPL_LOOP LOOP_ONE> + .... + </TMPL_LOOP> + +=cut + +=head2 NOTES + +HTML::Template's tags are meant to mimic normal HTML tags. However, +they are allowed to "break the rules". Something like: + + <img src="<TMPL_VAR IMAGE_SRC>"> + +is not really valid HTML, but it is a perfectly valid use and will +work as planned. + +The "NAME=" in the tag is optional, although for extensibility's sake I +recommend using it. Example - "<TMPL_LOOP LOOP_NAME>" is acceptable. + +If you're a fanatic about valid HTML and would like your templates +to conform to valid HTML syntax, you may optionally type template tags +in the form of HTML comments. This may be of use to HTML authors who +would like to validate their templates' HTML syntax prior to +HTML::Template processing, or who use DTD-savvy editing tools. + + <!-- TMPL_VAR NAME=PARAM1 --> + +In order to realize a dramatic savings in bandwidth, the standard +(non-comment) tags will be used throughout this documentation. + +=head1 METHODS + +=head2 new() + +Call new() to create a new Template object: + + my $template = HTML::Template->new( filename => 'file.tmpl', + option => 'value' + ); + +You must call new() with at least one name => value pair specifying how +to access the template text. You can use "filename => 'file.tmpl'" to +specify a filename to be opened as the template. Alternately you can +use: + + my $t = HTML::Template->new( scalarref => $ref_to_template_text, + option => 'value' + ); + +and + + my $t = HTML::Template->new( arrayref => $ref_to_array_of_lines , + option => 'value' + ); + + +These initialize the template from in-memory resources. In almost +every case you'll want to use the filename parameter. If you're +worried about all the disk access from reading a template file just +use mod_perl and the cache option detailed below. + +You can also read the template from an already opened filehandle, +either traditionally as a glob or as a FileHandle: + + my $t = HTML::Template->new( filehandle => *FH, option => 'value'); + +The four new() calling methods can also be accessed as below, if you +prefer. + + my $t = HTML::Template->new_file('file.tmpl', option => 'value'); + + my $t = HTML::Template->new_scalar_ref($ref_to_template_text, + option => 'value'); + + my $t = HTML::Template->new_array_ref($ref_to_array_of_lines, + option => 'value'); + + my $t = HTML::Template->new_filehandle($fh, + option => 'value'); + +And as a final option, for those that might prefer it, you can call new as: + + my $t = HTML::Template->new(type => 'filename', + source => 'file.tmpl'); + +Which works for all three of the source types. + +If the environment variable HTML_TEMPLATE_ROOT is set and your +filename doesn't begin with /, then the path will be relative to the +value of $HTML_TEMPLATE_ROOT. Example - if the environment variable +HTML_TEMPLATE_ROOT is set to "/home/sam" and I call +HTML::Template->new() with filename set to "sam.tmpl", the +HTML::Template will try to open "/home/sam/sam.tmpl" to access the +template file. You can also affect the search path for files with the +"path" option to new() - see below for more information. + +You can modify the Template object's behavior with new. These options +are available: + +=over 4 + +=item Error Detection Options + +=over 4 + +=item * + +die_on_bad_params - if set to 0 the module will let you call +$template->param(param_name => 'value') even if 'param_name' doesn't +exist in the template body. Defaults to 1. + +=item * + +strict - if set to 0 the module will allow things that look like they +might be TMPL_* tags to get by without dieing. Example: + + <TMPL_HUH NAME=ZUH> + +Would normally cause an error, but if you call new with strict => 0, +HTML::Template will ignore it. Defaults to 1. + +=item * + +vanguard_compatibility_mode - if set to 1 the module will expect to +see <TMPL_VAR>s that look like %NAME% in addition to the standard +syntax. Also sets die_on_bad_params => 0. If you're not at Vanguard +Media trying to use an old format template don't worry about this one. +Defaults to 0. + +=back + +=item Caching Options + +=over 4 + +=item * + +cache - if set to 1 the module will cache in memory the parsed +templates based on the filename parameter and modification date of the +file. This only applies to templates opened with the filename +parameter specified, not scalarref or arrayref templates. Caching +also looks at the modification times of any files included using +<TMPL_INCLUDE> tags, but again, only if the template is opened with +filename parameter. + +This is mainly of use in a persistent environment like +Apache/mod_perl. It has absolutely no benefit in a normal CGI +environment since the script is unloaded from memory after every +request. For a cache that does work for normal CGIs see the +'shared_cache' option below. + +Note that different new() parameter settings do not cause a cache +refresh, only a change in the modification time of the template will +trigger a cache refresh. For most usages this is fine. My simplistic +testing shows that using cache yields a 90% performance increase under +mod_perl. Cache defaults to 0. + +=item * + +shared_cache - if set to 1 the module will store its cache in shared +memory using the IPC::SharedCache module (available from CPAN). The +effect of this will be to maintain a single shared copy of each parsed +template for all instances of HTML::Template to use. This can be a +significant reduction in memory usage in a multiple server +environment. As an example, on one of our systems we use 4MB of +template cache and maintain 25 httpd processes - shared_cache results +in saving almost 100MB! Of course, some reduction in speed versus +normal caching is to be expected. Another difference between normal +caching and shared_cache is that shared_cache will work in a CGI +environment - normal caching is only useful in a persistent +environment like Apache/mod_perl. + +By default HTML::Template uses the IPC key 'TMPL' as a shared root +segment (0x4c504d54 in hex), but this can be changed by setting the +'ipc_key' new() parameter to another 4-character or integer key. +Other options can be used to affect the shared memory cache correspond +to IPC::SharedCache options - ipc_mode, ipc_segment_size and +ipc_max_size. See L<IPC::SharedCache> for a description of how these +work - in most cases you shouldn't need to change them from the +defaults. + +For more information about the shared memory cache system used by +HTML::Template see L<IPC::SharedCache>. + +=item * + +double_cache - if set to 1 the module will use a combination of +shared_cache and normal cache mode for the best possible caching. Of +course, it also uses the most memory of all the cache modes. All the +same ipc_* options that work with shared_cache apply to double_cache +as well. By default double_cache is off. + +=item * + +blind_cache - if set to 1 the module behaves exactly as with normal +caching but does not check to see if the file has changed on each +request. This option should be used with caution, but could be of use +on high-load servers. My tests show blind_cache performing only 1 to +2 percent faster than cache under mod_perl. + +NOTE: Combining this option with shared_cache can result in stale +templates stuck permanently in shared memory! + +=item * + +file_cache - if set to 1 the module will store its cache in a file +using the Storable module. It uses no additional memory, and my +simplistic testing shows that it yields a 50% performance advantage. +Like shared_cache, it will work in a CGI environment. Default is 0. + +If you set this option you must set the "file_cache_dir" option. See +below for details. + +NOTE: Storable using flock() to ensure safe access to cache files. +Using file_cache on a system or filesystem (NFS) without flock() +support is dangerous. + + +=item * + +file_cache_dir - sets the directory where the module will store the +cache files if file_cache is enabled. Your script will need write +permissions to this directory. You'll also need to make sure the +sufficient space is available to store the cache files. + +=item * + +file_cache_dir_mode - sets the file mode for newly created file_cache +directories and subdirectories. Defaults to 0700 for security but +this may be inconvenient if you do not have access to the account +running the webserver. + +=item * + +double_file_cache - if set to 1 the module will use a combination of +file_cache and normal cache mode for the best possible caching. The +file_cache_* options that work with file_cache apply to double_file_cache +as well. By default double_file_cache is 0. + +=back + +=item Filesystem Options + +=over 4 + +=item * + +path - you can set this variable with a list of paths to search for +files specified with the "filename" option to new() and for files +included with the <TMPL_INCLUDE> tag. This list is only consulted +when the filename is relative. The HTML_TEMPLATE_ROOT environment +variable is always tried first if it exists. In the case of a +<TMPL_INCLUDE> file, the path to the including file is also tried +before path is consulted. + +Example: + + my $template = HTML::Template->new( filename => 'file.tmpl', + path => [ '/path/to/templates', + '/alternate/path' + ] + ); + +NOTE: the paths in the path list must be expressed as UNIX paths, +separated by the forward-slash character ('/'). + +=item * + +search_path_on_include - if set to a true value the module will search +from the top of the array of paths specified by the path option on +every <TMPL_INCLUDE> and use the first matching template found. The +normal behavior is to look only in the current directory for a +template to include. Defaults to 0. + +=back + +=item Debugging Options + +=over 4 + +=item * + +debug - if set to 1 the module will write random debugging information +to STDERR. Defaults to 0. + +=item * + +stack_debug - if set to 1 the module will use Data::Dumper to print +out the contents of the parse_stack to STDERR. Defaults to 0. + +=item * + +cache_debug - if set to 1 the module will send information on cache +loads, hits and misses to STDERR. Defaults to 0. + +=item * + +shared_cache_debug - if set to 1 the module will turn on the debug +option in IPC::SharedCache - see L<IPC::SharedCache> for +details. Defaults to 0. + +=item * + +memory_debug - if set to 1 the module will send information on cache +memory usage to STDERR. Requires the GTop module. Defaults to 0. + +=back + +=item Miscellaneous Options + +=over 4 + +=item * + +associate - this option allows you to inherit the parameter values +from other objects. The only requirement for the other object is that +it have a param() method that works like HTML::Template's param(). A +good candidate would be a CGI.pm query object. Example: + + my $query = new CGI; + my $template = HTML::Template->new(filename => 'template.tmpl', + associate => $query); + +Now, $template->output() will act as though + + $template->param('FormField', $cgi->param('FormField')); + +had been specified for each key/value pair that would be provided by +the $cgi->param() method. Parameters you set directly take precedence +over associated parameters. + +You can specify multiple objects to associate by passing an anonymous +array to the associate option. They are searched for parameters in +the order they appear: + + my $template = HTML::Template->new(filename => 'template.tmpl', + associate => [$query, $other_obj]); + +The old associateCGI() call is still supported, but should be +considered obsolete. + +NOTE: The parameter names are matched in a case-insensitve manner. If +you have two parameters in a CGI object like 'NAME' and 'Name' one +will be chosen randomly by associate. This behavior can be changed by +the following option. + +=item * + +case_sensitive - setting this option to true causes HTML::Template to +treat template variable names case-sensitively. The following example +would only set one parameter without the "case_sensitive" option: + + my $template = HTML::Template->new(filename => 'template.tmpl', + case_sensitive => 1); + $template->param( + FieldA => 'foo', + fIELDa => 'bar', + ); + +This option defaults to off. + +=item * + +loop_context_vars - when this parameter is set to true (it is false by +default) four loop context variables are made available inside a loop: +__FIRST__, __LAST__, __INNER__, __ODD__. They can be used with +<TMPL_IF>, <TMPL_UNLESS> and <TMPL_ELSE> to control how a loop is +output. Example: + + <TMPL_LOOP NAME="FOO"> + <TMPL_IF NAME="__FIRST__"> + This only outputs on the first pass. + </TMPL_IF> + + <TMPL_IF NAME="__ODD__"> + This outputs every other pass, on the odd passes. + </TMPL_IF> + + <TMPL_UNLESS NAME="__ODD__"> + This outputs every other pass, on the even passes. + </TMPL_IF> + + <TMPL_IF NAME="__INNER__"> + This outputs on passes that are neither first nor last. + </TMPL_IF> + + <TMPL_IF NAME="__LAST__"> + This only outputs on the last pass. + <TMPL_IF> + </TMPL_LOOP> + +One use of this feature is to provide a "separator" similar in effect +to the perl function join(). Example: + + <TMPL_LOOP FRUIT> + <TMPL_IF __LAST__> and </TMPL_IF> + <TMPL_VAR KIND><TMPL_UNLESS __LAST__>, <TMPL_ELSE>.</TMPL_UNLESS> + </TMPL_LOOP> + +Would output (in a browser) something like: + + Apples, Oranges, Brains, Toes, and Kiwi. + +Given an appropriate param() call, of course. NOTE: A loop with only +a single pass will get both __FIRST__ and __LAST__ set to true, but +not __INNER__. + +=item * + +no_includes - set this option to 1 to disallow the <TMPL_INCLUDE> tag +in the template file. This can be used to make opening untrusted +templates B<slightly> less dangerous. Defaults to 0. + +=item * + +max_includes - set this variable to determine the maximum depth that +includes can reach. Set to 10 by default. Including files to a depth +greater than this value causes an error message to be displayed. Set +to 0 to disable this protection. + +=item * + +global_vars - normally variables declared outside a loop are not +available inside a loop. This option makes <TMPL_VAR>s like global +variables in Perl - they have unlimited scope. This option also +affects <TMPL_IF> and <TMPL_UNLESS>. + +Example: + + This is a normal variable: <TMPL_VAR NORMAL>.<P> + + <TMPL_LOOP NAME=FROOT_LOOP> + Here it is inside the loop: <TMPL_VAR NORMAL><P> + </TMPL_LOOP> + +Normally this wouldn't work as expected, since <TMPL_VAR NORMAL>'s +value outside the loop is not available inside the loop. + +The global_vars option also allows you to access the values of an +enclosing loop within an inner loop. For example, in this loop the +inner loop will have access to the value of OUTER_VAR in the correct +iteration: + + <TMPL_LOOP OUTER_LOOP> + OUTER: <TMPL_VAR OUTER_VAR> + <TMPL_LOOP INNER_LOOP> + INNER: <TMPL_VAR INNER_VAR> + INSIDE OUT: <TMPL_VAR OUTER_VAR> + </TMPL_LOOP> + </TMPL_LOOP> + +=item * + +filter - this option allows you to specify a filter for your template +files. A filter is a subroutine that will be called after +HTML::Template reads your template file but before it starts parsing +template tags. + +In the most simple usage, you simply assign a code reference to the +filter parameter. This subroutine will recieve a single arguement - a +reference to a string containing the template file text. Here is an +example that accepts templates with tags that look like "!!!ZAP_VAR +FOO!!!" and transforms them into HTML::Template tags: + + my $filter = sub { + my $text_ref = shift; + $$text_ref =~ s/!!!ZAP_(.*?)!!!/<TMPL_$1>/g; + }; + + # open zap.tmpl using the above filter + my $template = HTML::Template->new(filename => 'zap.tmpl', + filter => $filter); + +More complicated usages are possible. You can request that your +filter receieve the template text as an array of lines rather than as +a single scalar. To do that you need to specify your filter using a +hash-ref. In this form you specify the filter using the "sub" key and +the desired argument format using the "format" key. The available +formats are "scalar" and "array". Using the "array" format will incur +a performance penalty but may be more convenient in some situations. + + my $template = HTML::Template->new(filename => 'zap.tmpl', + filter => { sub => $filter, + format => 'array' }); + +You may also have multiple filters. This allows simple filters to be +combined for more elaborate functionality. To do this you specify an +array of filters. The filters are applied in the order they are +specified. + + my $template = HTML::Template->new(filename => 'zap.tmpl', + filter => [ + { sub => \&decompress, + format => 'scalar' }, + { sub => \&remove_spaces, + format => 'array' } + ]); + +The specified filters will be called for any TMPL_INCLUDEed files just +as they are for the main template file. + +=back + +=back 4 + +=cut + + +use integer; # no floating point math so far! +use strict; # and no funny business, either. + +use Carp; # generate better errors with more context +use File::Spec; # generate paths that work on all platforms + +# define accessor constants used to improve readability of array +# accesses into "objects". I used to use 'use constant' but that +# seems to cause occasional irritating warnings in older Perls. +package HTML::Template::LOOP; +sub TEMPLATE_HASH { 0; } +sub PARAM_SET { 1 }; + +package HTML::Template::COND; +sub VARIABLE { 0 }; +sub VARIABLE_TYPE { 1 }; +sub VARIABLE_TYPE_VAR { 0 }; +sub VARIABLE_TYPE_LOOP { 1 }; +sub JUMP_IF_TRUE { 2 }; +sub JUMP_ADDRESS { 3 }; +sub WHICH { 4 }; +sub WHICH_IF { 0 }; +sub WHICH_UNLESS { 1 }; + +# back to the main package scope. +package HTML::Template; + +# open a new template and return an object handle +sub new { + my $pkg = shift; + my $self; { my %hash; $self = bless(\%hash, $pkg); } + + # the options hash + my $options = {}; + $self->{options} = $options; + + # set default parameters in options hash + %$options = ( + debug => 0, + stack_debug => 0, + timing => 0, + search_path_on_include => 0, + cache => 0, + blind_cache => 0, + file_cache => 0, + file_cache_dir => '', + file_cache_dir_mode => 0700, + cache_debug => 0, + shared_cache_debug => 0, + memory_debug => 0, + die_on_bad_params => 1, + vanguard_compatibility_mode => 0, + associate => [], + path => [], + strict => 1, + loop_context_vars => 0, + max_includes => 10, + shared_cache => 0, + double_cache => 0, + double_file_cache => 0, + ipc_key => 'TMPL', + ipc_mode => 0666, + ipc_segment_size => 65536, + ipc_max_size => 0, + global_vars => 0, + no_includes => 0, + case_sensitive => 0, + filter => [], + ); + + # load in options supplied to new() + for (my $x = 0; $x <= $#_; $x += 2) { + defined($_[($x + 1)]) or croak("HTML::Template->new() called with odd number of option parameters - should be of the form option => value"); + $options->{lc($_[$x])} = $_[($x + 1)]; + } + + # blind_cache = 1 implies cache = 1 + $options->{blind_cache} and $options->{cache} = 1; + + # shared_cache = 1 implies cache = 1 + $options->{shared_cache} and $options->{cache} = 1; + + # file_cache = 1 implies cache = 1 + $options->{file_cache} and $options->{cache} = 1; + + # double_cache is a combination of shared_cache and cache. + $options->{double_cache} and $options->{cache} = 1; + $options->{double_cache} and $options->{shared_cache} = 1; + + # double_file_cache is a combination of file_cache and cache. + $options->{double_file_cache} and $options->{cache} = 1; + $options->{double_file_cache} and $options->{file_cache} = 1; + + # vanguard_compatibility_mode implies die_on_bad_params = 0 + $options->{vanguard_compatibility_mode} and + $options->{die_on_bad_params} = 0; + + # handle the "type", "source" parameter format (does anyone use it?) + if (exists($options->{type})) { + exists($options->{source}) or croak("HTML::Template->new() called with 'type' parameter set, but no 'source'!"); + ($options->{type} eq 'filename' or $options->{type} eq 'scalarref' or + $options->{type} eq 'arrayref' or $options->{type} eq 'filehandle') or + croak("HTML::Template->new() : type parameter must be set to 'filename', 'arrayref', 'scalarref' or 'filehandle'!"); + + $options->{$options->{type}} = $options->{source}; + delete $options->{type}; + delete $options->{source}; + } + + # associate should be an array of one element if it's not + # already an array. + if (ref($options->{associate}) ne 'ARRAY') { + $options->{associate} = [ $options->{associate} ]; + } + + # path should be an array if it's not already + if (ref($options->{path}) ne 'ARRAY') { + $options->{path} = [ $options->{path} ]; + } + + # filter should be an array if it's not already + if (ref($options->{filter}) ne 'ARRAY') { + $options->{filter} = [ $options->{filter} ]; + } + + # make sure objects in associate area support param() + foreach my $object (@{$options->{associate}}) { + defined($object->can('param')) or + croak("HTML::Template->new called with associate option, containing object of type " . ref($object) . " which lacks a param() method!"); + } + + # check for syntax errors: + my $source_count = 0; + exists($options->{filename}) and $source_count++; + exists($options->{filehandle}) and $source_count++; + exists($options->{arrayref}) and $source_count++; + exists($options->{scalarref}) and $source_count++; + if ($source_count != 1) { + croak("HTML::Template->new called with multiple (or no) template sources specified! A valid call to new() has exactly one filename => 'file' OR exactly one scalarref => \\\$scalar OR exactly one arrayref => \\\@array OR exactly one filehandle => \*FH"); + } + + # do some memory debugging - this is best started as early as possible + if ($options->{memory_debug}) { + # memory_debug needs GTop + eval { require GTop; }; + croak("Could not load GTop. You must have GTop installed to use HTML::Template in memory_debug mode. The error was: $@") + if ($@); + $self->{gtop} = GTop->new(); + $self->{proc_mem} = $self->{gtop}->proc_mem($$); + print STDERR "\n### HTML::Template Memory Debug ### START ", $self->{proc_mem}->size(), "\n"; + } + + if ($options->{file_cache}) { + # make sure we have a file_cache_dir option + croak("You must specify the file_cache_dir option if you want to use file_cache.") + unless defined $options->{file_cache_dir} and + length $options->{file_cache_dir}; + + # file_cache needs some extra modules loaded + eval { require Storable; }; + croak("Could not load Storable. You must have Storable installed to use HTML::Template in file_cache mode. The error was: $@") + if ($@); + eval { require Digest::MD5; }; + croak("Could not load Digest::MD5. You must have Digest::MD5 installed to use HTML::Template in file_cache mode. The error was: $@") + if ($@); + } + + if ($options->{shared_cache}) { + # shared_cache needs some extra modules loaded + eval { require IPC::SharedCache; }; + croak("Could not load IPC::SharedCache. You must have IPC::SharedCache installed to use HTML::Template in shared_cache mode. The error was: $@") + if ($@); + + # initialize the shared cache + my %cache; + tie %cache, 'IPC::SharedCache', + ipc_key => $options->{ipc_key}, + load_callback => [\&_load_shared_cache, $self], + validate_callback => [\&_validate_shared_cache, $self], + debug => $options->{shared_cache_debug}, + ipc_mode => $options->{ipc_mode}, + max_size => $options->{ipc_max_size}, + ipc_segment_size => $options->{ipc_segment_size}; + $self->{cache} = \%cache; + } + + print STDERR "### HTML::Template Memory Debug ### POST CACHE INIT ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + # initialize data structures + $self->_init; + + print STDERR "### HTML::Template Memory Debug ### POST _INIT CALL ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + # drop the shared cache - leaving out this step results in the + # template object evading garbage collection since the callbacks in + # the shared cache tie hold references to $self! This was not easy + # to find, by the way. + delete $self->{cache} if $options->{shared_cache}; + + return $self; +} + +# an internally used new that receives its parse_stack and param_map as input +sub _new_from_loop { + my $pkg = shift; + my $self; { my %hash; $self = bless(\%hash, $pkg); } + + # the options hash + my $options = {}; + $self->{options} = $options; + + # set default parameters in options hash - a subset of the options + # valid in a normal new(). Since _new_from_loop never calls _init, + # many options have no relevance. + %$options = ( + debug => 0, + stack_debug => 0, + die_on_bad_params => 1, + associate => [], + loop_context_vars => 0, + ); + + # load in options supplied to new() + for (my $x = 0; $x <= $#_; $x += 2) { + defined($_[($x + 1)]) or croak("HTML::Template->new() called with odd number of option parameters - should be of the form option => value"); + $options->{lc($_[$x])} = $_[($x + 1)]; + } + + $self->{param_map} = $options->{param_map}; + $self->{parse_stack} = $options->{parse_stack}; + delete($options->{param_map}); + delete($options->{parse_stack}); + + return $self; +} + +# a few shortcuts to new(), of possible use... +sub new_file { + my $pkg = shift; return $pkg->new('filename', @_); +} +sub new_filehandle { + my $pkg = shift; return $pkg->new('filehandle', @_); +} +sub new_array_ref { + my $pkg = shift; return $pkg->new('arrayref', @_); +} +sub new_scalar_ref { + my $pkg = shift; return $pkg->new('scalarref', @_); +} + +# initializes all the object data structures, either from cache or by +# calling the appropriate routines. +sub _init { + my $self = shift; + my $options = $self->{options}; + + if ($options->{double_cache}) { + # try the normal cache, return if we have it. + $self->_fetch_from_cache(); + return if (defined $self->{param_map} and defined $self->{parse_stack}); + + # try the shared cache + $self->_fetch_from_shared_cache(); + + # put it in the local cache if we got it. + $self->_commit_to_cache() + if (defined $self->{param_map} and defined $self->{parse_stack}); + } elsif ($options->{double_file_cache}) { + # try the normal cache, return if we have it. + $self->_fetch_from_cache(); + return if (defined $self->{param_map} and defined $self->{parse_stack}); + + # try the file cache + $self->_fetch_from_file_cache(); + + # put it in the local cache if we got it. + $self->_commit_to_cache() + if (defined $self->{param_map} and defined $self->{parse_stack}); + } elsif ($options->{shared_cache}) { + # try the shared cache + $self->_fetch_from_shared_cache(); + } elsif ($options->{file_cache}) { + # try the file cache + $self->_fetch_from_file_cache(); + } elsif ($options->{cache}) { + # try the normal cache + $self->_fetch_from_cache(); + } + + # if we got a cache hit, return + return if (defined $self->{param_map} and defined $self->{parse_stack}); + + # if we're here, then we didn't get a cached copy, so do a full + # init. + $self->_init_template(); + $self->_parse(); + + # now that we have a full init, cache the structures if cacheing is + # on. shared cache is already cool. + if($options->{file_cache}){ + $self->_commit_to_file_cache(); + } + $self->_commit_to_cache() if (($options->{cache} + and not $options->{shared_cache} + and not $options->{file_cache}) or + ($options->{double_cache}) or + ($options->{double_file_cache})); +} + +# Caching subroutines - they handle getting and validating cache +# records from either the in-memory or shared caches. + +# handles the normal in memory cache +use vars qw( %CACHE ); +sub _fetch_from_cache { + my $self = shift; + my $options = $self->{options}; + + # return if there's no cache entry for this filename + return unless exists($options->{filename}); + my $filepath = $self->_find_file($options->{filename}); + return unless (defined($filepath) and + exists $CACHE{$filepath}); + + $options->{filepath} = $filepath; + + # validate the cache + my $mtime = $self->_mtime($filepath); + if (defined $mtime) { + # return if the mtime doesn't match the cache + if (defined($CACHE{$filepath}{mtime}) and + ($mtime != $CACHE{$filepath}{mtime})) { + $options->{cache_debug} and + print STDERR "CACHE MISS : $filepath : $mtime\n"; + return; + } + + # if the template has includes, check each included file's mtime + # and return if different + if (exists($CACHE{$filepath}{included_mtimes})) { + foreach my $filename (keys %{$CACHE{$filepath}{included_mtimes}}) { + next unless + defined($CACHE{$filepath}{included_mtimes}{$filename}); + + my $included_mtime = (stat($filename))[9]; + if ($included_mtime != $CACHE{$filepath}{included_mtimes}{$filename}) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### CACHE MISS : $filepath : INCLUDE $filename : $included_mtime\n"; + + return; + } + } + } + } + + # got a cache hit! + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### CACHE HIT : $filepath\n"; + + $self->{param_map} = $CACHE{$filepath}{param_map}; + $self->{parse_stack} = $CACHE{$filepath}{parse_stack}; + exists($CACHE{$filepath}{included_mtimes}) and + $self->{included_mtimes} = $CACHE{$filepath}{included_mtimes}; + + # clear out values from param_map from last run + $self->_normalize_options(); + $self->clear_params(); +} + +sub _commit_to_cache { + my $self = shift; + my $options = $self->{options}; + + my $filepath = $options->{filepath}; + if (not defined $filepath) { + $filepath = $self->_find_file($options->{filename}); + confess("HTML::Template->new() : Cannot open included file $options->{filename} : file not found.") + unless defined($filepath); + $options->{filepath} = $filepath; + } + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### CACHE LOAD : $filepath\n"; + + $options->{blind_cache} or + $CACHE{$filepath}{mtime} = $self->_mtime($filepath); + $CACHE{$filepath}{param_map} = $self->{param_map}; + $CACHE{$filepath}{parse_stack} = $self->{parse_stack}; + exists($self->{included_mtimes}) and + $CACHE{$filepath}{included_mtimes} = $self->{included_mtimes}; +} + +# generates MD5 from filepath to determine filename for cache file +sub _get_cache_filename { + my ($self, $filepath) = @_; + + # hash the filename ... + my $hash = Digest::MD5::md5_hex($filepath); + + # ... and build a path out of it. Using the first two charcters + # gives us 255 buckets. This means you can have 255,000 templates + # in the cache before any one directory gets over a few thousand + # files in it. That's probably pretty good for this planet. If not + # then it should be configurable. + if (wantarray) { + return (substr($hash,0,2), substr($hash,2)) + } else { + return File::Spec->join($self->{options}{file_cache_dir}, + substr($hash,0,2), substr($hash,2)); + } +} + +# handles the file cache +sub _fetch_from_file_cache { + my $self = shift; + my $options = $self->{options}; + return unless exists($options->{filename}); + + # return if there's no cache entry for this filename + my $filepath = $self->_find_file($options->{filename}); + return unless defined $filepath; + my $cache_filename = $self->_get_cache_filename($filepath); + return unless -e $cache_filename; + + eval { + $self->{record} = Storable::lock_retrieve($cache_filename); + }; + croak("HTML::Template::new() - Problem reading cache file $cache_filename (file_cache => 1) : $@") + if $@; + croak("HTML::Template::new() - Problem reading cache file $cache_filename (file_cache => 1) : $!") + unless defined $self->{record}; + + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = @{$self->{record}}; + + $options->{filepath} = $filepath; + + # validate the cache + my $mtime = $self->_mtime($filepath); + if (defined $mtime) { + # return if the mtime doesn't match the cache + if (defined($self->{mtime}) and + ($mtime != $self->{mtime})) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### FILE CACHE MISS : $filepath : $mtime\n"; + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = (undef, undef, undef, undef); + return; + } + + # if the template has includes, check each included file's mtime + # and return if different + if (exists($self->{included_mtimes})) { + foreach my $filename (keys %{$self->{included_mtimes}}) { + next unless + defined($self->{included_mtimes}{$filename}); + + my $included_mtime = (stat($filename))[9]; + if ($included_mtime != $self->{included_mtimes}{$filename}) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### FILE CACHE MISS : $filepath : INCLUDE $filename : $included_mtime\n"; + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = (undef, undef, undef, undef); + return; + } + } + } + } + + # got a cache hit! + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### FILE CACHE HIT : $filepath\n"; + + # clear out values from param_map from last run + $self->_normalize_options(); + $self->clear_params(); +} + +sub _commit_to_file_cache { + my $self = shift; + my $options = $self->{options}; + + my $filepath = $options->{filepath}; + if (not defined $filepath) { + $filepath = $self->_find_file($options->{filename}); + confess("HTML::Template->new() : Cannot open included file $options->{filename} : file not found.") + unless defined($filepath); + $options->{filepath} = $filepath; + } + + my ($cache_dir, $cache_file) = $self->_get_cache_filename($filepath); + $cache_dir = File::Spec->join($options->{file_cache_dir}, $cache_dir); + if (not -d $cache_dir) { + if (not -d $options->{file_cache_dir}) { + mkdir($options->{file_cache_dir},$options->{file_cache_dir_mode}) + or croak("HTML::Template->new() : can't mkdir $options->{file_cache_dir} (file_cache => 1): $!"); + } + mkdir($cache_dir,$options->{file_cache_dir_mode}) + or croak("HTML::Template->new() : can't mkdir $cache_dir (file_cache => 1): $!"); + } + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### FILE CACHE LOAD : $options->{filepath}\n"; + + my $result; + eval { + $result = Storable::lock_store([ $self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack} ], + scalar File::Spec->join($cache_dir, $cache_file) + ); + }; + croak("HTML::Template::new() - Problem writing cache file $cache_dir/$cache_file (file_cache => 1) : $@") + if $@; + croak("HTML::Template::new() - Problem writing cache file $cache_dir/$cache_file (file_cache => 1) : $!") + unless defined $result; +} + +# Shared cache routines. +sub _fetch_from_shared_cache { + my $self = shift; + my $options = $self->{options}; + + my $filepath = $self->_find_file($options->{filename}); + return unless defined $filepath; + + # fetch from the shared cache. + $self->{record} = $self->{cache}{$filepath}; + + ($self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack}) = @{$self->{record}} + if defined($self->{record}); + + $options->{cache_debug} and defined($self->{record}) and print STDERR "### HTML::Template Cache Debug ### CACHE HIT : $filepath\n"; + # clear out values from param_map from last run + $self->_normalize_options(), $self->clear_params() + if (defined($self->{record})); + delete($self->{record}); + + return $self; +} + +sub _validate_shared_cache { + my ($self, $filename, $record) = @_; + my $options = $self->{options}; + + $options->{shared_cache_debug} and print STDERR "### HTML::Template Cache Debug ### SHARED CACHE VALIDATE : $filename\n"; + + return 1 if $options->{blind_cache}; + + my ($c_mtime, $included_mtimes, $param_map, $parse_stack) = @$record; + + # if the modification time has changed return false + my $mtime = $self->_mtime($filename); + if (defined $mtime and defined $c_mtime + and $mtime != $c_mtime) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### SHARED CACHE MISS : $filename : $mtime\n"; + return 0; + } + + # if the template has includes, check each included file's mtime + # and return false if different + if (defined $mtime and defined $included_mtimes) { + foreach my $fname (keys %$included_mtimes) { + next unless defined($included_mtimes->{$fname}); + if ($included_mtimes->{$fname} != (stat($fname))[9]) { + $options->{cache_debug} and + print STDERR "### HTML::Template Cache Debug ### SHARED CACHE MISS : $filename : INCLUDE $fname\n"; + return 0; + } + } + } + + # all done - return true + return 1; +} + +sub _load_shared_cache { + my ($self, $filename) = @_; + my $options = $self->{options}; + my $cache = $self->{cache}; + + $self->_init_template(); + $self->_parse(); + + $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ### SHARED CACHE LOAD : $options->{filepath}\n"; + + print STDERR "### HTML::Template Memory Debug ### END CACHE LOAD ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + return [ $self->{mtime}, + $self->{included_mtimes}, + $self->{param_map}, + $self->{parse_stack} ]; +} + +# utility function - given a filename performs documented search and +# returns a full path of undef if the file cannot be found. +sub _find_file { + my ($self, $filename, $extra_path) = @_; + my $options = $self->{options}; + my $filepath; + + # first check for a full path + return File::Spec->canonpath($filename) + if (File::Spec->file_name_is_absolute($filename) and (-e $filename)); + + # try the extra_path if one was specified + if (defined($extra_path)) { + $extra_path->[$#{$extra_path}] = $filename; + $filepath = File::Spec->canonpath(File::Spec->catfile(@$extra_path)); + return File::Spec->canonpath($filepath) if -e $filepath; + } + + # try pre-prending HTML_Template_Root + if (exists($ENV{HTML_TEMPLATE_ROOT})) { + $filepath = File::Spec->catfile($ENV{HTML_TEMPLATE_ROOT}, $filename); + return File::Spec->canonpath($filepath) if -e $filepath; + } + + # try "path" option list.. + foreach my $path (@{$options->{path}}) { + $filepath = File::Spec->canonpath(File::Spec->catfile($path, $filename)); + return File::Spec->canonpath($filepath) if -e $filepath; + } + + # try even a relative path from the current directory... + return File::Spec->canonpath($filename) if -e $filename; + + return undef; +} + +# utility function - computes the mtime for $filename +sub _mtime { + my ($self, $filepath) = @_; + my $options = $self->{options}; + + return(undef) if ($options->{blind_cache}); + + # make sure it still exists in the filesystem + (-r $filepath) or Carp::confess("HTML::Template : template file $filepath does not exist or is unreadable."); + + # get the modification time + return (stat(_))[9]; +} + +# utility function - enforces new() options across LOOPs that have +# come from a cache. Otherwise they would have stale options hashes. +sub _normalize_options { + my $self = shift; + my $options = $self->{options}; + + my @pstacks = ($self->{parse_stack}); + while(@pstacks) { + my $pstack = pop(@pstacks); + foreach my $item (@$pstack) { + next unless (ref($item) eq 'HTML::Template::LOOP'); + foreach my $template (values %{$item->[HTML::Template::LOOP::TEMPLATE_HASH]}) { + # must be the same list as the call to _new_from_loop... + $template->{options}{debug} = $options->{debug}; + $template->{options}{stack_debug} = $options->{stack_debug}; + $template->{options}{die_on_bad_params} = $options->{die_on_bad_params}; + $template->{options}{case_sensitive} = $options->{case_sensitive}; + + push(@pstacks, $template->{parse_stack}); + } + } + } +} + +# initialize the template buffer +sub _init_template { + my $self = shift; + my $options = $self->{options}; + + print STDERR "### HTML::Template Memory Debug ### START INIT_TEMPLATE ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + if (exists($options->{filename})) { + my $filepath = $options->{filepath}; + if (not defined $filepath) { + $filepath = $self->_find_file($options->{filename}); + confess("HTML::Template->new() : Cannot open included file $options->{filename} : file not found.") + unless defined($filepath); + # we'll need this for future reference - to call stat() for example. + $options->{filepath} = $filepath; + } + + confess("HTML::Template->new() : Cannot open included file $options->{filename} : $!") + unless defined(open(TEMPLATE, $filepath)); + $self->{mtime} = $self->_mtime($filepath); + + # read into scalar, note the mtime for the record + $self->{template} = ""; + while (read(TEMPLATE, $self->{template}, 10240, length($self->{template}))) {} + close(TEMPLATE); + + } elsif (exists($options->{scalarref})) { + # copy in the template text + $self->{template} = ${$options->{scalarref}}; + + delete($options->{scalarref}); + } elsif (exists($options->{arrayref})) { + # if we have an array ref, join and store the template text + $self->{template} = join("", @{$options->{arrayref}}); + + delete($options->{arrayref}); + } elsif (exists($options->{filehandle})) { + # just read everything in in one go + local $/ = undef; + $self->{template} = readline($options->{filehandle}); + + delete($options->{filehandle}); + } else { + confess("HTML::Template : Need to call new with filename, filehandle, scalarref or arrayref parameter specified."); + } + + print STDERR "### HTML::Template Memory Debug ### END INIT_TEMPLATE ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + # handle filters if necessary + $self->_call_filters(\$self->{template}) if @{$options->{filter}}; + + return $self; +} + +# handle calling user defined filters +sub _call_filters { + my $self = shift; + my $template_ref = shift; + my $options = $self->{options}; + + my ($format, $sub); + foreach my $filter (@{$options->{filter}}) { + croak("HTML::Template->new() : bad value set for filter parameter - must be a code ref or a hash ref.") + unless ref $filter; + + # translate into CODE->HASH + $filter = { 'format' => 'scalar', 'sub' => $filter } + if (ref $filter eq 'CODE'); + + if (ref $filter eq 'HASH') { + $format = $filter->{'format'}; + $sub = $filter->{'sub'}; + + # check types and values + croak("HTML::Template->new() : bad value set for filter parameter - hash must contain \"format\" key and \"sub\" key.") + unless defined $format and defined $sub; + croak("HTML::Template->new() : bad value set for filter parameter - \"format\" must be either 'array' or 'scalar'") + unless $format eq 'array' or $format eq 'scalar'; + croak("HTML::Template->new() : bad value set for filter parameter - \"sub\" must be a code ref") + unless ref $sub and ref $sub eq 'CODE'; + + # catch errors + eval { + if ($format eq 'scalar') { + # call + $sub->($template_ref); + } else { + # modulate + my @array = map { $_."\n" } split("\n", $$template_ref); + # call + $sub->(\@array); + # demodulate + $$template_ref = join("", @array); + } + }; + croak("HTML::Template->new() : fatal error occured during filter call: $@") if $@; + } else { + croak("HTML::Template->new() : bad value set for filter parameter - must be code ref or hash ref"); + } + } + # all done + return $template_ref; +} + +# _parse sifts through a template building up the param_map and +# parse_stack structures. +# +# The end result is a Template object that is fully ready for +# output(). +sub _parse { + my $self = shift; + my $options = $self->{options}; + + $options->{debug} and print STDERR "### HTML::Template Debug ### In _parse:\n"; + + # setup the stacks and maps - they're accessed by typeglobs that + # reference the top of the stack. They are masked so that a loop + # can transparently have its own versions. + use vars qw(@pstack %pmap @ifstack @ucstack %top_pmap); + local (*pstack, *ifstack, *pmap, *ucstack, *top_pmap); + + # the pstack is the array of scalar refs (plain text from the + # template file), VARs, LOOPs, IFs and ELSEs that output() works on + # to produce output. Looking at output() should make it clear what + # _parse is trying to accomplish. + my @pstacks = ([]); + *pstack = $pstacks[0]; + $self->{parse_stack} = $pstacks[0]; + + # the pmap binds names to VARs, LOOPs and IFs. It allows param() to + # access the right variable. NOTE: output() does not look at the + # pmap at all! + my @pmaps = ({}); + *pmap = $pmaps[0]; + *top_pmap = $pmaps[0]; + $self->{param_map} = $pmaps[0]; + + # the ifstack is a temporary stack containing pending ifs and elses + # waiting for a /if. + my @ifstacks = ([]); + *ifstack = $ifstacks[0]; + + # the ucstack is a temporary stack containing conditions that need + # to be bound to param_map entries when their block is finished. + # This happens when a conditional is encountered before any other + # reference to its NAME. Since a conditional can reference VARs and + # LOOPs it isn't possible to make the link right away. + my @ucstacks = ([]); + *ucstack = $ucstacks[0]; + + # the loopstack is another temp stack for closing loops. unlike + # those above it doesn't get scoped inside loops, therefore it + # doesn't need the typeglob magic. + my @loopstack = (); + + # the fstack is a stack of filenames and counters that keeps track + # of which file we're in and where we are in it. This allows + # accurate error messages even inside included files! + # fcounter, fmax and fname are aliases for the current file's info + use vars qw($fcounter $fname $fmax); + local (*fcounter, *fname, *fmax); + + my @fstack = ([$options->{filepath} || "/fake/path/for/non/file/template", + 1, + scalar @{[$self->{template} =~ m/(\n)/g]} + 1 + ]); + (*fname, *fcounter, *fmax) = \ ( @{$fstack[0]} ); + + my $NOOP = HTML::Template::NOOP->new(); + my $ESCAPE = HTML::Template::ESCAPE->new(); + my $URLESCAPE = HTML::Template::URLESCAPE->new(); + + # all the tags that need NAMEs: + my %need_names = map { $_ => 1 } + qw(TMPL_VAR TMPL_LOOP TMPL_IF TMPL_UNLESS TMPL_INCLUDE); + + # variables used below that don't need to be my'd in the loop + my ($name, $which, $escape); + + # handle the old vanguard format + $options->{vanguard_compatibility_mode} and + $self->{template} =~ s/%([-\w\/\.+]+)%/<TMPL_VAR NAME=$1>/g; + + # now split up template on '<', leaving them in + my @chunks = split(m/(?=<)/, $self->{template}); + + # all done with template + delete $self->{template}; + + # loop through chunks, filling up pstack + my $last_chunk = $#chunks; + CHUNK: for (my $chunk_number = 0; + $chunk_number <= $last_chunk; + $chunk_number++) { + next unless defined $chunks[$chunk_number]; + my $chunk = $chunks[$chunk_number]; + + # a general regex to match any and all TMPL_* tags + if ($chunk =~ /^< + (?:!--\s*)? + ( + \/?[Tt][Mm][Pp][Ll]_ + (?: + (?:[Vv][Aa][Rr]) + | + (?:[Ll][Oo][Oo][Pp]) + | + (?:[Ii][Ff]) + | + (?:[Ee][Ll][Ss][Ee]) + | + (?:[Uu][Nn][Ll][Ee][Ss][Ss]) + | + (?:[Ii][Nn][Cc][Ll][Uu][Dd][Ee]) + ) + ) # $1 => $which - start of the tag + + \s* + + # ESCAPE attribute + (?: + [Ee][Ss][Cc][Aa][Pp][Ee] + \s*=\s* + (?: + ( 0 | (?:"0") | (?:'0') ) # $2 => ESCAPE off + | + ( 1 | (?:"1") | (?:'1') | + (?:[Hh][Tt][Mm][Ll]) | + (?:"[Hh][Tt][Mm][Ll]") | + (?:'[Hh][Tt][Mm][Ll]') | + (?:[Uu][Rr][Ll]) | + (?:"[Uu][Rr][Ll]") | + (?:'[Uu][Rr][Ll]') | + ) # $3 => ESCAPE on + ) + )* # allow multiple ESCAPEs + + \s* + + # NAME attribute + (?: + (?: + [Nn][Aa][Mm][Ee] + \s*=\s* + )? + (?: + "([^">]*)" # $4 => double-quoted NAME value " + | + '([^'>]*)' # $5 => single-quoted NAME value + | + ([^\s=>]*) # $6 => unquoted NAME value + ) + )? + + \s* + + # ESCAPE attribute + (?: + [Ee][Ss][Cc][Aa][Pp][Ee] + \s*=\s* + (?: + ( 0 | (?:"0") | (?:'0') ) # $7 => ESCAPE off + | + ( 1 | (?:"1") | (?:'1') | + (?:[Hh][Tt][Mm][Ll]) | + (?:"[Hh][Tt][Mm][Ll]") | + (?:'[Hh][Tt][Mm][Ll]') | + (?:[Uu][Rr][Ll]) | + (?:"[Uu][Rr][Ll]") | + (?:'[Uu][Rr][Ll]') | + ) # $8 => ESCAPE on + ) + )* # allow multiple ESCAPEs + + \s* + + (?:--)?> + (.*) # $9 => $post - text that comes after the tag + $/sx) { + + $which = uc($1); # which tag is it + + $escape = $3 || $8; + $escape = 0 if $2 || $7; # ESCAPE=0 + $escape = 0 unless defined($escape); + + # what name for the tag? undef for a /tag at most, one of the + # following three will be defined + undef $name; + $name = $4 if defined($4); + $name = $5 if defined($5); + $name = $6 if defined($6); + + # allow mixed case in filenames, otherwise flatten + $name = lc($name) unless ($which eq 'TMPL_INCLUDE' or $options->{case_sensitive}); + + my $post = $9; # what comes after on the line + + # die if we need a name and didn't get one + die "HTML::Template->new() : No NAME given to a $which tag at $fname : line $fcounter." if (!defined($name) and $need_names{$which}); + + # die if we got an escape but can't use one + die "HTML::Template->new() : ESCAPE option invalid in a $which tag at $fname : line $fcounter." if ( $escape and ($which ne 'TMPL_VAR')); + + # take actions depending on which tag found + if ($which eq 'TMPL_VAR') { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : parsed VAR $name\n"; + + # if we already have this var, then simply link to the existing + # HTML::Template::VAR, else create a new one. + my $var; + if (exists $pmap{$name}) { + $var = $pmap{$name}; + (ref($var) eq 'HTML::Template::VAR') or + die "HTML::Template->new() : Already used param name $name as a TMPL_LOOP, found in a TMPL_VAR at $fname : line $fcounter."; + } else { + $var = HTML::Template::VAR->new(); + $pmap{$name} = $var; + $top_pmap{$name} = HTML::Template::VAR->new() + if $options->{global_vars} and not exists $top_pmap{$name}; + } + + # if ESCAPE was set, push an ESCAPE op on the stack before + # the variable. output will handle the actual work. + if ($escape) { + if ($escape =~ /^"?[Uu][Rr][Ll]"?$/) { + push(@pstack, $URLESCAPE); + } else { + push(@pstack, $ESCAPE); + } + } + + push(@pstack, $var); + + } elsif ($which eq 'TMPL_LOOP') { + # we've got a loop start + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : LOOP $name start\n"; + + # if we already have this loop, then simply link to the existing + # HTML::Template::LOOP, else create a new one. + my $loop; + if (exists $pmap{$name}) { + $loop = $pmap{$name}; + (ref($loop) eq 'HTML::Template::LOOP') or + die "HTML::Template->new() : Already used param name $name as a TMPL_VAR, TMPL_IF or TMPL_UNLESS, found in a TMP_LOOP at $fname : line $fcounter!"; + + } else { + # store the results in a LOOP object - actually just a + # thin wrapper around another HTML::Template object. + $loop = HTML::Template::LOOP->new(); + $pmap{$name} = $loop; + } + + # get it on the loopstack, pstack of the enclosing block + push(@pstack, $loop); + push(@loopstack, [$loop, $#pstack]); + + # magic time - push on a fresh pmap and pstack, adjust the typeglobs. + # this gives the loop a separate namespace (i.e. pmap and pstack). + push(@pstacks, []); + *pstack = $pstacks[$#pstacks]; + push(@pmaps, {}); + *pmap = $pmaps[$#pmaps]; + push(@ifstacks, []); + *ifstack = $ifstacks[$#ifstacks]; + push(@ucstacks, []); + *ucstack = $ucstacks[$#ucstacks]; + + # auto-vivify __FIRST__, __LAST__ and __INNER__ if + # loop_context_vars is set. Otherwise, with + # die_on_bad_params set output() will might cause errors + # when it tries to set them. + if ($options->{loop_context_vars}) { + $pmap{__first__} = HTML::Template::VAR->new(); + $pmap{__inner__} = HTML::Template::VAR->new(); + $pmap{__last__} = HTML::Template::VAR->new(); + $pmap{__odd__} = HTML::Template::VAR->new(); + } + + } elsif ($which eq '/TMPL_LOOP') { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : LOOP end\n"; + + my $loopdata = pop(@loopstack); + die "HTML::Template->new() : found </TMPL_LOOP> with no matching <TMPL_LOOP> at $fname : line $fcounter!" unless defined $loopdata; + + my ($loop, $starts_at) = @$loopdata; + + # resolve pending conditionals + foreach my $uc (@ucstack) { + my $var = $uc->[HTML::Template::COND::VARIABLE]; + if (exists($pmap{$var})) { + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } else { + $pmap{$var} = HTML::Template::VAR->new(); + $top_pmap{$var} = HTML::Template::VAR->new() + if $options->{global_vars} and not exists $top_pmap{$var}; + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } + if (ref($pmap{$var}) eq 'HTML::Template::VAR') { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_VAR; + } else { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_LOOP; + } + } + + # get pmap and pstack for the loop, adjust the typeglobs to + # the enclosing block. + my $param_map = pop(@pmaps); + *pmap = $pmaps[$#pmaps]; + my $parse_stack = pop(@pstacks); + *pstack = $pstacks[$#pstacks]; + + scalar(@ifstack) and die "HTML::Template->new() : Dangling <TMPL_IF> or <TMPL_UNLESS> in loop ending at $fname : line $fcounter."; + pop(@ifstacks); + *ifstack = $ifstacks[$#ifstacks]; + pop(@ucstacks); + *ucstack = $ucstacks[$#ucstacks]; + + # instantiate the sub-Template, feeding it parse_stack and + # param_map. This means that only the enclosing template + # does _parse() - sub-templates get their parse_stack and + # param_map fed to them already filled in. + $loop->[HTML::Template::LOOP::TEMPLATE_HASH]{$starts_at} + = HTML::Template->_new_from_loop( + parse_stack => $parse_stack, + param_map => $param_map, + debug => $options->{debug}, + die_on_bad_params => $options->{die_on_bad_params}, + loop_context_vars => $options->{loop_context_vars}, + case_sensitive => $options->{case_sensitive}, + ); + + } elsif ($which eq 'TMPL_IF' or $which eq 'TMPL_UNLESS' ) { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : $which $name start\n"; + + # if we already have this var, then simply link to the existing + # HTML::Template::VAR/LOOP, else defer the mapping + my $var; + if (exists $pmap{$name}) { + $var = $pmap{$name}; + } else { + $var = $name; + } + + # connect the var to a conditional + my $cond = HTML::Template::COND->new($var); + if ($which eq 'TMPL_IF') { + $cond->[HTML::Template::COND::WHICH] = HTML::Template::COND::WHICH_IF; + $cond->[HTML::Template::COND::JUMP_IF_TRUE] = 0; + } else { + $cond->[HTML::Template::COND::WHICH] = HTML::Template::COND::WHICH_UNLESS; + $cond->[HTML::Template::COND::JUMP_IF_TRUE] = 1; + } + + # push unconnected conditionals onto the ucstack for + # resolution later. Otherwise, save type information now. + if ($var eq $name) { + push(@ucstack, $cond); + } else { + if (ref($var) eq 'HTML::Template::VAR') { + $cond->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_VAR; + } else { + $cond->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_LOOP; + } + } + + # push what we've got onto the stacks + push(@pstack, $cond); + push(@ifstack, $cond); + + } elsif ($which eq '/TMPL_IF' or $which eq '/TMPL_UNLESS') { + $options->{debug} and print STDERR "### HTML::Template Debug ###$fname : line $fcounter : $which end\n"; + + my $cond = pop(@ifstack); + die "HTML::Template->new() : found </${which}> with no matching <TMPL_IF> at $fname : line $fcounter." unless defined $cond; + if ($which eq '/TMPL_IF') { + die "HTML::Template->new() : found </TMPL_IF> incorrectly terminating a <TMPL_UNLESS> (use </TMPL_UNLESS>) at $fname : line $fcounter.\n" + if ($cond->[HTML::Template::COND::WHICH] == HTML::Template::COND::WHICH_UNLESS); + } else { + die "HTML::Template->new() : found </TMPL_UNLESS> incorrectly terminating a <TMPL_IF> (use </TMPL_IF>) at $fname : line $fcounter.\n" + if ($cond->[HTML::Template::COND::WHICH] == HTML::Template::COND::WHICH_IF); + } + + # connect the matching to this "address" - place a NOOP to + # hold the spot. This allows output() to treat an IF in the + # assembler-esque "Conditional Jump" mode. + push(@pstack, $NOOP); + $cond->[HTML::Template::COND::JUMP_ADDRESS] = $#pstack; + + } elsif ($which eq 'TMPL_ELSE') { + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : ELSE\n"; + + my $cond = pop(@ifstack); + die "HTML::Template->new() : found <TMPL_ELSE> with no matching <TMPL_IF> or <TMPL_UNLESS> at $fname : line $fcounter." unless defined $cond; + + + my $else = HTML::Template::COND->new($cond->[HTML::Template::COND::VARIABLE]); + $else->[HTML::Template::COND::WHICH] = $cond->[HTML::Template::COND::WHICH]; + $else->[HTML::Template::COND::JUMP_IF_TRUE] = not $cond->[HTML::Template::COND::JUMP_IF_TRUE]; + + # need end-block resolution? + if (defined($cond->[HTML::Template::COND::VARIABLE_TYPE])) { + $else->[HTML::Template::COND::VARIABLE_TYPE] = $cond->[HTML::Template::COND::VARIABLE_TYPE]; + } else { + push(@ucstack, $else); + } + + push(@pstack, $else); + push(@ifstack, $else); + + # connect the matching to this "address" - thus the if, + # failing jumps to the ELSE address. The else then gets + # elaborated, and of course succeeds. On the other hand, if + # the IF fails and falls though, output will reach the else + # and jump to the /if address. + $cond->[HTML::Template::COND::JUMP_ADDRESS] = $#pstack; + + } elsif ($which eq 'TMPL_INCLUDE') { + # handle TMPL_INCLUDEs + $options->{debug} and print STDERR "### HTML::Template Debug ### $fname : line $fcounter : INCLUDE $name \n"; + + # no includes here, bub + $options->{no_includes} and croak("HTML::Template : Illegal attempt to use TMPL_INCLUDE in template file : (no_includes => 1)"); + + my $filename = $name; + + # look for the included file... + my $filepath; + if ($options->{search_path_on_include}) { + $filepath = $self->_find_file($filename); + } else { + $filepath = $self->_find_file($filename, + [File::Spec->splitdir($fstack[-1][0])] + ); + } + die "HTML::Template->new() : Cannot open included file $filename : file not found." + unless defined($filepath); + die "HTML::Template->new() : Cannot open included file $filename : $!" + unless defined(open(TEMPLATE, $filepath)); + + # read into the array + my $included_template = ""; + while(read(TEMPLATE, $included_template, 10240, length($included_template))) {} + close(TEMPLATE); + + # call filters if necessary + $self->_call_filters(\$included_template) if @{$options->{filter}}; + + if ($included_template) { # not empty + # handle the old vanguard format - this needs to happen here + # since we're not about to do a next CHUNKS. + $options->{vanguard_compatibility_mode} and + $included_template =~ s/%([-\w\/\.+]+)%/<TMPL_VAR NAME=$1>/g; + + # collect mtimes for included files + if ($options->{cache} and !$options->{blind_cache}) { + $self->{included_mtimes}{$filepath} = (stat($filepath))[9]; + } + + # adjust the fstack to point to the included file info + push(@fstack, [$filepath, 1, + scalar @{[$included_template =~ m/(\n)/g]} + 1]); + (*fname, *fcounter, *fmax) = \ ( @{$fstack[$#fstack]} ); + + # make sure we aren't infinitely recursing + die "HTML::Template->new() : likely recursive includes - parsed $options->{max_includes} files deep and giving up (set max_includes higher to allow deeper recursion)." if ($options->{max_includes} and (scalar(@fstack) > $options->{max_includes})); + + # stick the remains of this chunk onto the bottom of the + # included text. + $included_template .= $post; + $post = undef; + + # move the new chunks into place. + splice(@chunks, $chunk_number, 1, + split(m/(?=<)/, $included_template)); + + # recalculate stopping point + $last_chunk = $#chunks; + + # start in on the first line of the included text - nothing + # else to do on this line. + $chunk = $chunks[$chunk_number]; + + redo CHUNK; + } + } else { + # zuh!? + die "HTML::Template->new() : Unknown or unmatched TMPL construct at $fname : line $fcounter."; + } + # push the rest after the tag + if (defined($post)) { + if (ref($pstack[$#pstack]) eq 'SCALAR') { + ${$pstack[$#pstack]} .= $post; + } else { + push(@pstack, \$post); + } + } + } else { # just your ordinary markup + # make sure we didn't reject something TMPL_* but badly formed + if ($options->{strict}) { + die "HTML::Template->new() : Syntax error in <TMPL_*> tag at $fname : $fcounter." if ($chunk =~ /<(?:!--\s*)?\/?[Tt][Mm][Pp][Ll]_/); + } + + # push the rest and get next chunk + if (defined($chunk)) { + if (ref($pstack[$#pstack]) eq 'SCALAR') { + ${$pstack[$#pstack]} .= $chunk; + } else { + push(@pstack, \$chunk); + } + } + } + # count newlines in chunk and advance line count + $fcounter += scalar(@{[$chunk =~ m/(\n)/g]}); + # if we just crossed the end of an included file + # pop off the record and re-alias to the enclosing file's info + pop(@fstack), (*fname, *fcounter, *fmax) = \ ( @{$fstack[$#fstack]} ) + if ($fcounter > $fmax); + + } # next CHUNK + + # make sure we don't have dangling IF or LOOP blocks + scalar(@ifstack) and die "HTML::Template->new() : At least one <TMPL_IF> or <TMPL_UNLESS> not terminated at end of file!"; + scalar(@loopstack) and die "HTML::Template->new() : At least one <TMPL_LOOP> not terminated at end of file!"; + + # resolve pending conditionals + foreach my $uc (@ucstack) { + my $var = $uc->[HTML::Template::COND::VARIABLE]; + if (exists($pmap{$var})) { + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } else { + $pmap{$var} = HTML::Template::VAR->new(); + $top_pmap{$var} = HTML::Template::VAR->new() + if $options->{global_vars} and not exists $top_pmap{$var}; + $uc->[HTML::Template::COND::VARIABLE] = $pmap{$var}; + } + if (ref($pmap{$var}) eq 'HTML::Template::VAR') { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_VAR; + } else { + $uc->[HTML::Template::COND::VARIABLE_TYPE] = HTML::Template::COND::VARIABLE_TYPE_LOOP; + } + } + + # want a stack dump? + if ($options->{stack_debug}) { + require 'Data/Dumper.pm'; + print STDERR "### HTML::Template _param Stack Dump ###\n\n", Data::Dumper::Dumper($self->{parse_stack}), "\n"; + } + + # get rid of filters - they cause runtime errors if Storable tries + # to store them. This can happen under global_vars. + delete $options->{filter}; +} + +# a recursive sub that associates each loop with the loops above +# (treating the top-level as a loop) +sub _globalize_vars { + my $self = shift; + + # associate with the loop (and top-level templates) above in the tree. + push(@{$self->{options}{associate}}, @_); + + # recurse down into the template tree, adding ourself to the end of + # list. + push(@_, $self); + map { $_->_globalize_vars(@_) } + map {values %{$_->[HTML::Template::LOOP::TEMPLATE_HASH]}} + grep { ref($_) eq 'HTML::Template::LOOP'} @{$self->{parse_stack}}; +} + +# method used to recursively un-hook associate +sub _unglobalize_vars { + my $self = shift; + + # disassociate + $self->{options}{associate} = undef; + + # recurse down into the template tree disassociating + map { $_->_unglobalize_vars() } + map {values %{$_->[HTML::Template::LOOP::TEMPLATE_HASH]}} + grep { ref($_) eq 'HTML::Template::LOOP'} @{$self->{parse_stack}}; +} + +=head2 param() + +param() can be called in a number of ways + +1) To return a list of parameters in the template : + + my @parameter_names = $self->param(); + + +2) To return the value set to a param : + + my $value = $self->param('PARAM'); + +3) To set the value of a parameter : + + # For simple TMPL_VARs: + $self->param(PARAM => 'value'); + + # with a subroutine reference that gets called to get the value + # of the scalar. The sub will recieve the template object as a + # parameter. + $self->param(PARAM => sub { return 'value' }); + + # And TMPL_LOOPs: + $self->param(LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ] + ); + +4) To set the value of a a number of parameters : + + # For simple TMPL_VARs: + $self->param(PARAM => 'value', + PARAM2 => 'value' + ); + + # And with some TMPL_LOOPs: + $self->param(PARAM => 'value', + PARAM2 => 'value', + LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ], + ANOTHER_LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ] + ); + +5) To set the value of a a number of parameters using a hash-ref : + + $self->param( + { + PARAM => 'value', + PARAM2 => 'value', + LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ], + ANOTHER_LOOP_PARAM => + [ + { PARAM => VALUE_FOR_FIRST_PASS, ... }, + { PARAM => VALUE_FOR_SECOND_PASS, ... } + ... + ] + } + ); + +=cut + + +sub param { + my $self = shift; + my $options = $self->{options}; + my $param_map = $self->{param_map}; + + # the no-parameter case - return list of parameters in the template. + return keys(%$param_map) unless scalar(@_); + + my $first = shift; + my $type = ref $first; + + # the one-parameter case - could be a parameter value request or a + # hash-ref. + if (!scalar(@_) and !length($type)) { + my $param = $options->{case_sensitive} ? $first : lc $first; + + # check for parameter existence + $options->{die_on_bad_params} and !exists($param_map->{$param}) and + croak("HTML::Template : Attempt to get nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params set => 1)"); + + return undef unless (exists($param_map->{$param}) and + defined($param_map->{$param})); + + return ${$param_map->{$param}} if + (ref($param_map->{$param}) eq 'HTML::Template::VAR'); + return $param_map->{$param}[HTML::Template::LOOP::PARAM_SET]; + } + + if (!scalar(@_)) { + croak("HTML::Template->param() : Single reference arg to param() must be a hash-ref! You gave me a $type.") + unless $type eq 'HASH' or + (ref($first) and UNIVERSAL::isa($first, 'HASH')); + push(@_, %$first); + } else { + unshift(@_, $first); + } + + croak("HTML::Template->param() : You gave me an odd number of parameters to param()!") + unless ((@_ % 2) == 0); + + # strangely, changing this to a "while(@_) { shift, shift }" type + # loop causes perl 5.004_04 to die with some nonsense about a + # read-only value. + for (my $x = 0; $x <= $#_; $x += 2) { + my $param = $options->{case_sensitive} ? $_[$x] : lc $_[$x]; + my $value = $_[($x + 1)]; + + # check that this param exists in the template + $options->{die_on_bad_params} and !exists($param_map->{$param}) and + croak("HTML::Template : Attempt to set nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1)"); + + # if we're not going to die from bad param names, we need to ignore + # them... + next unless (exists($param_map->{$param})); + + # figure out what we've got, taking special care to allow for + # objects that are compatible underneath. + my $value_type = ref($value); + if (defined($value_type) and length($value_type) and ($value_type eq 'ARRAY' or ((ref($value) !~ /^(CODE)|(HASH)|(SCALAR)$/) and $value->isa('ARRAY')))) { + (ref($param_map->{$param}) eq 'HTML::Template::LOOP') or + croak("HTML::Template::param() : attempt to set parameter '$param' with an array ref - parameter is not a TMPL_LOOP!"); + $param_map->{$param}[HTML::Template::LOOP::PARAM_SET] = [@{$value}]; + } else { + (ref($param_map->{$param}) eq 'HTML::Template::VAR') or + croak("HTML::Template::param() : attempt to set parameter '$param' with a scalar - parameter is not a TMPL_VAR!"); + ${$param_map->{$param}} = $value; + } + } +} + +=pod + +=head2 clear_params() + +Sets all the parameters to undef. Useful internally, if nowhere else! + +=cut + +sub clear_params { + my $self = shift; + my $type; + foreach my $name (keys %{$self->{param_map}}) { + $type = ref($self->{param_map}{$name}); + undef(${$self->{param_map}{$name}}) + if ($type eq 'HTML::Template::VAR'); + undef($self->{param_map}{$name}[HTML::Template::LOOP::PARAM_SET]) + if ($type eq 'HTML::Template::LOOP'); + } +} + + +# obsolete implementation of associate +sub associateCGI { + my $self = shift; + my $cgi = shift; + (ref($cgi) eq 'CGI') or + croak("Warning! non-CGI object was passed to HTML::Template::associateCGI()!\n"); + push(@{$self->{options}{associate}}, $cgi); + return 1; +} + + +=head2 output() + +output() returns the final result of the template. In most situations +you'll want to print this, like: + + print $template->output(); + +When output is called each occurrence of <TMPL_VAR NAME=name> is +replaced with the value assigned to "name" via param(). If a named +parameter is unset it is simply replaced with ''. <TMPL_LOOPS> are +evaluated once per parameter set, accumlating output on each pass. + +Calling output() is guaranteed not to change the state of the +Template object, in case you were wondering. This property is mostly +important for the internal implementation of loops. + +You may optionally supply a filehandle to print to automatically as +the template is generated. This may improve performance and lower +memory consumption. Example: + + $template->output(print_to => *STDOUT); + +The return value is undefined when using the "print_to" option. + +=cut + +use vars qw(%URLESCAPE_MAP); +sub output { + my $self = shift; + my $options = $self->{options}; + + croak("HTML::Template->output() : You gave me an odd number of parameters to output()!") + unless ((@_ % 2) == 0); + my %args = @_; + + print STDERR "### HTML::Template Memory Debug ### START OUTPUT ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + $options->{debug} and print STDERR "### HTML::Template Debug ### In output\n"; + + # want a stack dump? + if ($options->{stack_debug}) { + require 'Data/Dumper.pm'; + print STDERR "### HTML::Template output Stack Dump ###\n\n", Data::Dumper::Dumper($self->{parse_stack}), "\n"; + } + + # globalize vars - this happens here to localize the circular + # references created by global_vars. + $self->_globalize_vars() if ($options->{global_vars}); + + # support the associate magic, searching for undefined params and + # attempting to fill them from the associated objects. + if (scalar(@{$options->{associate}})) { + # prepare case-mapping hashes to do case-insensitive matching + # against associated objects. This allows CGI.pm to be + # case-sensitive and still work with asssociate. + my (%case_map, $lparam); + foreach my $associated_object (@{$options->{associate}}) { + # what a hack! This should really be optimized out for case_sensitive. + if ($options->{case_sensitive}) { + map { + $case_map{$associated_object}{$_} = $_ + } $associated_object->param(); + } else { + map { + $case_map{$associated_object}{lc($_)} = $_ + } $associated_object->param(); + } + } + + foreach my $param (keys %{$self->{param_map}}) { + unless (defined($self->param($param))) { + OBJ: foreach my $associated_object (reverse @{$options->{associate}}) { + $self->param($param, scalar $associated_object->param($case_map{$associated_object}{$param})), last OBJ + if (exists($case_map{$associated_object}{$param})); + } + } + } + } + + use vars qw($line @parse_stack); local(*line, *parse_stack); + + # walk the parse stack, accumulating output in $result + *parse_stack = $self->{parse_stack}; + my $result = ''; + + tie $result, 'HTML::Template::PRINTSCALAR', $args{print_to} + if defined $args{print_to} and not tied $args{print_to}; + + my $type; + my $parse_stack_length = $#parse_stack; + for (my $x = 0; $x <= $parse_stack_length; $x++) { + *line = \$parse_stack[$x]; + $type = ref($line); + + if ($type eq 'SCALAR') { + $result .= $$line; + } elsif ($type eq 'HTML::Template::VAR' and ref($$line) eq 'CODE') { + defined($$line) and $result .= $$line->($self); + } elsif ($type eq 'HTML::Template::VAR') { + defined($$line) and $result .= $$line; + } elsif ($type eq 'HTML::Template::LOOP') { + if (defined($line->[HTML::Template::LOOP::PARAM_SET])) { + eval { $result .= $line->output($x, $options->{loop_context_vars}); }; + croak("HTML::Template->output() : fatal error in loop output : $@") + if $@; + } + } elsif ($type eq 'HTML::Template::COND') { + if ($line->[HTML::Template::COND::JUMP_IF_TRUE]) { + if ($line->[HTML::Template::COND::VARIABLE_TYPE] == HTML::Template::COND::VARIABLE_TYPE_VAR) { + if (defined ${$line->[HTML::Template::COND::VARIABLE]}) { + if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if ${$line->[HTML::Template::COND::VARIABLE]}->($self); + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if ${$line->[HTML::Template::COND::VARIABLE]}; + } + } + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if + (defined $line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET] and + scalar @{$line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET]}); + } + } else { + if ($line->[HTML::Template::COND::VARIABLE_TYPE] == HTML::Template::COND::VARIABLE_TYPE_VAR) { + if (defined ${$line->[HTML::Template::COND::VARIABLE]}) { + if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless ${$line->[HTML::Template::COND::VARIABLE]}->($self); + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless ${$line->[HTML::Template::COND::VARIABLE]}; + } + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS]; + } + } else { + $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if + (not defined $line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET] or + not scalar @{$line->[HTML::Template::COND::VARIABLE][HTML::Template::LOOP::PARAM_SET]}); + } + } + } elsif ($type eq 'HTML::Template::NOOP') { + next; + } elsif ($type eq 'HTML::Template::ESCAPE') { + $x++; + *line = \$parse_stack[$x]; + if (defined($$line)) { + my $toencode = $$line; + + # straight from the CGI.pm bible. + $toencode=~s/&/&/g; + $toencode=~s/\"/"/g; #" + $toencode=~s/>/>/g; + $toencode=~s/</</g; + $toencode=~s/'/'/g; #' + + $result .= $toencode; + } + next; + } elsif ($type eq 'HTML::Template::URLESCAPE') { + $x++; + *line = \$parse_stack[$x]; + if (defined($$line)) { + my $toencode = $$line; + # Build a char->hex map if one isn't already available + unless (exists($URLESCAPE_MAP{chr(1)})) { + for (0..255) { $URLESCAPE_MAP{chr($_)} = sprintf('%%%02X', $_); } + } + # do the translation (RFC 2396 ^uric) + $toencode =~ s!([^a-zA-Z0-9_.\-])!$URLESCAPE_MAP{$1}!g; + $result .= $toencode; + } + } else { + confess("HTML::Template::output() : Unknown item in parse_stack : " . $type); + } + } + + # undo the globalization circular refs + $self->_unglobalize_vars() if ($options->{global_vars}); + + print STDERR "### HTML::Template Memory Debug ### END OUTPUT ", $self->{proc_mem}->size(), "\n" + if $options->{memory_debug}; + + return undef if defined $args{print_to}; + return $result; +} + +=pod + +=head2 query() + +This method allow you to get information about the template structure. +It can be called in a number of ways. The simplest usage of query is +simply to check whether a parameter name exists in the template, using +the C<name> option: + + if ($template->query(name => 'foo')) { + # do something if a varaible of any type + # named FOO is in the template + } + +This same usage returns the type of the parameter. The type is the +same as the tag minus the leading 'TMPL_'. So, for example, a +TMPL_VAR parameter returns 'VAR' from query(). + + if ($template->query(name => 'foo') eq 'VAR') { + # do something if FOO exists and is a TMPL_VAR + } + +Note that the variables associated with TMPL_IFs and TMPL_UNLESSs will +be identified as 'VAR' unless they are also used in a TMPL_LOOP, in +which case they will return 'LOOP'. + +C<query()> also allows you to get a list of parameters inside a loop +(and inside loops inside loops). Example loop: + + <TMPL_LOOP NAME="EXAMPLE_LOOP"> + <TMPL_VAR NAME="BEE"> + <TMPL_VAR NAME="BOP"> + <TMPL_LOOP NAME="EXAMPLE_INNER_LOOP"> + <TMPL_VAR NAME="INNER_BEE"> + <TMPL_VAR NAME="INNER_BOP"> + </TMPL_LOOP> + </TMPL_LOOP> + +And some query calls: + + # returns 'LOOP' + $type = $template->query(name => 'EXAMPLE_LOOP'); + + # returns ('bop', 'bee', 'example_inner_loop') + @param_names = $template->query(loop => 'EXAMPLE_LOOP'); + + # both return 'VAR' + $type = $template->query(name => ['EXAMPLE_LOOP', 'BEE']); + $type = $template->query(name => ['EXAMPLE_LOOP', 'BOP']); + + # and this one returns 'LOOP' + $type = $template->query(name => ['EXAMPLE_LOOP', + 'EXAMPLE_INNER_LOOP']); + + # and finally, this returns ('inner_bee', 'inner_bop') + @inner_param_names = $template->query(loop => ['EXAMPLE_LOOP', + 'EXAMPLE_INNER_LOOP']); + + # for non existent parameter names you get undef + # this returns undef. + $type = $template->query(name => 'DWEAZLE_ZAPPA'); + + # calling loop on a non-loop parameter name will cause an error. + # this dies: + $type = $template->query(loop => 'DWEAZLE_ZAPPA'); + +As you can see above the C<loop> option returns a list of parameter +names and both C<name> and C<loop> take array refs in order to refer +to parameters inside loops. It is an error to use C<loop> with a +parameter that is not a loop. + +Note that all the names are returned in lowercase and the types are +uppercase. + +Just like C<param()>, C<query()> with no arguements returns all the +parameter names in the template at the top level. + +=cut + +sub query { + my $self = shift; + $self->{options}{debug} and print STDERR "### HTML::Template Debug ### query(", join(', ', @_), ")\n"; + + # the no-parameter case - return $self->param() + return $self->param() unless scalar(@_); + + croak("HTML::Template::query() : Odd number of parameters passed to query!") + if (scalar(@_) % 2); + croak("HTML::Template::query() : Wrong number of parameters passed to query - should be 2.") + if (scalar(@_) != 2); + + my ($opt, $path) = (lc shift, shift); + croak("HTML::Template::query() : invalid parameter ($opt)") + unless ($opt eq 'name' or $opt eq 'loop'); + + # make path an array unless it already is + $path = [$path] unless (ref $path); + + # find the param in question. + my @objs = $self->_find_param(@$path); + return undef unless scalar(@objs); + my ($obj, $type); + + # do what the user asked with the object + if ($opt eq 'name') { + # we only look at the first one. new() should make sure they're + # all the same. + ($obj, $type) = (shift(@objs), shift(@objs)); + return undef unless defined $obj; + return 'VAR' if $type eq 'HTML::Template::VAR'; + return 'LOOP' if $type eq 'HTML::Template::LOOP'; + croak("HTML::Template::query() : unknown object ($type) in param_map!"); + + } elsif ($opt eq 'loop') { + my %results; + while(@objs) { + ($obj, $type) = (shift(@objs), shift(@objs)); + croak("HTML::Template::query() : Search path [", join(', ', @$path), "] doesn't end in a TMPL_LOOP - it is an error to use the 'loop' option on a non-loop parameter. To avoid this problem you can use the 'name' option to query() to check the type first.") + unless ((defined $obj) and ($type eq 'HTML::Template::LOOP')); + + # SHAZAM! This bit extracts all the parameter names from all the + # loop objects for this name. + map {$results{$_} = 1} map { keys(%{$_->{'param_map'}}) } + values(%{$obj->[HTML::Template::LOOP::TEMPLATE_HASH]}); + } + # this is our loop list, return it. + return keys(%results); + } +} + +# a function that returns the object(s) corresponding to a given path and +# its (their) ref()(s). Used by query() in the obvious way. +sub _find_param { + my $self = shift; + my $spot = $self->{options}{case_sensitive} ? shift : lc shift; + + # get the obj and type for this spot + my $obj = $self->{'param_map'}{$spot}; + return unless defined $obj; + my $type = ref $obj; + + # return if we're here or if we're not but this isn't a loop + return ($obj, $type) unless @_; + return unless ($type eq 'HTML::Template::LOOP'); + + # recurse. this is a depth first seach on the template tree, for + # the algorithm geeks in the audience. + return map { $_->_find_param(@_) } + values(%{$obj->[HTML::Template::LOOP::TEMPLATE_HASH]}); +} + +# HTML::Template::VAR, LOOP, etc are *light* objects - their internal +# spec is used above. No encapsulation or information hiding is to be +# assumed. + +package HTML::Template::VAR; + +sub new { + my ($pkg) = @_; + my $value; + my $self = \$value; + bless($self, $pkg); + return $self; +} + +package HTML::Template::LOOP; + +sub new { + my ($pkg) = shift; + my $self = []; + bless($self, $pkg); + return $self; +} + +sub output { + my $self = shift; + my $index = shift; + my $loop_context_vars = shift; + my $template = $self->[TEMPLATE_HASH]{$index}; + my $value_sets_array = $self->[PARAM_SET]; + return unless defined($value_sets_array); + + my $result = ''; + my $count = 0; + my $odd = 0; + foreach my $value_set (@$value_sets_array) { + if ($loop_context_vars) { + if ($count == 0) { + @{$value_set}{qw(__first__ __inner__ __last__)} = (1,0,$#{$value_sets_array} == 0); + } elsif ($count == $#{$value_sets_array}) { + @{$value_set}{qw(__first__ __inner__ __last__)} = (0,0,1); + } else { + @{$value_set}{qw(__first__ __inner__ __last__)} = (0,1,0); + } + $odd = $value_set->{__odd__} = not $odd; + } + $template->param($value_set); + $result .= $template->output; + $template->clear_params; + @{$value_set}{qw(__first__ __last__ __inner__ __odd__)} = (0,0,0,0) + if ($loop_context_vars); + $count++; + } + + return $result; +} + +package HTML::Template::COND; + +sub new { + my $pkg = shift; + my $var = shift; + my $self = []; + $self->[VARIABLE] = $var; + + bless($self, $pkg); + return $self; +} + +package HTML::Template::NOOP; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + +package HTML::Template::ESCAPE; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + +package HTML::Template::URLESCAPE; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + +# scalar-tying package for output(print_to => *HANDLE) implementation +package HTML::Template::PRINTSCALAR; +use strict; + +sub TIESCALAR { bless \$_[1], $_[0]; } +sub FETCH { } +sub STORE { + my $self = shift; + local *FH = $$self; + print FH @_; +} +1; +__END__ + +=head1 FREQUENTLY ASKED QUESTIONS + +In the interest of greater understanding I've started a FAQ section of +the perldocs. Please look in here before you send me email. + +=over 4 + +=item 1 + +Q: Is there a place to go to discuss HTML::Template and/or get help? + +A: There's a mailing-list for HTML::Template at htmltmpl@lists.vm.com. +Send a blank message to htmltmpl-subscribe@lists.vm.com to join! + +=item 2 + +Q: Is there a searchable archive for the mailing-list? + +A: Yes, thanks to Sean P. Scanlon, there is: + + http://bluedot.net/mail/archive/ + +=item 3 + +Q: I want support for <TMPL_XXX>! How about it? + +A: Maybe. I definitely encourage people to discuss their ideas for +HTML::Template on the mailing list. Please be ready to explain to me +how the new tag fits in with HTML::Template's mission to provide a +fast, lightweight system for using HTML templates. + +NOTE: Offering to program said addition and provide it in the form of +a patch to the most recent version of HTML::Template will definitely +have a softening effect on potential opponents! + +=item 4 + +Q: I found a bug, can you fix it? + +A: That depends. Did you send me the VERSION of HTML::Template, a test +script and a test template? If so, then almost certainly. + +If you're feeling really adventurous, HTML::Template has a publically +available CVS server. See below for more information in the PUBLIC +CVS SERVER section. + +=item 5 + +Q: <TMPL_VAR>s from the main template aren't working inside a +<TMPL_LOOP>! Why? + +A: This is the intended behavior. <TMPL_LOOP> introduces a separate +scope for <TMPL_VAR>s much like a subroutine call in Perl introduces a +separate scope for "my" variables. + +If you want your <TMPL_VAR>s to be global you can set the +'global_vars' option when you call new(). See above for documentation +of the 'global_vars' new() option. + +=item 6 + +Q: Why do you use /[Tt]/ instead of /t/i? It's so ugly! + +A: Simple - the case-insensitive match switch is very inefficient. +According to _Mastering_Regular_Expressions_ from O'Reilly Press, +/[Tt]/ is faster and more space efficient than /t/i - by as much as +double against long strings. //i essentially does a lc() on the +string and keeps a temporary copy in memory. + +When this changes, and it is in the 5.6 development series, I will +gladly use //i. Believe me, I realize [Tt] is hideously ugly. + +=item 7 + +Q: How can I pre-load my templates using cache-mode and mod_perl? + +A: Add something like this to your startup.pl: + + use HTML::Template; + use File::Find; + + print STDERR "Pre-loading HTML Templates...\n"; + find( + sub { + return unless /\.tmpl$/; + HTML::Template->new( + filename => "$File::Find::dir/$_", + cache => 1, + ); + }, + '/path/to/templates', + '/another/path/to/templates/' + ); + +Note that you'll need to modify the "return unless" line to specify +the extension you use for your template files - I use .tmpl, as you +can see. You'll also need to specify the path to your template files. + +One potential problem: the "/path/to/templates/" must be EXACTLY the +same path you use when you call HTML::Template->new(). Otherwise the +cache won't know they're the same file and will load a new copy - +instead getting a speed increase, you'll double your memory usage. To +find out if this is happening set cache_debug => 1 in your application +code and look for "CACHE MISS" messages in the logs. + +=item 8 + +Q: What characters are allowed in TMPL_* NAMEs? + +A: Numbers, letters, '.', '/', '+', '-' and '_'. + +=item 9 + +Q: How can I execute a program from inside my template? + +A: Short answer: you can't. Longer answer: you shouldn't since this +violates the fundamental concept behind HTML::Template - that design +and code should be seperate. + +But, inevitably some people still want to do it. If that describes +you then you should take a look at +L<HTML::Template::Expr|HTML::Template::Expr>. Using +HTML::Template::Expr it should be easy to write a run_program() +function. Then you can do awful stuff like: + + <tmpl_var expr="run_program('foo.pl')"> + +Just, please, don't tell me about it. I'm feeling guilty enough just +for writing HTML::Template::Expr in the first place. + +=item 10 + +Q: Can I get a copy of these docs in Japanese? + +A: Yes you can. See Kawai Takanori's translation at: + + http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm + +=item 11 + +Q: What's the best way to create a <select> form element using +HTML::Template? + +A: There is much disagreement on this issue. My personal preference +is to use CGI.pm's excellent popup_menu() and scrolling_list() +functions to fill in a single <tmpl_var select_foo> variable. + +To some people this smacks of mixing HTML and code in a way that they +hoped HTML::Template would help them avoid. To them I'd say that HTML +is a violation of the principle of separating design from programming. +There's no clear separation between the programmatic elements of the +<form> tags and the layout of the <form> tags. You'll have to draw +the line somewhere - clearly the designer can't be entirely in charge +of form creation. + +It's a balancing act and you have to weigh the pros and cons on each side. +It is certainly possible to produce a <select> element entirely inside the +template. What you end up with is a rat's nest of loops and conditionals. +Alternately you can give up a certain amount of flexibility in return for +vastly simplifying your templates. I generally choose the latter. + +Another option is to investigate HTML::FillInForm which some have +reported success using to solve this problem. + +=back + +=head1 BUGS + +I am aware of no bugs - if you find one, join the mailing list and +tell us about it (htmltmpl@lists.vm.com). You can join the +HTML::Template mailing-list by sending a blank email to +htmltmpl-subscribe@lists.vm.com. Of course, you can still email me +directly (sam@tregar.com) with bugs, but I reserve the right to +forward bug reports to the mailing list. + +When submitting bug reports, be sure to include full details, +including the VERSION of the module, a test script and a test template +demonstrating the problem! + +If you're feeling really adventurous, HTML::Template has a publically +available CVS server. See below for more information in the PUBLIC +CVS SERVER section. + +=head1 CREDITS + +This module was the brain child of my boss, Jesse Erlbaum +( jesse@vm.com ) at Vanguard Media ( http://vm.com ) . The most original +idea in this module - the <TMPL_LOOP> - was entirely his. + +Fixes, Bug Reports, Optimizations and Ideas have been generously +provided by: + + Richard Chen + Mike Blazer + Adriano Nagelschmidt Rodrigues + Andrej Mikus + Ilya Obshadko + Kevin Puetz + Steve Reppucci + Richard Dice + Tom Hukins + Eric Zylberstejn + David Glasser + Peter Marelas + James William Carlson + Frank D. Cringle + Winfried Koenig + Matthew Wickline + Doug Steinwand + Drew Taylor + Tobias Brox + Michael Lloyd + Simran Gambhir + Chris Houser <chouser@bluweb.com> + Larry Moore + Todd Larason + Jody Biggs + T.J. Mather + Martin Schroth + Dave Wolfe + uchum + Kawai Takanori + Peter Guelich + Chris Nokleberg + Ralph Corderoy + William Ward + Ade Olonoh + Mark Stosberg + Lance Thomas + Roland Giersig + Jere Julian + Peter Leonard + Kenny Smith + Sean P. Scanlon + Martin Pfeffer + David Ferrance + Gyepi Sam + Darren Chamberlain + +Thanks! + +=head1 WEBSITE + +You can find information about HTML::Template and other related modules at: + + http://html-template.sourceforge.net + +=head1 PUBLIC CVS SERVER + +HTML::Template now has a publicly accessible CVS server provided by +SourceForge (www.sourceforge.net). You can access it by going to +http://sourceforge.net/cvs/?group_id=1075. Give it a try! + +=head1 AUTHOR + +Sam Tregar, sam@tregar.com (you can also find me on the mailing list +at htmltmpl@lists.vm.com - join it by sending a blank message to +htmltmpl-subscribe@lists.vm.com). + +=head1 LICENSE + + HTML::Template : A module for using HTML Templates with Perl + Copyright (C) 2000-2002 Sam Tregar (sam@tregar.com) + + This module is free software; you can redistribute it and/or modify it + under the terms of either: + + a) the GNU General Public License as published by the Free Software + Foundation; either version 1, or (at your option) any later version, + + or + + b) the "Artistic License" which comes with this module. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either + the GNU General Public License or the Artistic License for more details. + + You should have received a copy of the Artistic License with this + module, in the file ARTISTIC. If not, I'll be glad to provide one. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA + +=cut |
