CGI-Application-Plugin-AnyTemplate

 view release on metacpan or  search on metacpan

lib/CGI/Application/Plugin/AnyTemplate/Base.pm  view on Meta::CPAN

        $self->{'param'} ||= {};
        return keys %{ $self->{'param'} };
    }
}

=item get_param_hash

Returns the template variables as a hash of names and values.

    my %params     = $template->get_param_hash;

In a scalar context, returns a reference to the hash used
internally to contain the values:

    my $params_ref = $template->get_param_hash;

=cut

sub get_param_hash {
    my $self = shift;
    $self->{'param'} ||= {};
    return %{ $self->{'param'} } if wantarray;
    return $self->{'param'};
}

=item clear_params

Clears the values stored in the template:

    $template->param(
        'name1' => 'value1',
        'name1' => 'value2'
    );
    $template->clear_params;
    $template->param(
        'name_foo' => 'value_bar',
    );

    # params are now:
        'name_foo' => 'value_bar',


=cut

sub clear_params {
    my $self = shift;
    $self->{'param'} = {};
}

=item output

Returns the template with all the values filled in.

    return $template->output();

You can also supply names and values to the template at this stage:

    return $template->output('name' => 'value', 'name2' => 'value2');

Before the template output is generated, the C<< template_pre_process >>
hook is called.  Any callbacks that you register to this hook will be
called before each template is processed.  Register a
C<template_pre_process> callback as follows:

    $self->add_callback('template_pre_process', \&my_tmpl_pre_process);

Pre-process callbacks will be passed a reference to the C<$template>
object, and can can modify the parameters passed into the template by
using the C<param> method:

    sub my_tmpl_pre_process {
        my ($self, $template) = @_;

        # Change the internal template parameters by reference
        my $params = $template->get_param_hash;

        foreach my $key (keys %$params) {
            $params{$key} = to_piglatin($params{$key});
        }

        # Can also set values using the param method
        $template->param('foo', 'bar');

    }


After the template output is generated, the C<template_post_process> hook is called.
You can register a C<template_post_process> callback as follows:

    $self->add_callback('template_post_process', \&my_tmpl_post_process);

Any callbacks that you register to this hook will be called after each
template is processed, and will be passed both a reference to the
template object and a reference to the output generated by the template.
This allows you to modify the output of the template:

    sub my_tmpl_post_process {
        my ($self, $template, $output_ref) = @_;

        $$output_ref =~ s/foo/bar/;
    }




When you call the C<output> method, any components embedded in the
template are run.  See C<EMBEDDED COMPONENTS>, below.

=cut

# calling forms:
#    $template->output;
#    $template->output('file.html', \%params);
#    $template->output(\%params)
#
#    $template->fill;
#    $template->fill('file.html', \%params);
#    $template->fill(\%params)

sub output {
    my $self = shift;

    $self->param(@_);

    my $webapp = $self->{'webapp'};

    if ($webapp and $webapp->can('call_hook')) {
        $webapp->call_hook('template_pre_process', $self);
    }

    my $output = $self->render_template;

    if ($webapp and $webapp->can('call_hook')) {
        my $output_param = $output;
        $output_param = \$output_param unless ref $output_param;
        $webapp->call_hook('template_post_process', $self, $output_param);
    }
    if ($self->{'return_references'}) {
        return ref $output ? $output : \$output;
    }
    else {
        return ref $output ? $$output : $output;
    }
}

=item filename

If the template was loaded from a file, the C<filename> method returns the template filename.

=cut

sub filename {



( run in 1.438 second using v1.01-cache-2.11-cpan-39bf76dae61 )