You can include plugins you have defined yourself that are outside of @INC by specifying a relative (to the program that constructs a new Template object) path.

Supposing the following output of ls -R:

.: Local index.pl ./Local: MyPlugin.pm

...where one might use the module Local/MyPlugin.pm with ``use Local::MyPlugin;'', an argument to the constructor of ``PLUGIN_BASE => 'Local','' would allow you to say ``[% USE MyPlugin %]''.

A trivial example (using the proposed directory structure) may be illustrated, thus:

index.pl:

#! /usr/bin/perl use strict; use warnings FATAL => 'all'; use CGI; use Template; my $tt = Template->new( { PLUGIN_BASE => 'Local', } ); print CGI->new->header; $tt->process( \*DATA, { msg => 'Seems to work' } ) or die $!; exit; __DATA__ [% USE MyPlugin %] <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd "> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>[% msg %]</title> </head> <body> <h1>[% msg | make_red %]</h1> </body> </html>

Local/MyPlugin.pm:

package Local::MyPlugin; use base qw(Template::Plugin); use strict; use warnings FATAL => 'all'; sub new { my($self, $context) = @_; $context->define_filter( 'make_red', \&make_red, 0 ); return $self; } sub make_red { my $input = shift; return '<span style="color: #f00; ">' . $input . '</span>'; } 1;

There's a book which explains all this very nicely, personally I found it easier-going than browsing the docs.

As for whether you should use your plugin-defining module elsewhere in your application, I can't see an obvious case where you'd want to use code that inherits from Template::Plugin::Base outside of the context of processing templates but, if you can, there's no particular reason you cannot. I would argue that it makes sense to keep your plugin logic packaged separately, if only to make its purpose obvious to a future maintainer.


MB

In reply to Re: TT PLUGIN_PATH by matthewb
in thread TT PLUGIN_PATH by jatill

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.