OpenInteract2::ContentGenerator::TemplateSource - Common routines for loading content from OI2 templates
# Sample from Text::Template content generator sub generate { my ( $self, $template_config, $template_vars, $template_source ) = @_; $log ||= get_logger( LOG_TEMPLATE ); my ( $source_type, $source ) = OpenInteract2::ContentGenerator::TemplateSource->identify( $template_source ); if ( $source_type eq 'NAME' ) { my ( $template, $filename, $modified ) = OpenInteract2::ContentGenerator::TemplateSource->load_source( $source ); $source_type = 'STRING'; $source = $template; $log->is_debug && $log->debug( "Loading from name $source" ); } else { $log->is_debug && $Log->Debug( "Loading from source $source_type" ); } $template_config->{TYPE} = $source_type; $template_config->{SOURCE} = ( ref $source eq 'SCALAR' ) ? $$source : $source; my $template = Text::Template->new( %{ $template_config } ); unless ( $template ) { my $msg = "Failed to create template parsing object: " . $Text::Template::ERROR; $log->error( $msg ); oi_error $msg; } my $content = $template->fill_in( HASH => $template_vars ); unless ( $content ) { my $msg = "Failed to fill in template: $Text::Template::ERROR"; $log->error( $msg ); oi_error $msg ; } return $content; }
identify( \%template_source )
Checks \%template_source for template information and returns a source type and source. Here are the types of information we check for in \%template_source and what is returned:
\%template_source
Key name: Set source type to 'NAME' and source to the value of the name key. (This is the most common condition.)
name
Key message_key: If we can lookup a template name from the language handle retured by the OpenInteract2::Request}OpenInteract2::Request object set source type to 'NAME' and source to the value of the message key found from the language handle.
Throws an exception if the language handle does not return a value for the message key lookup (that is, you do not have the key defined in any of your message files).
Key text: Set source type to 'STRING' and source to a scalar reference with the value of the text key. If text is already a reference it just copies the reference, otherwise it takes a reference to the text in the key.
text
Key filehandle: Set source type to 'FILE' and source to the filehandle in filehandle.
filehandle
Key object: Set source type to 'STRING' and source to a reference to the content of the template key of the OpenInteract2::SiteTemplate object in object.
template
object
If none of these are found an exception is thrown. (We throw a different exception if you use the ancient 'db'/'package' syntax.)
Additionally, if we are able to pull a name from the template source and the current OpenInteract2::Controller object can handle it, we call add_template_used() on it, passing it the template name.
add_template_used()
Returns: two item list of source type and source.
load_source( $template_name )
Fetches the template with the fully-qualified name $template_name and returns a three-item list with: contents, full filename, and the last modified time.
$template_name
If the template is not found we throw an exception, and any exception thrown from the fetch propogates up.
Returns: a three-item list with: contents, full filename, and the last modified time (which is a DateTime object).
Copyright (c) 2002-2004 Chris Winters. All rights reserved.
Chris Winters <chris@cwinters.com>
To install OpenInteract2::URL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm OpenInteract2::URL
CPAN shell
perl -MCPAN -e shell install OpenInteract2::URL
For more information on module installation, please visit the detailed CPAN module installation guide.