HTML-Template

 view release on metacpan or  search on metacpan

lib/HTML/Template.pm  view on Meta::CPAN


had been specified for each key/value pair that would be provided by the
C<< $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],
    );

B<NOTE>: The parameter names are matched in a case-insensitive 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 C<case_sensitive> 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 C<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.

B<NOTE>: with C<case_sensitive> and C<loop_context_vars> the special
loop variables are available in lower-case only.

=item * loop_context_vars

When this parameter is set to true (it is false by default) extra variables
that depend on the loop's context are made available inside a loop. These are:

=over

=item * __first__

Value that is true for the first iteration of the loop and false every other time.

=item * __last__

Value that is true for the last iteration of the loop and false every other time.

=item * __inner__

Value that is true for the every iteration of the loop except for the first and last.

=item * __outer__

Value that is true for the first and last iterations of the loop.

=item * __odd__

Value that is true for the every odd iteration of the loop.

=item * __even__

Value that is true for the every even iteration of the loop.

=item * __counter__

An integer (starting from 1) whose value increments for each iteration of the loop.

=item * __index__

An integer (starting from 0) whose value increments for each iteration of the loop.

=back

Just like any other C<TMPL_VAR>s these variables can be used in 
C<< <TMPL_IF> >>, C<< <TMPL_UNLESS> >> and C<< <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_UNLESS>

      <TMPL_IF NAME="__inner__">
        This outputs on passes that are neither first nor last.
      </TMPL_IF>

      This is pass number <TMPL_VAR NAME="__counter__">.

      <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 C<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 something like:

  Apples, Oranges, Brains, Toes, and Kiwi.



( run in 2.288 seconds using v1.01-cache-2.11-cpan-71847e10f99 )