in reply to Template Toolkit custom plugins?

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/BBCodeFilter.pm:
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?

Replies are listed 'Best First'.
Re^2: Template Toolkit custom plugins?
by lindex (Friar) on Oct 04, 2015 at 21:45 UTC
    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.
      -update-
      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