CGI-Application-Plugin-TT-1.06 000755 001750 001750 0 14721400126 14640 5 ustar 00wes wes 000000 000000 README 100644 001750 001750 43040 14721400126 15622 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 SYNOPSIS
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
sub myrunmode {
my $self = shift;
my %params = (
email => 'email@company.com',
menu => [
{ title => 'Home', href => '/home.html' },
{ title => 'Download', href => '/download.html' },
],
session_obj => $self->session,
);
return $self->tt_process('template.tmpl', \%params);
}
DESCRIPTION
CGI::Application::Plugin::TT adds support for the popular Template
Toolkit engine to your CGI::Application modules by providing several
helper methods that allow you to process template files from within
your runmodes.
It compliments the support for HTML::Template that is built into
CGI::Application through the load_tmpl method. It also provides a few
extra features than just the ability to load a template.
METHODS
tt_process
This is a simple wrapper around the Template Toolkit process method. It
accepts zero, one or two parameters; an optional template filename, and
an optional hashref of template parameters (the template filename is
optional, and will be autogenerated by a call to
$self->tt_template_name if not provided). The return value will be a
scalar reference to the output of the template.
package My::App::Browser
sub myrunmode {
my $self = shift;
return $self->tt_process( 'Browser/myrunmode.tmpl', { foo => 'bar' } );
}
sub myrunmode2 {
my $self = shift;
return $self->tt_process( { foo => 'bar' } ); # will process template 'My/App/Browser/myrunmode2.tmpl'
}
tt_config
This method can be used to customize the functionality of the
CGI::Application::Plugin::TT module, and the Template Toolkit module
that it wraps. The recommended place to call tt_config is as a class
method in the global scope of your module (See SINGLETON SUPPORT for an
explanation of why this is a good idea). If this method is called after
a call to tt_process or tt_obj, then it will die with an error message.
It is not a requirement to call this method, as the module will work
without any configuration. However, most will find it useful to set at
least a path to the location of the template files ( or you can set the
path later using the tt_include_path method).
our $TEMPLATE_OPTIONS = {
COMPILE_DIR => '/tmp/tt_cache',
DEFAULT => 'notfound.tmpl',
PRE_PROCESS => 'defaults.tmpl',
};
__PACKAGE__->tt_config( TEMPLATE_OPTIONS => $TEMPLATE_OPTIONS );
The following parameters are accepted:
TEMPLATE_OPTIONS
This allows you to customize how the Template object is created by
providing a list of options that will be passed to the Template
constructor. Please see the documentation for the Template module for
the exact syntax of the parameters, or see below for an example.
TEMPLATE_NAME_GENERATOR
This allows you to provide your own method for auto-generating the
template filename. It requires a reference to a function that will be
passed the $self object as it's only parameter. This function will be
called every time $self->tt_process is called without providing the
filename of the template to process. This can standardize the way
templates are organized and structured by making the template
filenames follow a predefined pattern.
The default template filename generator uses the current module name,
and the name of the calling function to generate a filename. This
means your templates are named by a combination of the module name,
and the runmode.
TEMPLATE_PRECOMPILE_DIR
This options allows you to specify a directory (or an array of
directories) to search when this module is loaded and then compile
all files found into memory. This provides a speed boost in
persistent environments (mod_perl, fast-cgi) and can improve memory
usage in environments that use shared memory (mod_perl).
TEMPLATE_PRECOMPILE_FILETEST
This option allows you to specify exactly which files will get
compiled when using the TEMPLATE_PRECOMPILE_DIR option. You can
provide it with one of 3 different variable types:
STRING
A filename extension that can specify what type of files will be
loaded (eg 'tmpl').
REGEXP
Filenames that match the regular expression will be precompiled (
eg qr/\.(tt|tmpl|html)$/ ).
CODEREF
A code reference that will be called once for each filename and
directory found, and if it returns true, the template will be
precompiled (eg sub { my $file = shift; ... } ).
tt_obj
This method will return the underlying Template Toolkit object that is
used behind the scenes. It is usually not necessary to use this object
directly, as you can process templates and configure the Template
object through the tt_process and tt_config methods. Every call to this
method will return the same object during a single request.
It may be useful for debugging purposes.
tt_params
This method will accept a hash or hashref of parameters that will be
included in the processing of every call to tt_process. It is important
to note that the parameters defined using tt_params will be passed to
every template that is processed during a given request cycle. Usually
only one template is processed per request, but it is entirely possible
to call tt_process multiple times with different templates. Every time
tt_process is called, the hashref of parameters passed to tt_process
will be merged with the parameters set using the tt_params method.
Parameters passed through tt_process will have precedence in case of
duplicate parameters.
This can be useful to add global values to your templates, for example
passing the user's name automatically if they are logged in.
sub cgiapp_prerun {
my $self = shift;
$self->tt_params(username => $ENV{REMOTE_USER}) if $ENV{REMOTE_USER};
}
tt_clear_params
This method will clear all the currently stored parameters that have
been set with tt_params.
tt_pre_process
This is an overridable method that works in the spirit of
cgiapp_prerun. The method will be called just before a template is
processed, and will be passed the template filename, and a hashref of
template parameters. It can be used to make last minute changes to the
template, or the parameters before the template is processed.
sub tt_pre_process {
my ($self, $file, $vars) = @_;
$vars->{user} = $ENV{REMOTE_USER};
return;
}
If you are using CGI::Application 4.0 or greater, you can also register
this as a callback.
__PACKAGE__->add_callback('tt_pre_process', sub {
my ($self, $file, $vars) = @_;
$vars->{user} = $ENV{REMOTE_USER};
return;
});
tt_post_process
This, like it's counterpart cgiapp_postrun, is called right after a
template has been processed. It will be passed a scalar reference to
the processed template.
sub tt_post_process {
my ($self, $htmlref) = shift;
require HTML::Clean;
my $h = HTML::Clean->new($htmlref);
$h->strip;
my $newref = $h->data;
$$htmlref = $$newref;
return;
}
If you are using CGI::Application 4.0 or greater, you can also register
this as a callback (See tt_pre_process for an example of how to use
it).
tt_template_name
This method will generate a template name for you based on two pieces
of information: the method name of the caller, and the package name of
the caller. It allows you to consistently name your templates based on
a directory hierarchy and naming scheme defined by the structure of the
code. This can simplify development and lead to more consistent,
readable code.
If you do not want the template to be named after the method that
called tt_template_name, you can pass in an integer, and the method
used to generate the template name will be that many levels above the
caller. It defaults to zero.
For example:
package My::App::Browser
sub dummy_call {
my $self = shift;
return $self->tt_template_name(1); # parent callers name
}
sub view {
my $self = shift;
my $template;
$template = $self->tt_template_name; # returns 'My/App/Browser/view.tmpl'
$template = $self->dummy_call; # also returns 'My/App/Browser/view.tmpl'
return $self->tt_process($template, { var1 => param1 });
}
To simplify things even more, tt_process automatically calls
$self->tt_template_name for you if you do not pass a template name, so
the above can be reduced to this:
package MyApp::Example
sub view {
my $self = shift;
return $self->tt_process({ var1 => param1 }); # process template 'MyApp/Example/view.tmpl'
}
Since the path is generated based on the name of the module, you could
place all of your templates in the same directory as your Perl modules,
and then pass @INC as your INCLUDE_PATH parameter. Whether that is
actually a good idea is left up to the reader.
$self->tt_include_path(\@INC);
tt_include_path
This method will allow you to set the include path for the Template
Toolkit object after the object has already been created. Normally you
set the INCLUDE_PATH option when creating the Template Toolkit object,
but sometimes it can be useful to change this value after the object
has already been created. This method will allow you to do that without
needing to create an entirely new Template Toolkit object. This can be
especially handy when using the Singleton support mentioned below,
where a Template Toolkit object may persist across many request. It is
important to note that a call to tt_include_path will change the
INCLUDE_PATH for all subsequent calls to this object, until
tt_include_path is called again. So if you change the INCLUDE_PATH
based on the user that is connecting to your site, then make sure you
call tt_include_path on every request.
my $root = '/var/www/';
$self->tt_include_path( [$root.$ENV{SERVER_NAME}, $root.'default'] );
When called with no parameters tt_include_path returns an arrayref
containing the current INCLUDE_PATH.
DEFAULT PARAMETERS
By default, the TT plugin will automatically add a parameter 'c' to the
template that will return to your CGI::Application object $self. This
allows you to access any methods in your CGI::Application module that
you could normally call on $self from within your template. This allows
for some powerful actions in your templates. For example, your
templates will be able to access query parameters, or if you use the
CGI::Application::Plugin::Session module, you can access session
parameters.
Hello [% c.session.param('username') || 'Anonymous User' %]
Reload this page
Another useful plugin that can use this feature is the
CGI::Application::Plugin::HTMLPrototype plugin, which gives easy access
to the very powerful prototype.js JavaScript library.
[% c.prototype.define_javascript_functions %]
Extra Info
With this extra flexibility comes some responsibility as well. It could
lead down a dangerous path if you start making alterations to your
object from within the template. For example you could call
c.header_add to add new outgoing headers, but that is something that
should be left in your code, not in your template. Try to limit
yourself to pulling in information into your templates (like the
session example above does).
EXAMPLE
In a CGI::Application module:
package My::App
use CGI::Application::Plugin::TT;
use base qw(CGI::Application);
# configure the template object once during the init stage
sub cgiapp_init {
my $self = shift;
# Configure the template
$self->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => '/path/to/template/files',
POST_CHOMP => 1,
FILTERS => {
'currency' => sub { sprintf('$ %0.2f', @_) },
},
},
);
}
sub cgiapp_prerun {
my $self = shift;
# Add the username to all templates if the user is logged in
$self->tt_params(username => $ENV{REMOTE_USER}) if $ENV{REMOTE_USER};
}
sub tt_pre_process {
my $self = shift;
my $template = shift;
my $params = shift;
# could add the username here instead if we want
$params->{username} = $ENV{REMOTE_USER}) if $ENV{REMOTE_USER};
return;
}
sub tt_post_process {
my $self = shift;
my $htmlref = shift;
# clean up the resulting HTML
require HTML::Clean;
my $h = HTML::Clean->new($htmlref);
$h->strip;
my $newref = $h->data;
$$htmlref = $$newref;
return;
}
sub my_runmode {
my $self = shift;
my %params = (
foo => 'bar',
);
# return the template output
return $self->tt_process('my_runmode.tmpl', \%params);
}
sub my_otherrunmode {
my $self = shift;
my %params = (
foo => 'bar',
);
# Since we don't provide the name of the template to tt_process, it
# will be auto-generated by a call to $self->tt_template_name,
# which will result in a filename of 'Example/my_otherrunmode.tmpl'.
return $self->tt_process(\%params);
}
SINGLETON SUPPORT
Creating a Template Toolkit object can be an expensive operation if it
needs to be done for every request. This startup cost increases
dramatically as the number of templates you use increases. The reason
for this is that when TT loads and parses a template, it generates
actual Perl code to do the rendering of that template. This means that
the rendering of the template is extremely fast, but the initial
parsing of the templates can be inefficient. Even by using the built-in
caching mechanism that TT provides only writes the generated Perl code
to the filesystem. The next time a TT object is created, it will need
to load these templates from disk, and eval the source code that they
contain.
So to improve the efficiency of Template Toolkit, we should keep the
object (and hence all the compiled templates) in memory across multiple
requests. This means you only get hit with the startup cost the first
time the TT object is created.
All you need to do to use this module as a singleton is to call
tt_config as a class method instead of as an object method. All the
same parameters can be used when calling tt_config as a class method.
When creating the singleton, the Template Toolkit object will be saved
in the namespace of the module that created it. The singleton will also
be inherited by any subclasses of this module. So in effect this is not
a traditional Singleton, since an instance of a Template Toolkit object
is only shared by a module and it's children. This allows you to still
have different configurations for different CGI::Application modules if
you require it. If you want all of your CGI::Application applications
to share the same Template Toolkit object, just create a Base class
that calls tt_config to configure the plugin, and have all of your
applications inherit from this Base class.
SINGLETON EXAMPLE
package My::App;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
My::App->tt_config(
TEMPLATE_OPTIONS => {
POST_CHOMP => 1,
},
);
sub cgiapp_prerun {
my $self = shift;
# Set the INCLUDE_PATH (will change the INCLUDE_PATH for
# all subsequent requests as well, until tt_include_path is called
# again)
my $basedir = '/path/to/template/files/',
$self->tt_include_path( [$basedir.$ENV{SERVER_NAME}, $basedir.'default'] );
}
sub my_runmode {
my $self = shift;
# Will use the same TT object across multiple request
return $self->tt_process({ param1 => 'value1' });
}
package My::App::Subclass;
use base qw(My::App);
sub my_other_runmode {
my $self = shift;
# Uses the TT object from the parent class (My::App)
return $self->tt_process({ param2 => 'value2' });
}
BUGS
Please report any bugs or feature requests to
bug-cgi-application-plugin-tt@rt.cpan.org, or through the web interface
at http://rt.cpan.org. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
CONTRIBUTING
Patches, questions and feedback are welcome.
SEE ALSO
CGI::Application, Template, perl(1)
Changes 100644 001750 001750 7050 14721400126 16216 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 Revision history for Perl extension CGI::Application::Plugin::TT.
1.06 2024-11-26 11:07:32-06:00 America/Chicago
- Fix minor documentation typos (Wes Malone, RT#60921)
1.05 Fri Jun 4 14:25:49 EST 2010
- fix dev popup support by html encoding the data sent
to the popup window (patch by Clayton L. Scott)
- fix test failure on windows (patch by Alexandr Ciornii)
1.04 Wed Nov 1 07:08:50 EST 2006
- add TEMPLATE_PRECOMPILE_DIR option which can
automatically compile all your templates on startup
(patch by Michael Peters)
- slightly refactored the default tt_template_name code
- doc fix (Trammell Hudson/Robert Sedlacek)
1.03 Thu May 18 12:27:26 EDT 2006
- the default tt_template_name method now accepts
a parameter that specifies how many caller levels
we walk up (from the calling method) to find the
method name to use as a base for the template name
(defaults to 0)
- a side effect of this change is that you can now
pass any parameters you like to your custom
TEMPLATE_NAME_GENERATOR method, when calling
$self->tt_template_name(...).
1.02 Sun Feb 5 20:11:23 EST 2006
- Allow call to tt_process with no parameters
(brad -at- footle.org)
1.01 Wed Jan 25 16:00:38 EST 2006
- Fix doc error in synopsis (Jonathan Anderson)
- Before calling 'call_hook' make sure it exists
- Update pod coverage tests
1.00 Wed Oct 19 14:11:22 EDT 2005
- added support for tt_include_path to return the
current value of INCLUDE_PATH
0.10 Fri Sep 23 08:58:34 EDT 2005
- fix tests for DevPopup so it doesn't fail if it is not
installed (Thanks to Jason Purdy and Rhesa Rozendaal)
0.09 Wed Sep 21 15:59:03 EDT 2005
- added support for the load_tmpl hook in CGI::App
- added support for the DevPopup plugin
- added pod coverage tests
0.08 Sun Jul 31 17:38:16 EDT 2005
- Made some small doc changes that I meant to put in the
last release.
0.07 Sat Jul 30 9:18:46 EDT 2005
- fixed Windows path bug in test suite (Emanuele Zeppieri)
- Simplify the pod tests according to Test::Pod docs
- Support the new callback hooks in CGI::Application 4.0
- Automatically add { c => $self } to template params
(see docs under DEFAULT PARAMETERS)
- minor doc cleanups
0.06 Thu Feb 3 15:38:39 EST 2005
- Document use of tt_config as a class method for singleton
support
- Some other small documentation cleanups
0.05 Mon Jan 24 11:47:06 EST 2005
- add tt_template_name which autogenerates template filenames
- tt_process will call tt_template_name if the template
name is not provided as an arguement
- add Singleton support for TT object
0.04 Fri Dec 3 12:02:56 EST 2004
- die if there is an error processing a template in tt_process
0.03 Sun Sep 19 18:13:03 EST 2004
- scrap CGI::Application::Plugin support for simple Exporter
system.
- Moved module to the CGI::Application::Plugin namespace.
- module no longer depends on inheritance, so just use'ing
the module will suffice to import the required methods
into the current class.
0.02 Mon Jul 26 23:44:39 EST 2004
- add support for the new CGI::Application::Plugin base class.
This means the usage has changed. Altering the inheritance
tree is no longer necesary, as you only need to use the module
and it will import the plugin methods into the callers
namespace automatically. See the docs for more details...
0.01 Sun Feb 15 16:10:39 EST 2004
- original version
LICENSE 100644 001750 001750 46413 14721400126 15756 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 This software is copyright (c) 2024 by Cees Hek.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
Terms of the Perl programming language system itself
a) the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any
later version, or
b) the "Artistic License"
--- The GNU General Public License, Version 1, February 1989 ---
This software is Copyright (c) 2024 by Cees Hek.
This is free software, licensed under:
The GNU General Public License, Version 1, February 1989
GNU GENERAL PUBLIC LICENSE
Version 1, February 1989
Copyright (C) 1989 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The license agreements of most software companies try to keep users
at the mercy of those companies. By contrast, our General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. The
General Public License applies to the Free Software Foundation's
software and to any other program whose authors commit to using it.
You can use it for your programs, too.
When we speak of free software, we are referring to freedom, not
price. Specifically, the General Public License is designed to make
sure that you have the freedom to give away or sell copies of free
software, that you receive source code or can get it if you want it,
that you can change the software or use pieces of it in new free
programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of a such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must tell them their rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any program or other work which
contains a notice placed by the copyright holder saying it may be
distributed under the terms of this General Public License. The
"Program", below, refers to any such program or work, and a "work based
on the Program" means either the Program or any work containing the
Program or a portion of it, either verbatim or with modifications. Each
licensee is addressed as "you".
1. You may copy and distribute verbatim copies of the Program's source
code as you receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice and
disclaimer of warranty; keep intact all the notices that refer to this
General Public License and to the absence of any warranty; and give any
other recipients of the Program a copy of this General Public License
along with the Program. You may charge a fee for the physical act of
transferring a copy.
2. You may modify your copy or copies of the Program or any portion of
it, and copy and distribute such modifications under the terms of Paragraph
1 above, provided that you also do the following:
a) cause the modified files to carry prominent notices stating that
you changed the files and the date of any change; and
b) cause the whole of any work that you distribute or publish, that
in whole or in part contains the Program or any part thereof, either
with or without modifications, to be licensed at no charge to all
third parties under the terms of this General Public License (except
that you may choose to grant warranty protection to some or all
third parties, at your option).
c) If the modified program normally reads commands interactively when
run, you must cause it, when started running for such interactive use
in the simplest and most usual way, to print or display an
announcement including an appropriate copyright notice and a notice
that there is no warranty (or else, saying that you provide a
warranty) and that users may redistribute the program under these
conditions, and telling the user how to view a copy of this General
Public License.
d) You may charge a fee for the physical act of transferring a
copy, and you may at your option offer warranty protection in
exchange for a fee.
Mere aggregation of another independent work with the Program (or its
derivative) on a volume of a storage or distribution medium does not bring
the other work under the scope of these terms.
3. You may copy and distribute the Program (or a portion or derivative of
it, under Paragraph 2) in object code or executable form under the terms of
Paragraphs 1 and 2 above provided that you also do one of the following:
a) accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of
Paragraphs 1 and 2 above; or,
b) accompany it with a written offer, valid for at least three
years, to give any third party free (except for a nominal charge
for the cost of distribution) a complete machine-readable copy of the
corresponding source code, to be distributed under the terms of
Paragraphs 1 and 2 above; or,
c) accompany it with the information you received as to where the
corresponding source code may be obtained. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form alone.)
Source code for a work means the preferred form of the work for making
modifications to it. For an executable file, complete source code means
all the source code for all modules it contains; but, as a special
exception, it need not include source code for modules which are standard
libraries that accompany the operating system on which the executable
file runs, or for standard header files or definitions files that
accompany that operating system.
4. You may not copy, modify, sublicense, distribute or transfer the
Program except as expressly provided under this General Public License.
Any attempt otherwise to copy, modify, sublicense, distribute or transfer
the Program is void, and will automatically terminate your rights to use
the Program under this License. However, parties who have received
copies, or rights to use copies, from you under this General Public
License will not have their licenses terminated so long as such parties
remain in full compliance.
5. By copying, distributing or modifying the Program (or any work based
on the Program) you indicate your acceptance of this license to do so,
and all its terms and conditions.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the original
licensor to copy, distribute or modify the Program subject to these
terms and conditions. You may not impose any further restrictions on the
recipients' exercise of the rights granted herein.
7. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of the license which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
the license, you may choose any version ever published by the Free Software
Foundation.
8. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to humanity, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively convey
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
Copyright (C) 19yy
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19xx name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the
appropriate parts of the General Public License. Of course, the
commands you use may be called something other than `show w' and `show
c'; they could even be mouse-clicks or menu items--whatever suits your
program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
program `Gnomovision' (a program to direct compilers to make passes
at assemblers) written by James Hacker.
, 1 April 1989
Ty Coon, President of Vice
That's all there is to it!
--- The Perl Artistic License 1.0 ---
This software is Copyright (c) 2024 by Cees Hek.
This is free software, licensed under:
The Perl Artistic License 1.0
The "Artistic License"
Preamble
The intent of this document is to state the conditions under which a
Package may be copied, such that the Copyright Holder maintains some
semblance of artistic control over the development of the package,
while giving the users of the package the right to use and distribute
the Package in a more-or-less customary fashion, plus the right to make
reasonable modifications.
Definitions:
"Package" refers to the collection of files distributed by the
Copyright Holder, and derivatives of that collection of files
created through textual modification.
"Standard Version" refers to such a Package if it has not been
modified, or has been modified in accordance with the wishes
of the Copyright Holder as specified below.
"Copyright Holder" is whoever is named in the copyright or
copyrights for the package.
"You" is you, if you're thinking about copying or distributing
this Package.
"Reasonable copying fee" is whatever you can justify on the
basis of media cost, duplication charges, time of people involved,
and so on. (You will not be required to justify it to the
Copyright Holder, but only to the computing community at large
as a market that must bear the fee.)
"Freely Available" means that no fee is charged for the item
itself, though there may be fees involved in handling the item.
It also means that recipients of the item may redistribute it
under the same conditions they received it.
1. You may make and give away verbatim copies of the source form of the
Standard Version of this Package without restriction, provided that you
duplicate all of the original copyright notices and associated disclaimers.
2. You may apply bug fixes, portability fixes and other modifications
derived from the Public Domain or from the Copyright Holder. A Package
modified in such a way shall still be considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided
that you insert a prominent notice in each changed file stating how and
when you changed that file, and provided that you do at least ONE of the
following:
a) place your modifications in the Public Domain or otherwise make them
Freely Available, such as by posting said modifications to Usenet or
an equivalent medium, or placing the modifications on a major archive
site such as uunet.uu.net, or by allowing the Copyright Holder to include
your modifications in the Standard Version of the Package.
b) use the modified Package only within your corporation or organization.
c) rename any non-standard executables so the names do not conflict
with standard executables, which must also be provided, and provide
a separate manual page for each non-standard executable that clearly
documents how it differs from the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
4. You may distribute the programs of this Package in object code or
executable form, provided that you do at least ONE of the following:
a) distribute a Standard Version of the executables and library files,
together with instructions (in the manual page or equivalent) on where
to get the Standard Version.
b) accompany the distribution with the machine-readable source of
the Package with your modifications.
c) give non-standard executables non-standard names, and clearly
document the differences in manual pages (or equivalent), together
with instructions on where to get the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
5. You may charge a reasonable copying fee for any distribution of this
Package. You may charge any fee you choose for support of this
Package. You may not charge a fee for this Package itself. However,
you may distribute this Package in aggregate with other (possibly
commercial) programs as part of a larger (possibly commercial) software
distribution provided that you do not advertise this Package as a
product of your own. You may embed this Package's interpreter within
an executable of yours (by linking); this shall be construed as a mere
form of aggregation, provided that the complete Standard Version of the
interpreter is so embedded.
6. The scripts and library files supplied as input to or produced as
output from the programs of this Package do not automatically fall
under the copyright of this Package, but belong to whoever generated
them, and may be sold commercially, and may be aggregated with this
Package. If such scripts or library files are aggregated with this
Package via the so-called "undump" or "unexec" methods of producing a
binary executable image, then distribution of such an image shall
neither be construed as a distribution of this Package nor shall it
fall under the restrictions of Paragraphs 3 and 4, provided that you do
not represent such an executable image as a Standard Version of this
Package.
7. C subroutines (or comparably compiled subroutines in other
languages) supplied by you and linked into this Package in order to
emulate subroutines and variables of the language defined by this
Package shall not be considered part of this Package, but are the
equivalent of input as in Paragraph 6, provided these subroutines do
not change the language in any way that would cause it to fail the
regression tests for the language.
8. Aggregation of this Package with a commercial distribution is always
permitted provided that the use of this Package is embedded; that is,
when no overt attempt is made to make this Package's interfaces visible
to the end user of the commercial distribution. Such use shall not be
construed as a distribution of this Package.
9. The name of the Copyright Holder may not be used to endorse or promote
products derived from this software without specific prior written permission.
10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
The End
cpanfile 100644 001750 001750 366 14721400126 16412 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 requires 'CGI::Application' => 4.0;
requires 'Class::ISA';
requires 'File::Spec';
requires 'Scalar::Util';
requires 'Template' => 2.0;
on test => sub {
requires 'Test::More' => 1.001002;
recommends 'CGI::Application::Plugin::DevPopup';
};
dist.ini 100644 001750 001750 513 14721400126 16344 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 name = CGI-Application-Plugin-TT
author = Cees Hek
license = Perl_5
copyright_holder = Cees Hek
version = 1.06
[NextRelease]
[@Git]
[@Basic]
[GithubMeta]
[MetaJSON]
[PodWeaver]
[PkgVersion]
[ReadmeFromPod]
[PodSyntaxTests]
[PodCoverageTests]
[Prereqs::FromCPANfile]
META.yml 100644 001750 001750 1524 14721400126 16174 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 ---
abstract: 'Plugin that adds Template Toolkit support to CGI::Application'
author:
- 'Cees Hek '
build_requires:
Test::More: '1.001002'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.031, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: CGI-Application-Plugin-TT
requires:
CGI::Application: '4'
Class::ISA: '0'
File::Spec: '0'
Scalar::Util: '0'
Template: '2'
resources:
homepage: https://github.com/cees/cgi-application-plugin-tt
repository: https://github.com/cees/cgi-application-plugin-tt.git
version: '1.06'
x_generated_by_perl: v5.38.2
x_serialization_backend: 'YAML::Tiny version 1.74'
x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later'
MANIFEST 100644 001750 001750 1461 14721400126 16054 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 # This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.031.
Changes
LICENSE
MANIFEST
META.json
META.yml
Makefile.PL
README
cpanfile
dist.ini
lib/CGI/Application/Plugin/TT.pm
t/01_basic.t
t/02_error.t
t/03_tname.t
t/04_singleton.t
t/05_include_path.t
t/06_callback.t
t/07_devpopup.t
t/08_load_tmpl.t
t/09_precompile_dir.t
t/TestAppBase.pm
t/TestAppBasic.pm
t/TestAppCallback.pm
t/TestAppDevPopup.pm
t/TestAppError.pm
t/TestAppIncludePath.pm
t/TestAppLoadtmpl.pm
t/TestAppPrecompile.pm
t/TestAppSingleton.pm
t/TestAppTName.pm
t/TestAppTName/NoNameNoVars/test_mode.tmpl
t/TestAppTName/UpLevel/test_mode.tmpl
t/TestAppTName/test.tmpl
t/TestAppTName/test_mode.tmpl
t/author-pod-coverage.t
t/author-pod-syntax.t
t/include1/TestAppIncludePath/test_mode.tmpl
t/include2/TestAppIncludePath/test_mode.tmpl
META.json 100644 001750 001750 3251 14721400126 16343 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 {
"abstract" : "Plugin that adds Template Toolkit support to CGI::Application",
"author" : [
"Cees Hek "
],
"dynamic_config" : 0,
"generated_by" : "Dist::Zilla version 6.031, CPAN::Meta::Converter version 2.150010",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : 2
},
"name" : "CGI-Application-Plugin-TT",
"prereqs" : {
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"develop" : {
"requires" : {
"Pod::Coverage::TrustPod" : "0",
"Test::Pod" : "1.41",
"Test::Pod::Coverage" : "1.08"
}
},
"runtime" : {
"requires" : {
"CGI::Application" : "4",
"Class::ISA" : "0",
"File::Spec" : "0",
"Scalar::Util" : "0",
"Template" : "2"
}
},
"test" : {
"recommends" : {
"CGI::Application::Plugin::DevPopup" : "0"
},
"requires" : {
"Test::More" : "1.001002"
}
}
},
"release_status" : "stable",
"resources" : {
"homepage" : "https://github.com/cees/cgi-application-plugin-tt",
"repository" : {
"type" : "git",
"url" : "https://github.com/cees/cgi-application-plugin-tt.git",
"web" : "https://github.com/cees/cgi-application-plugin-tt"
}
},
"version" : "1.06",
"x_generated_by_perl" : "v5.38.2",
"x_serialization_backend" : "Cpanel::JSON::XS version 4.37",
"x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later"
}
Makefile.PL 100644 001750 001750 2345 14721400126 16677 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.031.
use strict;
use warnings;
use ExtUtils::MakeMaker;
my %WriteMakefileArgs = (
"ABSTRACT" => "Plugin that adds Template Toolkit support to CGI::Application",
"AUTHOR" => "Cees Hek ",
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => 0
},
"DISTNAME" => "CGI-Application-Plugin-TT",
"LICENSE" => "perl",
"NAME" => "CGI::Application::Plugin::TT",
"PREREQ_PM" => {
"CGI::Application" => 4,
"Class::ISA" => 0,
"File::Spec" => 0,
"Scalar::Util" => 0,
"Template" => 2
},
"TEST_REQUIRES" => {
"Test::More" => "1.001002"
},
"VERSION" => "1.06",
"test" => {
"TESTS" => "t/*.t"
}
);
my %FallbackPrereqs = (
"CGI::Application" => 4,
"Class::ISA" => 0,
"File::Spec" => 0,
"Scalar::Util" => 0,
"Template" => 2,
"Test::More" => "1.001002"
);
unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) {
delete $WriteMakefileArgs{TEST_REQUIRES};
delete $WriteMakefileArgs{BUILD_REQUIRES};
$WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs;
}
delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
unless eval { ExtUtils::MakeMaker->VERSION(6.52) };
WriteMakefile(%WriteMakefileArgs);
t 000755 001750 001750 0 14721400126 15024 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06 01_basic.t 100644 001750 001750 1113 14721400126 16726 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More tests => 6;
BEGIN { require_ok('CGI::Application::Plugin::TT') };
use lib './t';
use strict;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use CGI;
use TestAppBasic;
my $t1_obj = TestAppBasic->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/template param\./, 'template parameter');
like($t1_output, qr/template param hash\./, 'template parameter hash');
like($t1_output, qr/template param hashref\./, 'template parameter hashref');
like($t1_output, qr/pre_process param\./, 'pre process parameter');
like($t1_output, qr/post_process param\./, 'post process parameter');
03_tname.t 100644 001750 001750 4550 14721400126 16763 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More tests => 27;
use lib './t';
use strict;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use TestAppTName;
my $t1_obj = TestAppTName->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/file: test_mode\.tmpl/, 'correct template file');
like($t1_output, qr/:template param:/, 'template parameter');
like($t1_output, qr/:template param hash:/, 'template parameter hash');
like($t1_output, qr/:template param hashref:/, 'template parameter hashref');
like($t1_output, qr/:pre_process param:/, 'pre process parameter');
like($t1_output, qr/:post_process param:/, 'post process parameter');
like($t1_output, qr/:TestAppTName[\/\\]test_mode\.tmpl:/, 'template name ok');
my $t2_obj = TestAppTName::CustName->new();
my $t2_output = $t2_obj->run();
like($t2_output, qr/file: test\.tmpl/, 'correct template file');
like($t2_output, qr/:template param:/, 'template parameter');
like($t2_output, qr/:template param hash:/, 'template parameter hash');
like($t2_output, qr/:template param hashref:/, 'template parameter hashref');
like($t2_output, qr/:pre_process param:/, 'pre process parameter');
like($t2_output, qr/:post_process param:/, 'post process parameter');
like($t2_output, qr/:TestAppTName[\/\\]test\.tmpl:/, 'template name ok');
my $t3_obj = TestAppTName::NoVars->new();
my $t3_output = $t3_obj->run();
like($t3_output, qr/file: test_mode\.tmpl/, 'correct template file');
like($t3_output, qr/:pre_process param:/, 'pre process parameter');
like($t3_output, qr/:post_process param:/, 'post process parameter');
my $t4_obj = TestAppTName::NoNameNoVars->new();
my $t4_output = $t4_obj->run();
like($t4_output, qr/file: test_mode\.tmpl/, 'correct template file');
like($t4_output, qr/:pre_process param:/, 'pre process parameter');
like($t4_output, qr/:post_process param:/, 'post process parameter');
my $t5_obj = TestAppTName::UpLevel->new();
my $t5_output = $t5_obj->run();
like($t5_output, qr/file: test_mode\.tmpl/, 'correct template file');
like($t5_output, qr/:template param:/, 'template parameter');
like($t5_output, qr/:template param hash:/, 'template parameter hash');
like($t5_output, qr/:template param hashref:/, 'template parameter hashref');
like($t5_output, qr/:pre_process param:/, 'pre process parameter');
like($t5_output, qr/:post_process param:/, 'post process parameter');
like($t5_output, qr/:TestAppTName[\/\\]UpLevel[\/\\]test_mode\.tmpl:/, 'template name ok');
02_error.t 100644 001750 001750 364 14721400126 16766 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More tests => 1;
use lib './t';
use strict;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use CGI;
use TestAppError;
my $t1_obj = TestAppError->new();
my $t1_output = eval { $t1_obj->run() };
like($@, qr/parse error/, 'template parse error');
07_devpopup.t 100644 001750 001750 1236 14721400126 17523 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More;
use lib './t';
use strict;
BEGIN {
$ENV{CAP_DEVPOPUP_EXEC} = 1;
eval {
require CGI::Application::Plugin::DevPopup;
};
if ($@) {
plan skip_all => "CGI::Application::Plugin::DevPopup required for these tests";
exit;
}
}
plan tests => 3;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use CGI;
use TestAppDevPopup;
my $t1_obj = TestAppDevPopup->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/template param\./, 'template parameter');
like($t1_output, qr/<div class="test"><\/div>/, 'HTML tags were encoded as entities');
like($t1_output, qr/TT params for/, 'popup title found');
06_callback.t 100644 001750 001750 1033 14721400126 17407 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More tests => 5;
use lib './t';
use strict;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use CGI;
use TestAppCallback;
my $t1_obj = TestAppCallback->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/template param\./, 'template parameter');
like($t1_output, qr/template param hash\./, 'template parameter hash');
like($t1_output, qr/template param hashref\./, 'template parameter hashref');
like($t1_output, qr/pre_process param\./, 'pre process parameter');
like($t1_output, qr/post_process param\./, 'post process parameter');
04_singleton.t 100644 001750 001750 1520 14721400126 17654 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More;
use lib './t';
use strict;
eval {
require Class::ISA;
Class::ISA->import;
};
if ($@) {
plan skip_all => "Class::ISA required for Singleton support";
exit;
}
plan tests => 6;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use TestAppSingleton;
my $t1_obj = TestAppSingleton->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/template param\./, 'template parameter');
like($t1_output, qr/template param hash\./, 'template parameter hash');
like($t1_output, qr/template param hashref\./, 'template parameter hashref');
like($t1_output, qr/pre_process param\./, 'pre process parameter');
like($t1_output, qr/post_process param\./, 'post process parameter');
# make sure the CGI::Application instance is destroyed, and then check for TT object
undef $t1_obj;
ok(ref($TestAppSingleton::__TT_OBJECT), 'singleton still exists');
TestAppBase.pm 100644 001750 001750 734 14721400126 17661 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppBase;
use strict;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
sub cgiapp_init {
my $self = shift;
$self->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => 't',
POST_CHOMP => 1,
DEBUG => 1,
},
);
}
sub setup {
my $self = shift;
$self->start_mode('test_mode');
$self->run_modes(test_mode => 'test_mode' );
}
1;
08_load_tmpl.t 100644 001750 001750 473 14721400126 17617 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More tests => 2;
use lib './t';
use strict;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use CGI;
use TestAppLoadtmpl;
my $t1_obj = TestAppLoadtmpl->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/template param\./, 'template parameter');
like($t1_output, qr/load_tmpl param\./, 'load_tmpl parameter');
TestAppBasic.pm 100644 001750 001750 1516 14721400126 20047 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppBasic;
use strict;
use base qw(TestAppBase);
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash.');
$self->tt_params({template_param_hashref => 'template param hashref.'});
my $tt_vars = {
template_var => 'template param.'
};
return $self->tt_process(\*DATA, $tt_vars);
}
sub tt_pre_process {
my $self = shift;
my $file = shift;
my $vars = shift;
$vars->{pre_process_var} = 'pre_process param.';
}
sub tt_post_process {
my $self = shift;
my $htmlref = shift;
$$htmlref =~ s/post_process_var/post_process param./;
}
1;
# The test template file is below in the DATA segment
__DATA__
[% template_var %]
[% template_param_hash %]
[% template_param_hashref %]
[% pre_process_var %]
post_process_var
TestAppTName.pm 100644 001750 001750 4702 14721400126 20032 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppTName;
use strict;
use base qw(TestAppBase);
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash');
$self->tt_params({template_param_hashref => 'template param hashref'});
my $tt_vars = {
template_var => 'template param',
template_name => $self->tt_template_name,
};
return $self->tt_process($tt_vars);
}
sub tt_pre_process {
my $self = shift;
my $file = shift;
my $vars = shift;
$vars->{pre_process_var} = 'pre_process param';
}
sub tt_post_process {
my $self = shift;
my $htmlref = shift;
$$htmlref =~ s/post_process_var/post_process param/;
}
package TestAppTName::CustName;
use strict;
use TestAppTName;
@TestAppTName::CustName::ISA = qw(TestAppTName);
sub cgiapp_init {
my $self = shift;
$self->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => 't',
POST_CHOMP => 1,
DEBUG => 1,
},
TEMPLATE_NAME_GENERATOR => sub { return 'TestAppTName/test.tmpl' },
);
}
package TestAppTName::NoVars;
use strict;
@TestAppTName::NoVars::ISA = qw(TestAppTName);
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash');
$self->tt_params({template_param_hashref => 'template param hashref'});
return $self->tt_process('TestAppTName/test_mode.tmpl');
}
package TestAppTName::NoNameNoVars;
use strict;
@TestAppTName::NoNameNoVars::ISA = qw(TestAppTName);
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash');
$self->tt_params({template_param_hashref => 'template param hashref'});
return $self->tt_process;
}
package TestAppTName::UpLevel;
use strict;
@TestAppTName::UpLevel::ISA = qw(TestAppTName);
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash');
$self->tt_params({template_param_hashref => 'template param hashref'});
my $tt_vars = {
template_var => 'template param',
template_name => $self->call_tt_template_name,
};
return $self->call_tt_process($tt_vars);
}
sub call_tt_process {
my $self = shift;
return $self->tt_process($self->tt_template_name(1), @_);
}
sub call_tt_template_name {
my $self = shift;
return $self->tt_template_name(1);
}
1;
TestAppError.pm 100644 001750 001750 557 14721400126 20103 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppError;
use strict;
use base qw(TestAppBase);
sub test_mode {
my $self = shift;
my $tt_vars = {
unclosed_if => 'unclosed if'
};
return $self->tt_process(\*DATA, $tt_vars);
}
1;
# The test template file is below in the DATA segment
__DATA__
[% IF unclosed_if %]
[% unclosed_if %]
testing invalid template
05_include_path.t 100644 001750 001750 1512 14721400126 20313 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use Test::More;
use lib './t';
use strict;
eval {
require Class::ISA;
Class::ISA->import;
};
if ($@) {
plan skip_all => "Class::ISA required for Singleton support";
exit;
}
plan tests => 5;
$ENV{CGI_APP_RETURN_ONLY} = 1;
use TestAppIncludePath;
$ENV{TT_INCLUDE_PATH} = 't/include1';
my $t1_obj = TestAppIncludePath->new();
my $t1_output = $t1_obj->run();
like($t1_output, qr/include path: t\/include1/, 'include path');
like($t1_output, qr/template dir: include1/, 'template dir');
$ENV{TT_INCLUDE_PATH} = 't/include2';
my $t2_obj = TestAppIncludePath->new();
my $t2_output = $t2_obj->run();
like($t2_output, qr/include path: t\/include2/, 'include path second time');
like($t2_output, qr/template dir: include2/, 'template dir second time');
is_deeply($t1_obj->tt_include_path, [qw[t/include2]],'returns current paths');
TestAppCallback.pm 100644 001750 001750 1677 14721400126 20532 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppCallback;
use strict;
use base qw(TestAppBase);
__PACKAGE__->add_callback('tt_pre_process',
sub {
my $self = shift;
my $file = shift;
my $vars = shift;
$vars->{pre_process_var} = 'pre_process param.';
}
);
__PACKAGE__->add_callback('tt_post_process',
sub {
my $self = shift;
my $htmlref = shift;
$$htmlref =~ s/post_process_var/post_process param./;
}
);
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash.');
$self->tt_params({template_param_hashref => 'template param hashref.'});
my $tt_vars = {
template_var => 'template param.'
};
return $self->tt_process(\*DATA, $tt_vars);
}
1;
# The test template file is below in the DATA segment
__DATA__
[% template_var %]
[% template_param_hash %]
[% template_param_hashref %]
[% pre_process_var %]
post_process_var
TestAppDevPopup.pm 100644 001750 001750 652 14721400126 20550 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppDevPopup;
use strict;
use base qw(TestAppBase);
use CGI::Application::Plugin::DevPopup;
sub test_mode {
my $self = shift;
my $tt_vars = {
template_var => 'template param.',
html_var => ''
};
return $self->tt_process(\*DATA, $tt_vars);
}
1;
# The test template file is below in the DATA segment
__DATA__
[% template_var %]
TestAppLoadtmpl.pm 100644 001750 001750 1102 14721400126 20571 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppLoadtmpl;
use strict;
use base qw(TestAppBase);
__PACKAGE__->add_callback('load_tmpl',
sub {
my $self = shift;
my $options = shift;
my $vars = shift;
my $file = shift;
$vars->{load_tmpl_var} = 'load_tmpl param.';
}
);
sub test_mode {
my $self = shift;
my $tt_vars = {
template_var => 'template param.'
};
return $self->tt_process(\*DATA, $tt_vars);
}
1;
# The test template file is below in the DATA segment
__DATA__
[% template_var %]
[% load_tmpl_var %]
TestAppSingleton.pm 100644 001750 001750 2305 14721400126 20765 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppSingleton;
use strict;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
TestAppSingleton->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => 't',
POST_CHOMP => 1,
DEBUG => 1,
},
);
sub setup {
my $self = shift;
$self->start_mode('test_mode');
$self->run_modes(test_mode => 'test_mode' );
}
sub test_mode {
my $self = shift;
$self->tt_params(template_param_hash => 'template param hash.');
$self->tt_params({template_param_hashref => 'template param hashref.'});
my $tt_vars = {
template_var => 'template param.'
};
return $self->tt_process(\*DATA, $tt_vars);
}
sub tt_pre_process {
my $self = shift;
my $file = shift;
my $vars = shift;
$vars->{pre_process_var} = 'pre_process param.';
}
sub tt_post_process {
my $self = shift;
my $htmlref = shift;
$$htmlref =~ s/post_process_var/post_process param./;
}
1;
# The test template file is below in the DATA segment
__DATA__
[% template_var %]
[% template_param_hash %]
[% template_param_hashref %]
[% pre_process_var %]
post_process_var
09_precompile_dir.t 100644 001750 001750 3075 14721400126 20663 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t use strict;
use lib './t';
use Test::More tests => 6;
use TestAppPrecompile;
use File::Spec::Functions qw(catdir catfile rel2abs);
# get a temp directory that we can later look into
# to find the pre-compiled templates
my $tt_dir = catdir('t', 'include1', 'TestAppIncludePath');
my $file = rel2abs(catfile($tt_dir, 'test_mode.tmpl'));
test_success('tmpl');
test_success(qr/\.(tt|tmpl|html)$/);
test_success(sub { rel2abs($_[0]) eq $file });
test_failure('nottmpl');
test_failure(qr/\.(nottmpl)$/);
test_failure(sub { rel2abs($_[0]) eq 'blahblah' });
sub test_success {
my $cgiapp = TestAppPrecompile->new(PARAMS => {
TEMPLATE_PRECOMPILE_FILETEST => shift,
TT_DIR => $tt_dir,
});
my $tt = $cgiapp->tt_obj;
# make sure we have this internally cached in our TT obj
# This is kinda dirty since we are peeking pretty far into TT's internals
# but it doesn't expose this stuff externally
is(
rel2abs($tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->{HEAD}->[1]),
$file,
'file is cached'
);
}
sub test_failure {
my $cgiapp = TestAppPrecompile->new(PARAMS => {
TEMPLATE_PRECOMPILE_FILETEST => shift,
TT_DIR => $tt_dir,
});
my $tt = $cgiapp->tt_obj;
# make sure we have this internally cached in our TT obj
# This is kinda dirty since we are peeking pretty far into TT's internals
# but it doesn't expose this stuff externally
is(
$tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->{HEAD}->[1],
undef,
'file is not cached'
);
}
author-pod-syntax.t 100644 001750 001750 454 14721400126 20742 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t #!perl
BEGIN {
unless ($ENV{AUTHOR_TESTING}) {
print qq{1..0 # SKIP these tests are for testing by the author\n};
exit
}
}
# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests.
use strict; use warnings;
use Test::More;
use Test::Pod 1.41;
all_pod_files_ok();
TestAppPrecompile.pm 100644 001750 001750 716 14721400126 21106 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppPrecompile;
use strict;
use base qw(TestAppBase);
sub cgiapp_init {
my $self = shift;
$self->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => $self->param('TT_DIR'),
ABSOLUTE => 1,
},
TEMPLATE_PRECOMPILE_DIR => $self->param('TT_DIR'),
TEMPLATE_PRECOMPILE_FILETEST => $self->param('TEMPLATE_PRECOMPILE_FILETEST'),
);
}
1;
TestAppIncludePath.pm 100644 001750 001750 1041 14721400126 21217 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t package TestAppIncludePath;
use strict;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT (
TEMPLATE_OPTIONS => {
POST_CHOMP => 1,
DEBUG => 1,
},
);
sub setup {
my $self = shift;
$self->start_mode('test_mode');
$self->run_modes(test_mode => 'test_mode' );
}
sub test_mode {
my $self = shift;
my $path = $ENV{TT_INCLUDE_PATH};
$self->tt_include_path([$path]);
return $self->tt_process({ include_path => $path });
}
1;
author-pod-coverage.t 100644 001750 001750 567 14721400126 21214 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t #!perl
BEGIN {
unless ($ENV{AUTHOR_TESTING}) {
print qq{1..0 # SKIP these tests are for testing by the author\n};
exit
}
}
# This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests.
use strict;
use warnings;
use Test::Pod::Coverage 1.08;
use Pod::Coverage::TrustPod;
all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' });
TestAppTName 000755 001750 001750 0 14721400126 17331 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t test.tmpl 100644 001750 001750 241 14721400126 21323 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/TestAppTName file: test.tmpl
:[% template_var %]:
:[% template_param_hash %]:
:[% template_param_hashref %]:
:[% pre_process_var %]:
:post_process_var:
:[% template_name %]:
test_mode.tmpl 100644 001750 001750 246 14721400126 22334 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/TestAppTName file: test_mode.tmpl
:[% template_var %]:
:[% template_param_hash %]:
:[% template_param_hashref %]:
:[% pre_process_var %]:
:post_process_var:
:[% template_name %]:
Plugin 000755 001750 001750 0 14721400126 21432 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/lib/CGI/Application TT.pm 100644 001750 001750 74654 14721400126 22517 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/lib/CGI/Application/Plugin package CGI::Application::Plugin::TT;
$CGI::Application::Plugin::TT::VERSION = '1.06';
# ABSTRACT: Plugin that adds Template Toolkit support to CGI::Application
use Template 2.0;
use CGI::Application 4.0;
use Carp;
use File::Spec ();
use Scalar::Util ();
use strict;
use vars qw($VERSION @EXPORT);
require Exporter;
@EXPORT = qw(
tt_obj
tt_config
tt_params
tt_clear_params
tt_process
tt_include_path
tt_template_name
);
sub import {
my $pkg = shift;
my $callpkg = caller;
no strict 'refs';
foreach my $sym (@EXPORT) {
*{"${callpkg}::$sym"} = \&{$sym};
}
$callpkg->tt_config(@_) if @_;
if ($callpkg->isa('CGI::Application')) {
$callpkg->new_hook('tt_pre_process');
$callpkg->new_hook('tt_post_process');
} else {
warn "Calling package is not a CGI::Application module so not installing tt_pre_process and tt_post_process hooks. If you are using \@ISA instead of 'use base', make sure it is in a BEGIN { } block, and make sure these statements appear before the plugin is loaded";
}
}
##############################################
###
### tt_obj
###
##############################################
#
# Get a Template Toolkit object. The same object
# will be returned every time this method is called
# during a request cycle.
#
sub tt_obj {
my $self = shift;
my ($tt, $options, $frompkg) = _get_object_or_options($self);
if (!$tt) {
my $tt_options = $options->{TEMPLATE_OPTIONS};
if (keys %{$options->{TEMPLATE_OPTIONS}}) {
$tt = Template->new( $options->{TEMPLATE_OPTIONS} ) || carp "Can't load Template";
} else {
$tt = Template->new || carp "Can't load Template";
}
_set_object($frompkg||$self, $tt);
}
return $tt;
}
##############################################
###
### tt_config
###
##############################################
#
# Configure the Template Toolkit object
#
sub tt_config {
my $self = shift;
my $class = ref $self ? ref $self : $self;
my $tt_config;
if (ref $self) {
die "Calling tt_config after the tt object has already been created" if @_ && defined $self->{__TT};
$tt_config = $self->{__TT_CONFIG} ||= {};
} else {
no strict 'refs';
${$class.'::__TT_CONFIG'} ||= {};
$tt_config = ${$class.'::__TT_CONFIG'};
}
if (@_) {
my $props;
if (ref($_[0]) eq 'HASH') {
my $rthash = %{$_[0]};
$props = CGI::Application->_cap_hash($_[0]);
} else {
$props = CGI::Application->_cap_hash({ @_ });
}
my %options;
# Check for TEMPLATE_OPTIONS
if ($props->{TEMPLATE_OPTIONS}) {
carp "tt_config error: parameter TEMPLATE_OPTIONS is not a hash reference"
if Scalar::Util::reftype($props->{TEMPLATE_OPTIONS}) ne 'HASH';
$tt_config->{TEMPLATE_OPTIONS} = delete $props->{TEMPLATE_OPTIONS};
}
# Check for TEMPLATE_NAME_GENERATOR
if ($props->{TEMPLATE_NAME_GENERATOR}) {
carp "tt_config error: parameter TEMPLATE_NAME_GENERATOR is not a subroutine reference"
if Scalar::Util::reftype($props->{TEMPLATE_NAME_GENERATOR}) ne 'CODE';
$tt_config->{TEMPLATE_NAME_GENERATOR} = delete $props->{TEMPLATE_NAME_GENERATOR};
}
# Check for TEMPLATE_PRECOMPILE_FILETEST
if ($props->{TEMPLATE_PRECOMPILE_FILETEST}) {
carp "tt_config error: parameter TEMPLATE_PRECOMPILE_FILETEST is not a subroutine reference or regexp or string"
if defined Scalar::Util::reftype($props->{TEMPLATE_PRECOMPILE_FILETEST})
&& Scalar::Util::reftype($props->{TEMPLATE_PRECOMPILE_FILETEST}) ne 'CODE'
&& overload::StrVal($props->{TEMPLATE_PRECOMPILE_FILETEST}) !~ /^Regexp=/;
$tt_config->{TEMPLATE_PRECOMPILE_FILETEST} = delete $props->{TEMPLATE_PRECOMPILE_FILETEST};
}
# This property must be tested last, since it creates the TT object in order to
# preload all the templates.
#
# Check for TEMPLATE_PRECOMPILE_DIR
if( $props->{TEMPLATE_PRECOMPILE_DIR} ) {
my $type = Scalar::Util::reftype($props->{TEMPLATE_PRECOMPILE_DIR});
carp "tt_config error: parameter TEMPLATE_PRECOMPILE_DIR must be a SCALAR or an ARRAY ref"
unless( !defined($type) or $type eq 'ARRAY' );
# now look at each file and
my @dirs = ($type && $type eq 'ARRAY') ? @{$props->{TEMPLATE_PRECOMPILE_DIR}}
: ($props->{TEMPLATE_PRECOMPILE_DIR});
delete $props->{TEMPLATE_PRECOMPILE_DIR};
my $tt = $self->tt_obj;
my $junk = '';
my $filetester = sub { 1 };
if ($tt_config->{TEMPLATE_PRECOMPILE_FILETEST}) {
if (! defined Scalar::Util::reftype($tt_config->{TEMPLATE_PRECOMPILE_FILETEST})) {
$filetester = sub { $_[0] =~ /\.$tt_config->{TEMPLATE_PRECOMPILE_FILETEST}$/ };
} elsif (Scalar::Util::reftype($tt_config->{TEMPLATE_PRECOMPILE_FILETEST}) eq 'CODE') {
$filetester = $tt_config->{TEMPLATE_PRECOMPILE_FILETEST};
} elsif (overload::StrVal($tt_config->{TEMPLATE_PRECOMPILE_FILETEST}) =~ /^Regexp=/) {
$filetester = sub { $_[0] =~ $tt_config->{TEMPLATE_PRECOMPILE_FILETEST} };
}
}
require File::Find;
File::Find::find(
sub {
my $file = $File::Find::name;
return unless $filetester->($file);
if( !-d $file ) {
$tt->process( $file, {}, \$junk );
}
},
map { File::Spec->rel2abs($_) } @dirs,
);
}
# If there are still entries left in $props then they are invalid
carp "Invalid option(s) (".join(', ', keys %$props).") passed to tt_config" if %$props;
}
$tt_config;
}
##############################################
###
### tt_params
###
##############################################
#
# Set some parameters that will be added to
# any template object we process in this
# request cycle.
#
sub tt_params {
my $self = shift;
my @data = @_;
# Define the params stash if it doesn't exist
$self->{__TT_PARAMS} ||= {};
if (@data) {
my $params = $self->{__TT_PARAMS};
my $newparams = {};
if (ref $data[0] eq 'HASH') {
# hashref
%$newparams = %{ $data[0] };
} elsif ( (@data % 2) == 0 ) {
%$newparams = @data;
} else {
carp "tt_params requires a hash or hashref!";
}
# merge the new values into our stash of parameters
@$params{keys %$newparams} = values %$newparams;
}
return $self->{__TT_PARAMS};
}
##############################################
###
### tt_clear_params
###
##############################################
#
# Clear any template parameters that may have
# been set during this request cycle.
#
sub tt_clear_params {
my $self = shift;
my $params = $self->{__TT_PARAMS};
$self->{__TT_PARAMS} = {};
return $params;
}
##############################################
###
### tt_pre_process
###
##############################################
#
# Sample method that is called just before
# a Template is processed.
# Useful for setting global template params.
# It is passed the template filename and the hashref
# of template data
#
sub tt_pre_process {
my $self = shift;
my $file = shift;
my $vars = shift;
# Do your pre-processing here
}
##############################################
###
### tt_post_process
###
##############################################
#
# Sample method that is called just after
# a Template is processed.
# Useful for post processing the HTML.
# It is passed a scalar reference to the HTML code.
#
# Note: This could also be accomplished using the
# cgiapp_postrun method, except that this
# method is called after every template is
# processed (you could process multiple
# templates in one request), whereas
# cgiapp_postrun is only called once after
# the runmode has completed.
#
sub tt_post_process {
my $self = shift;
my $htmlref = shift;
# Do your post-processing here
}
##############################################
###
### tt_process
###
##############################################
#
# Process a Template Toolkit template and return
# the resulting html as a scalar ref
#
sub tt_process {
my $self = shift;
my $file = shift;
my $vars = shift;
my $html = '';
my $can_call_hook = UNIVERSAL::can($self, 'call_hook') ? 1 : 0;
if (! defined($vars) && (Scalar::Util::reftype($file)||'') eq 'HASH') {
$vars = $file;
$file = undef;
}
$file ||= $self->tt_template_name(1);
$vars ||= {};
my $template_name = $file;
# Call the load_tmpl hook that is part of CGI::Application
$self->call_hook(
'load_tmpl',
{}, # template options are ignored
$vars,
$file,
) if $can_call_hook;
# Call tt_pre_process hook
$self->tt_pre_process($file, $vars) if $self->can('tt_pre_process');
$self->call_hook('tt_pre_process', $file, $vars) if $can_call_hook;
# Include any parameters that may have been
# set with tt_params
my %params = ( %{ $self->tt_params() }, %$vars );
# Add c => $self in as a param for convenient access to sessions and such
$params{c} ||= $self;
$self->tt_obj->process($file, \%params, \$html) || croak $self->tt_obj->error();
# Call tt_post_process hook
$self->tt_post_process(\$html) if $self->can('tt_post_process');
$self->call_hook('tt_post_process', \$html) if $can_call_hook;
_tt_add_devpopup_info($self, $template_name, \%params);
return \$html;
}
##############################################
###
### tt_include_path
###
##############################################
#
# Change the include path after the template object
# has already been created
#
sub tt_include_path {
my $self = shift;
return $self->tt_obj->context->load_templates->[0]->include_path unless(@_);
$self->tt_obj->context->load_templates->[0]->include_path(ref($_[0]) ? $_[0] : [@_]);
return;
}
##############################################
###
### tt_template_name
###
##############################################
#
# Auto-generate the filename of a template based on
# the current module, and the name of the
# function that called us.
#
sub tt_template_name {
my $self = shift;
my ($tt, $options, $frompkg) = _get_object_or_options($self);
my $func = $options->{TEMPLATE_NAME_GENERATOR} || \&__tt_template_name;
return $self->$func(@_);
}
##############################################
###
### __tt_template_name
###
##############################################
#
# Generate the filename of a template based on
# the current module, and the name of the
# function that called us.
#
# example:
# module $self is blessed into: My::Module
# function name that called us: my_function
#
# generates: My/Module/my_function.tmpl
#
sub __tt_template_name {
my $self = shift;
my $uplevel = shift || 0;
# the directory is based on the object's package name
my $dir = File::Spec->catdir(split(/::/, ref($self)));
# the filename is the method name of the caller plus
# whatever offset the user asked for
(caller(2+$uplevel))[3] =~ /([^:]+)$/;
my $name = $1;
return File::Spec->catfile($dir, $name.'.tmpl');
}
##
## Private methods
##
sub _set_object {
my $self = shift;
my $tt = shift;
my $class = ref $self ? ref $self : $self;
if (ref $self) {
$self->{__TT_OBJECT} = $tt;
} else {
no strict 'refs';
${$class.'::__TT_OBJECT'} = $tt;
}
}
sub _get_object_or_options {
my $self = shift;
my $class = ref $self ? ref $self : $self;
# Handle the simple case by looking in the object first
if (ref $self) {
return ($self->{__TT_OBJECT}, $self->{__TT_CONFIG}) if $self->{__TT_OBJECT};
return (undef, $self->{__TT_CONFIG}) if $self->{__TT_CONFIG};
}
# See if we can find them in the class hierarchy
# We look at each of the modules in the @ISA tree, and
# their parents as well until we find either a tt
# object or a set of configuration parameters
require Class::ISA;
foreach my $super ($class, Class::ISA::super_path($class)) {
no strict 'refs';
return (${$super.'::__TT_OBJECT'}, ${$super.'::__TT_CONFIG'}, $super) if ${$super.'::__TT_OBJECT'};
return (undef, ${$super.'::__TT_CONFIG'}, $super) if ${$super.'::__TT_CONFIG'};
}
return;
}
##############################################
###
### _tt_add_devpopup_info
###
##############################################
#
# This method will look to see if the devpopup
# plugin is being used, and will display all the
# parameters that were passed to the template.
#
sub _tt_add_devpopup_info {
my $self = shift;
my $name = shift;
my $params = shift;
return unless UNIVERSAL::can($self, 'devpopup');
my %params = %$params;
foreach my $key (keys %params) {
if (my $class = Scalar::Util::blessed($params{$key})) {
$params{$key} = "Object:$class";
}
}
require Data::Dumper;
my $dumper = Data::Dumper->new([\%params]);
$dumper->Varname('Params');
$dumper->Indent(2);
my $dump = $dumper->Dump();
# Entity encode the output since it will be displayed on a webpage and we
# want all HTML content rendered as text (borrowed from HTML::Entities)
$dump =~ s/([^\n\r\t !\#\$%\(-;=?-~])/sprintf "%X;", ord($1)/ge;
$self->devpopup->add_report(
title => "TT params for $name",
summary => "All template parameters passed to template $name",
report => qq{},
);
return;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
CGI::Application::Plugin::TT - Plugin that adds Template Toolkit support to CGI::Application
=head1 VERSION
version 1.06
=head1 SYNOPSIS
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
sub myrunmode {
my $self = shift;
my %params = (
email => 'email@company.com',
menu => [
{ title => 'Home', href => '/home.html' },
{ title => 'Download', href => '/download.html' },
],
session_obj => $self->session,
);
return $self->tt_process('template.tmpl', \%params);
}
=head1 DESCRIPTION
CGI::Application::Plugin::TT adds support for the popular Template Toolkit engine
to your L modules by providing several helper methods that
allow you to process template files from within your runmodes.
It compliments the support for L that is built into L
through the B method. It also provides a few extra features than just the ability
to load a template.
=head1 METHODS
=head2 tt_process
This is a simple wrapper around the Template Toolkit process method. It
accepts zero, one or two parameters; an optional template filename, and an
optional hashref of template parameters (the template filename is optional, and
will be autogenerated by a call to $self->tt_template_name if not provided).
The return value will be a scalar reference to the output of the template.
package My::App::Browser
sub myrunmode {
my $self = shift;
return $self->tt_process( 'Browser/myrunmode.tmpl', { foo => 'bar' } );
}
sub myrunmode2 {
my $self = shift;
return $self->tt_process( { foo => 'bar' } ); # will process template 'My/App/Browser/myrunmode2.tmpl'
}
=head2 tt_config
This method can be used to customize the functionality of the CGI::Application::Plugin::TT module,
and the Template Toolkit module that it wraps. The recommended place to call C
is as a class method in the global scope of your module (See SINGLETON SUPPORT for an explanation
of why this is a good idea). If this method is called after a
call to tt_process or tt_obj, then it will die with an error message.
It is not a requirement to call this method, as the module will work without any
configuration. However, most will find it useful to set at least a path to the
location of the template files ( or you can set the path later using the tt_include_path
method).
our $TEMPLATE_OPTIONS = {
COMPILE_DIR => '/tmp/tt_cache',
DEFAULT => 'notfound.tmpl',
PRE_PROCESS => 'defaults.tmpl',
};
__PACKAGE__->tt_config( TEMPLATE_OPTIONS => $TEMPLATE_OPTIONS );
The following parameters are accepted:
=over 4
=item TEMPLATE_OPTIONS
This allows you to customize how the L object is created by providing a list of
options that will be passed to the L constructor. Please see the documentation
for the L module for the exact syntax of the parameters, or see below for an example.
=item TEMPLATE_NAME_GENERATOR
This allows you to provide your own method for auto-generating the template filename. It requires
a reference to a function that will be passed the $self object as it's only parameter. This function
will be called every time $self->tt_process is called without providing the filename of the template
to process. This can standardize the way templates are organized and structured by making the
template filenames follow a predefined pattern.
The default template filename generator uses the current module name, and the name of the calling
function to generate a filename. This means your templates are named by a combination of the
module name, and the runmode.
=item TEMPLATE_PRECOMPILE_DIR
This options allows you to specify a directory (or an array of directories) to
search when this module is loaded and then compile all files found into memory.
This provides a speed boost in persistent environments (mod_perl, fast-cgi) and
can improve memory usage in environments that use shared memory (mod_perl).
=item TEMPLATE_PRECOMPILE_FILETEST
This option allows you to specify exactly which files will get compiled when
using the TEMPLATE_PRECOMPILE_DIR option. You can provide it with one of 3
different variable types:
=over 4
=item STRING
A filename extension that can specify what type of files will be loaded (eg
'tmpl').
=item REGEXP
Filenames that match the regular expression will be precompiled ( eg
qr/\.(tt|tmpl|html)$/ ).
=item CODEREF
A code reference that will be called once for each filename and directory
found, and if it returns true, the template will be precompiled (eg sub { my
$file = shift; ... } ).
=back
=back
=head2 tt_obj
This method will return the underlying Template Toolkit object that is used
behind the scenes. It is usually not necessary to use this object directly,
as you can process templates and configure the Template object through
the tt_process and tt_config methods. Every call to this method will
return the same object during a single request.
It may be useful for debugging purposes.
=head2 tt_params
This method will accept a hash or hashref of parameters that will be included
in the processing of every call to tt_process. It is important to note that
the parameters defined using tt_params will be passed to every template that is
processed during a given request cycle. Usually only one template is processed
per request, but it is entirely possible to call tt_process multiple times with
different templates. Every time tt_process is called, the hashref of parameters
passed to tt_process will be merged with the parameters set using the tt_params
method. Parameters passed through tt_process will have precedence in case of
duplicate parameters.
This can be useful to add global values to your templates, for example passing
the user's name automatically if they are logged in.
sub cgiapp_prerun {
my $self = shift;
$self->tt_params(username => $ENV{REMOTE_USER}) if $ENV{REMOTE_USER};
}
=head2 tt_clear_params
This method will clear all the currently stored parameters that have been set with
tt_params.
=head2 tt_pre_process
This is an overridable method that works in the spirit of cgiapp_prerun. The method will
be called just before a template is processed, and will be passed the template filename,
and a hashref of template parameters. It can be used to make last minute changes to the
template, or the parameters before the template is processed.
sub tt_pre_process {
my ($self, $file, $vars) = @_;
$vars->{user} = $ENV{REMOTE_USER};
return;
}
If you are using CGI::Application 4.0 or greater, you can also register this as a callback.
__PACKAGE__->add_callback('tt_pre_process', sub {
my ($self, $file, $vars) = @_;
$vars->{user} = $ENV{REMOTE_USER};
return;
});
=head2 tt_post_process
This, like it's counterpart cgiapp_postrun, is called right after a template has been processed.
It will be passed a scalar reference to the processed template.
sub tt_post_process {
my ($self, $htmlref) = shift;
require HTML::Clean;
my $h = HTML::Clean->new($htmlref);
$h->strip;
my $newref = $h->data;
$$htmlref = $$newref;
return;
}
If you are using CGI::Application 4.0 or greater, you can also register this as a callback (See
tt_pre_process for an example of how to use it).
=head2 tt_template_name
This method will generate a template name for you based on two pieces of information: the
method name of the caller, and the package name of the caller. It allows you to consistently
name your templates based on a directory hierarchy and naming scheme defined by the structure
of the code. This can simplify development and lead to more consistent, readable code.
If you do not want the template to be named after the method that called
tt_template_name, you can pass in an integer, and the method used to generate
the template name will be that many levels above the caller. It defaults to
zero.
For example:
package My::App::Browser
sub dummy_call {
my $self = shift;
return $self->tt_template_name(1); # parent callers name
}
sub view {
my $self = shift;
my $template;
$template = $self->tt_template_name; # returns 'My/App/Browser/view.tmpl'
$template = $self->dummy_call; # also returns 'My/App/Browser/view.tmpl'
return $self->tt_process($template, { var1 => param1 });
}
To simplify things even more, tt_process automatically calls $self->tt_template_name for
you if you do not pass a template name, so the above can be reduced to this:
package MyApp::Example
sub view {
my $self = shift;
return $self->tt_process({ var1 => param1 }); # process template 'MyApp/Example/view.tmpl'
}
Since the path is generated based on the name of the module, you could place all of your templates
in the same directory as your Perl modules, and then pass @INC as your INCLUDE_PATH parameter.
Whether that is actually a good idea is left up to the reader.
$self->tt_include_path(\@INC);
=head2 tt_include_path
This method will allow you to set the include path for the Template Toolkit object after
the object has already been created. Normally you set the INCLUDE_PATH option when creating
the Template Toolkit object, but sometimes it can be useful to change this value after the
object has already been created. This method will allow you to do that without needing to
create an entirely new Template Toolkit object. This can be especially handy when using
the Singleton support mentioned below, where a Template Toolkit object may persist across many request.
It is important to note that a call to tt_include_path will change the INCLUDE_PATH for all
subsequent calls to this object, until tt_include_path is called again. So if you change the
INCLUDE_PATH based on the user that is connecting to your site, then make sure you call
tt_include_path on every request.
my $root = '/var/www/';
$self->tt_include_path( [$root.$ENV{SERVER_NAME}, $root.'default'] );
When called with no parameters tt_include_path returns an arrayref containing
the current INCLUDE_PATH.
=head1 DEFAULT PARAMETERS
By default, the TT plugin will automatically add a parameter 'c' to the template that
will return to your CGI::Application object $self. This allows you to access any
methods in your CGI::Application module that you could normally call on $self
from within your template. This allows for some powerful actions in your templates.
For example, your templates will be able to access query parameters, or if you use
the CGI::Application::Plugin::Session module, you can access session parameters.
Hello [% c.session.param('username') || 'Anonymous User' %]
Reload this page
Another useful plugin that can use this feature is the CGI::Application::Plugin::HTMLPrototype
plugin, which gives easy access to the very powerful prototype.js JavaScript library.
[% c.prototype.define_javascript_functions %]
Extra Info
With this extra flexibility comes some responsibility as well. It could lead down a
dangerous path if you start making alterations to your object from within the template.
For example you could call c.header_add to add new outgoing headers, but that is something
that should be left in your code, not in your template. Try to limit yourself to
pulling in information into your templates (like the session example above does).
=head1 EXAMPLE
In a CGI::Application module:
package My::App
use CGI::Application::Plugin::TT;
use base qw(CGI::Application);
# configure the template object once during the init stage
sub cgiapp_init {
my $self = shift;
# Configure the template
$self->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => '/path/to/template/files',
POST_CHOMP => 1,
FILTERS => {
'currency' => sub { sprintf('$ %0.2f', @_) },
},
},
);
}
sub cgiapp_prerun {
my $self = shift;
# Add the username to all templates if the user is logged in
$self->tt_params(username => $ENV{REMOTE_USER}) if $ENV{REMOTE_USER};
}
sub tt_pre_process {
my $self = shift;
my $template = shift;
my $params = shift;
# could add the username here instead if we want
$params->{username} = $ENV{REMOTE_USER}) if $ENV{REMOTE_USER};
return;
}
sub tt_post_process {
my $self = shift;
my $htmlref = shift;
# clean up the resulting HTML
require HTML::Clean;
my $h = HTML::Clean->new($htmlref);
$h->strip;
my $newref = $h->data;
$$htmlref = $$newref;
return;
}
sub my_runmode {
my $self = shift;
my %params = (
foo => 'bar',
);
# return the template output
return $self->tt_process('my_runmode.tmpl', \%params);
}
sub my_otherrunmode {
my $self = shift;
my %params = (
foo => 'bar',
);
# Since we don't provide the name of the template to tt_process, it
# will be auto-generated by a call to $self->tt_template_name,
# which will result in a filename of 'Example/my_otherrunmode.tmpl'.
return $self->tt_process(\%params);
}
=head1 SINGLETON SUPPORT
Creating a Template Toolkit object can be an expensive operation if it needs to be done for every
request. This startup cost increases dramatically as the number of templates you use
increases. The reason for this is that when TT loads and parses a template, it
generates actual Perl code to do the rendering of that template. This means that the rendering of
the template is extremely fast, but the initial parsing of the templates can be inefficient. Even
by using the built-in caching mechanism that TT provides only writes the generated Perl code to
the filesystem. The next time a TT object is created, it will need to load these templates from disk,
and eval the source code that they contain.
So to improve the efficiency of Template Toolkit, we should keep the object (and hence all the compiled
templates) in memory across multiple requests. This means you only get hit with the startup cost
the first time the TT object is created.
All you need to do to use this module as a singleton is to call tt_config as a class method
instead of as an object method. All the same parameters can be used when calling tt_config
as a class method.
When creating the singleton, the Template Toolkit object will be saved in the namespace of the
module that created it. The singleton will also be inherited by any subclasses of
this module. So in effect this is not a traditional Singleton, since an instance of a Template
Toolkit object is only shared by a module and it's children. This allows you to still have different
configurations for different CGI::Application modules if you require it. If you want all of your
CGI::Application applications to share the same Template Toolkit object, just create a Base class that
calls tt_config to configure the plugin, and have all of your applications inherit from this Base class.
=head1 SINGLETON EXAMPLE
package My::App;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
My::App->tt_config(
TEMPLATE_OPTIONS => {
POST_CHOMP => 1,
},
);
sub cgiapp_prerun {
my $self = shift;
# Set the INCLUDE_PATH (will change the INCLUDE_PATH for
# all subsequent requests as well, until tt_include_path is called
# again)
my $basedir = '/path/to/template/files/',
$self->tt_include_path( [$basedir.$ENV{SERVER_NAME}, $basedir.'default'] );
}
sub my_runmode {
my $self = shift;
# Will use the same TT object across multiple request
return $self->tt_process({ param1 => 'value1' });
}
package My::App::Subclass;
use base qw(My::App);
sub my_other_runmode {
my $self = shift;
# Uses the TT object from the parent class (My::App)
return $self->tt_process({ param2 => 'value2' });
}
=head1 BUGS
Please report any bugs or feature requests to
C, or through the web interface at
L. I will be notified, and then you'll automatically
be notified of progress on your bug as I make changes.
=head1 CONTRIBUTING
Patches, questions and feedback are welcome.
=head1 SEE ALSO
L, L, perl(1)
=head1 AUTHOR
Cees Hek
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2024 by Cees Hek.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
UpLevel 000755 001750 001750 0 14721400126 20705 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/TestAppTName test_mode.tmpl 100644 001750 001750 246 14721400126 23710 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/TestAppTName/UpLevel file: test_mode.tmpl
:[% template_var %]:
:[% template_param_hash %]:
:[% template_param_hashref %]:
:[% pre_process_var %]:
:post_process_var:
:[% template_name %]:
NoNameNoVars 000755 001750 001750 0 14721400126 21637 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/TestAppTName test_mode.tmpl 100644 001750 001750 246 14721400126 24642 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/TestAppTName/NoNameNoVars file: test_mode.tmpl
:[% template_var %]:
:[% template_param_hash %]:
:[% template_param_hashref %]:
:[% pre_process_var %]:
:post_process_var:
:[% template_name %]:
TestAppIncludePath 000755 001750 001750 0 14721400126 22231 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/include1 test_mode.tmpl 100644 001750 001750 70 14721400126 25207 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/include1/TestAppIncludePath include path: [% include_path %]
template dir: include1
TestAppIncludePath 000755 001750 001750 0 14721400126 22232 5 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/include2 test_mode.tmpl 100644 001750 001750 70 14721400126 25210 0 ustar 00wes wes 000000 000000 CGI-Application-Plugin-TT-1.06/t/include2/TestAppIncludePath include path: [% include_path %]
template dir: include2