Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

How to use self made Template Toolkit plugins?

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


kakimoto at tpg

Oct 20, 2008, 5:39 AM

Post #1 of 4 (698 views)
Permalink
How to use self made Template Toolkit plugins?

hi there
i am thinking of writing some template toolkit plugins for use within
my *.tt2 files.

I read the Template toolkit book but of course, this is catalyst so some
things (ie configuration) is different

1) how do I tell my *.tt2 file that a certain template is to be found in
a certain location?

2) where is the best place (based on best practices) to store the plugin
files for TT2?

thanks

k.akimoto

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


jshirley at gmail

Oct 20, 2008, 8:55 AM

Post #2 of 4 (631 views)
Permalink
Re: How to use self made Template Toolkit plugins? [In reply to]

On Mon, Oct 20, 2008 at 5:39 AM, <kakimoto[at]tpg.com.au> wrote:
>
> hi there
> i am thinking of writing some template toolkit plugins for use within
> my *.tt2 files.
>
> I read the Template toolkit book but of course, this is catalyst so some
> things (ie configuration) is different
>

Not really, the hash that is passed into MyApp->config->{'View::TT'}
is passed into TT's ->new, so it is just plain old config. Your
question is also exclusive to Catalyst::View::TT, which is a very
brief RTFS: http://search.cpan.org/src/MRAMBERG/Catalyst-View-TT-0.27/lib/Catalyst/View/TT.pm

That should allow you to figure out how the TT object is being created.

> 1) how do I tell my *.tt2 file that a certain template is to be found in
> a certain location?
>

As documented in Catalyst::View::TT, you just add entries into INCLUDE_PATH
MyApp->config(
'View::TT' => {
INCLUDE_PATH => [.
MyApp->path_to( 'root', 'src' ), # root/src
MyApp->path_to( 'root', 'lib' ), # root/lib
],
}
);

> 2) where is the best place (based on best practices) to store the plugin
> files for TT2?
>

The same way every other plugin works. Code it like it is going on
CPAN, and install it - extra points for using local::lib for your
dependencies, too, so you have a separation from your system's site
perl.

-J

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jun 1, 2009, 4:02 AM

Post #3 of 4 (365 views)
Permalink
Re: How to use self made Template Toolkit plugins? [In reply to]

hi there,
how do I access the Catalyst variable in a custom made Template
Toolkit plugin?

WhaT I have done: read up on
http://search.cpan.org/src/MRAMBERG/Catalyst-View-TT-0.27/lib/Catalyst/View/TT.pm

For example, I have my own plugin, "package
myApp::View::Template::Plugin::User"

use strict;
use warnings;
use base 'Template::Plugin';
use Template::Plugin;
use DateTime;
use DateTime::Format::Pg;

sub load {
my ( $class, $context ) = @_;
return $class;
}

sub new {
my ( $class, $context, @params ) = @_;

bless { _CONTEXT => $context, }, $class; # returns blessed
MyPlugin object
}

sub get_current_user
{
my ($class, $c) = @_;

$c->log->debug( "The type is " . ref ($c));

return $c->session->{'active_user'} ? $c->session->{'active_user'} :
$c->user->login_id;
}

===============



And in my template toolkit, "display_user.tt2" I do something like:

[% current_user = User.get_current_user(Catalyst) %]
THE current user is ([% current_user %])....


I noticed that myApp::View::Template::Plugin::User->get_current_user
doesn't print out any values.
I suspect it has not even been able to recognise the Catalyst variable.

I even tried doing "[% current_user = User.get_current_user( $Catalyst
) %]" in the display_user.tt2 template file.


Please help :)


K. akimoto






_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jun 1, 2009, 5:03 AM

Post #4 of 4 (364 views)
Permalink
Re: Re: How to use self made Template Toolkit plugins? [In reply to]

kakimoto[at]tpg.com.au wrote:
> hi there,
> how do I access the Catalyst variable in a custom made Template
> Toolkit plugin?

You don't want to do this.

TT plugins should be for trivial formatting stuff only, teaching them
about your application context is massively breaking abstraction.

Cheers
t0m

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.