Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Template Toolkit custom plugins?

by lindex (Friar)
on Oct 03, 2015 at 03:00 UTC ( #1143694=perlquestion: print w/replies, xml ) Need Help??

lindex has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have any full examples having developed a custom Template::Toolkit plugin and loading it in a template?

Replies are listed 'Best First'.
Re: Template Toolkit custom plugins?
by dsheroh (Monsignor) on Oct 03, 2015 at 09:07 UTC
    This is practically a copy/paste from the docs, so if the docs don't help you then this probably won't either, but here you go:
    * In lib/MyApp/TT/Plugin/
    package MyApp::TT::Plugin::BBCodeFilter; use base qw(Template::Plugin::Filter); use Parse::BBCode; my $bbc; sub init { my $self = shift; my $name = $self->{_CONFIG}->{name} || 'bbcode'; $self->install_filter($name); $bbc = Parse::BBCode->new; return $self; } sub filter { my ($self, $text) = @_; $text = $bbc->render($text); return $text; } 1;

    * In the main application code:
    my %tpl_settings = ( ... PLUGIN_BASE => 'MyApp::TT::Plugin', ... ); my $tt = Template->new(%tpl_settings);

    * In a random template file:
    [% USE BBCodeFilter -%] <p>[% (comments || '(No Comments)') | bbcode %]</p>

    Simple as that. So what's actually causing you problems?
      I just wanted some different examples of types of plugin usage, the documentation is a little sparse on cookbook examples. My example:
      package MyHungryHippo; use strict; use warnings; use 5.16.0; use base qw[Template::Plugin]; sub new { my ($class, $context) = @_; return bless { _context => $context }, $class; } sub nomnom { my $self = shift; return 'eat it'; } 1; package main; use strict; use warnings; use 5.16.0; use Test::More; use_ok 'MyHungryHippo'; use_ok 'Template'; ok my $tt = Template->new({ PLUGINS => { 'HIPPO' => 'MyHungryHippo' } +}); die $tt->error if $tt->error; ok my $tmpl = join('', <DATA>); ok my $output = $tt->context->process( \$tmpl, {} ); like $output, qr/eat/ or diag $output; done_testing(); __DATA__ [% USE HIPPO %] [%- HIPPO.nomnom() -%]

        Hi, Here's an older example of TT usage from merlyn.
        Here's another one from the Twiki site, where i see Plugins have a separate Home page .

        Ask not what good Perl n country have done for you, Ask what good you will do for Perl n country
Re: Template Toolkit custom plugins?
by perlron (Pilgrim) on Oct 03, 2015 at 05:30 UTC

    In my experience Template::Toolkit is one of the most well documented and usable CPAN modules i have used, ever since i started using perl for scalable web develpment
    For more succinct answers, you can give the mailing list a try, though i've never had to use it owing to how well the module usage is articulated online. Even the online plugins and other documentation is really intuitive. If you can get a copy of the Badger Book , it would do you good.

    The Great Programmer is one who inspires others to code, not just one who writes great code
Re: Template Toolkit custom plugins?
by stevieb (Canon) on Oct 03, 2015 at 03:20 UTC

    Yep. Exceptionally simple ones.

    What's the real question?

Re: Template Toolkit custom plugins?
by sundialsvc4 (Abbot) on Oct 03, 2015 at 12:50 UTC

    Any of the existing plugins in CPAN are already available for your consideration as a starting point.   When I searched for template plugin, I got about 1,200 hits today.

    Yes, judicious use of custom plugins is a very beneficial part of Template-based application design.   Many app designs turn out to have reusable, app-specific presentation components that appear in many places.   These are excellent candidates to become plugins.   What might otherwise be complexity (and duplication) across many templates, instead becomes a shared reference to common code ... the custom, your-app-specific, plugin.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1143694]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2022-12-05 08:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found