package HTML::Template; $HTML::Template::VERSION = '2.2'; =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 For example, test.tmpl: Test Template My Home Directory is

My Path is set to Now create a small CGI program: use HTML::Template; # open the html template my $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters $template->param( HOME => $ENV{HOME}, PATH => $ENV{PATH}, ); # send the obligatory Content-Type print "Content-Type: text/html\n\n"; # print the template print $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 - , , , and . 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. A Japanese translation of the documentation is available at: http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm This module is licensed under the GPL. See the LICENSE section below for more details. =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 Note: even though these tags look like HTML they are a little different in a couple of ways. First, they must appear entirely on one line. Second, they're allowed to "break the rules". Something like: 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 - "" 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. In order to realize a dramatic savings in bandwidth, the standard (non-comment) tags will be used throughout the rest of this documentation. =head2 The tag is very simple. For each tag in the template you call $template->param(PARAMETER_NAME => "VALUE"). When the template is output the 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: "> 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: "> 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 The tag is a bit more complicated. The tag allows you to delimit a section of text and give it a name. Inside the you place s. Now you pass to param() a list (an array ref) of parameter assignments (hash refs). The loop iterates over this list and produces output from the text block for each pass. Unset parameters are skipped. Here's an example: In the template: Name:

Job:

In the script: $template->param(EMPLOYEE_INFO => [ { name => 'Sam', job => 'programmer' }, { name => 'Steve', job => 'soda jerk' }, ] ); print $template->output(); The output: Name: Sam

Job: programmer

Name: Steve

Job: soda jerk

As you can see above the takes a list of variable assignments and then iterates over the loop body producing output. Often you'll want to generate a '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: Word:
Number:

It would produce output like: Word: I Number: 1 Word: Am Number: 2 Word: Cool Number: 3 s within 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('ROW',[ { name => 'Bobby', nicknames => [ { name => 'the big bad wolf' }, { name => 'He-Man' }, ], }, ], ); Basically, each 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 , the only variables that are usable are the ones from the . The variables in the outer blocks are not visible within a template loop. For the computer-science geeks among you, a 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 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 a full path - beginning with a '/'. If it isn't a full path, the path to the enclosing file is tried first. After that the path in the environment variable HTML_TEMPLATE_ROOT is tried next, if it exists. Next, the "path" new() 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 The 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: Some text that only gets displayed if BOOL is true! Now if you call $template->param(BOOL => 1) then the above block will be included by output. blocks can include any valid HTML::Template construct - VARs and LOOPs and other IF/ELSE blocks. Note, however, that intersecting a and a is invalid. Not going to work: 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: This will output if the loop is not empty. .... 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 You can include an alternate block in your TMPL_IF block by using TMPL_ELSE. NOTE: You still end the block with , not ! Example: Some text that is included only if BOOL is true Some text that is included only if BOOL is false =head2 This tag is the opposite of . The block is output if the CONTROL_PARAMETER is set false or not defined. You can use with just as you can with . Example: Some text that is output only if BOOL is FALSE. Some text that is output only if BOOL is TRUE. If the name of a TMPL_LOOP is used in a TMPL_UNLESS, the UNLESS block output if the loop has zero rows. This will output if the loop is empty. .... =cut =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. The three 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'); 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 * 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: Would normally cause an error, but if you call new with strict => 0, HTML::Template will ignore it. Defaults to 1. =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 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 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. =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. =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 , and to control how a loop is output. Example: This only outputs on the first pass. This outputs every other pass, on the odd passes. This outputs every other pass, on the even passes. This outputs on passes that are neither first nor last. This only outputs on the last pass. One use of this feature is to provide a "separator" similar in effect to the perl function join(). Example: and , . 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 * 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 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 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 * no_includes - set this option to 1 to disallow the tag in the template file. This can be used to make opening untrusted templates B 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 * 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 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. =item * global_vars - normally variables declared outside a loop are not available inside a loop. This option makes s like global variables in Perl - they have unlimited scope. This option also affects and . Example: This is a normal variable: .

Here it is inside the loop:

Normally this wouldn't work as expected, since 's value outside the loop is not available inside the 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_(.*?)!!!//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". my $template = HTML::Template->new(filename => 'zap.tmpl', filter => { sub => $filter, format => 'array' }); Using the 'array' format is likely to be more efficient since this is how HTML::Template stores the template text internally. This may change in the future. 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. =item * vanguard_compatibility_mode - if set to 1 the module will expect to see 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. =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 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 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 croak("HTML::Template->new() : type parameter must be set to 'filename', 'arrayref' or 'scalarref'!"); $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->{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"); } # 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_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}; 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 the array, note the mtime for the record my @templateArray =