CGI-Application-Plugin-AnyTemplate
view release on metacpan or search on metacpan
lib/CGI/Application/Plugin/AnyTemplate.pm view on Meta::CPAN
else {
# args: hash
$config = { @_ };
}
}
# set current configuration from base
if (keys %$config) {
$self->{'current_config'} = Clone::clone($self->{'base_config'});
$self->_add_configuration($self->{'current_config'}, $config);
}
else {
$self->{'current_config'} = $self->{'base_config'};
}
my $plugin_config = $self->{'current_config'}{'plugin_config'};
my $return_references = 1;
if (exists $plugin_config->{'return_references'}) {
$return_references = $plugin_config->{'return_references'};
}
# determine template type
my $type = $plugin_config->{'type'}
|| $plugin_config->{'default_type'}
|| $self->_default_type;
# require driver
my $driver_class = $self->_require_driver($type);
# Generate driver config and native config
my %driver_config = $driver_class->default_driver_config;
# Copy all params with keys listed in the driver_config_keys
foreach my $param ($driver_class->driver_config_keys) {
if (exists $self->{'current_config'}{'driver_config'}{$type}{$param}) {
$driver_config{$param} = $self->{'current_config'}{'driver_config'}{$type}{$param};
}
}
# Copy whatever is left over into native config
my $native_config = $self->{'current_config'}{'native_config'}{$type};
foreach my $param (keys %{ $self->{'current_config'}{'driver_config'}{$type} }) {
# skip values copied into %driver_config
if (not exists $driver_config{$param}) {
$native_config->{$param} = $self->{'current_config'}{'driver_config'}{$type}{$param};
}
}
# Get the string, if supplied
my $string_ref;
if (exists $plugin_config->{'string'}) {
$string_ref = $plugin_config->{'string'} || '';
$string_ref = \$string_ref unless ref $string_ref;
}
# if no string, then guess template filename
my $filename;
unless ($string_ref) {
$filename = $self->_guess_template_filename($plugin_config, \%driver_config, $type, $self->{'webapp'}->get_current_runmode);
}
# call load_tmpl hook
my %tmpl_params;
if ($self->{'webapp'}->can('call_hook')) {
my %ht_params = (
'path' => $plugin_config->{'add_include_paths'},
);
$self->{'webapp'}->new_hook('load_tmpl');
$self->{'webapp'}->call_hook(
'load_tmpl',
\%ht_params,
\%tmpl_params,
$filename,
);
$plugin_config->{'add_include_paths'} = $ht_params{'path'};
}
# manage include paths
my $include_paths = $self->_merged_include_paths($plugin_config);
# create and initialize driver
my $driver = $driver_class->_new(
'driver_config' => \%driver_config,
'native_config' => $native_config,
'include_paths' => $include_paths,
'filename' => $filename,
'string_ref' => $string_ref,
'return_references' => $return_references,
'callers_package' => $plugin_config->{'callers_package'},
'webapp' => $self->{'webapp'},
'conf_name' => $self->{'conf_name'},
'component_handler_class' => $plugin_config->{'component_handler_class'},
);
# Store the tmpl params we picked up from the load_tmpl hook
if (keys %tmpl_params) {
$driver->param(%tmpl_params);
}
return $driver;
}
# These are the param keys of the plugin_config (see below)
sub _plugin_config_keys {
qw/
callers_package
auto_add_template_extension
default_type
type
include_paths
add_include_paths
file
string
component_handler_class
lib/CGI/Application/Plugin/AnyTemplate.pm view on Meta::CPAN
if (my $tmpl_path = $self->{'webapp'}->tmpl_path) {
if (ref $tmpl_path ne 'ARRAY') {
$tmpl_path = [ $tmpl_path ];
}
push @include_paths, @$tmpl_path;
}
if ($config->{'include_paths'} and ref $config->{'include_paths'} ne 'ARRAY') {
$config->{'include_paths'} = [ $config->{'include_paths'} ];
}
push @include_paths, @{ $config->{'include_paths'} || [] };
$config->{'add_include_paths'} ||= [];
$config->{'add_include_paths'} = [$config->{'add_include_paths'}] unless ref $config->{'add_include_paths'} eq 'ARRAY';
unshift @include_paths, @{$config->{'add_include_paths'}};
# remove duplicates
my %seen_include_path;
my @unique_include_paths;
foreach my $path (@include_paths) {
next if $seen_include_path{$path};
$seen_include_path{$path} = 1;
push @unique_include_paths, $path;
}
return @unique_include_paths if wantarray;
return \@unique_include_paths;
}
# Finds a template driver beneath the namespace of the current package
# followed by '::Driver::'. Requires this module and returns its package
# name
#
#
# For instance:
# $module = _require_driver('HTMLTemplate');
# print $module; # 'CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate'
sub _require_driver {
my ($self, $driver) = @_;
# only allow word characters and colons
if ($driver =~ /[^\w:]/) {
croak "CAP::AnyTemplate: Illegal template driver name: $driver\n";
}
my $module = (ref $self) . '::Driver::' . $driver;
eval "require $module";
if ($@) {
croak "CAP::AnyTemplate: template driver $module could not be found: $@";
}
return $module;
}
sub _guess_template_filename {
my ($self, $plugin_config, $driver_config, $type, $crm) = @_;
my $filename;
if (exists $plugin_config->{'file'}) {
$filename = $plugin_config->{'file'};
}
else {
# filename set to current run mode
$filename = $crm;
if (ref $plugin_config->{'template_filename_generator'} eq 'CODE') {
$filename = $plugin_config->{'template_filename_generator'}->($self->{'webapp'});
}
}
if ($plugin_config->{'auto_add_template_extension'}) {
# add extension
my $extension = $driver_config->{'template_extension'}
|| $self->_default_extension;
$filename = $filename . $extension;
}
return $filename;
}
=head2 fill
Fill is a convenience method which in a single step creates the
template, fills it with the template paramters and returns its output.
You can call it with or without a filename (or string ref).
The code:
$self->template->fill('filename', \%params);
is equivalent to:
my $template = $self->template->load('filename');
$template->output(\%params);
And the code:
$self->template->fill(\$some_text, \%params);
is equivalent to:
my $template = $self->template->load(\$some_text);
$template->output(\%params);
And the code:
$self->template->fill(\%params);
is equivalent to:
( run in 1.893 second using v1.01-cache-2.11-cpan-39bf76dae61 )