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

hi, I have a class (say MyClass::Blah) which have method like this f.e. :
sub cols { my ($self, $which, @list) = @_; no strict 'refs'; if (@list) { @{$$self{"_cols_$which"}} = @list; } else { return @{$$self{"_cols_$which"}} } }
Now I want to be able to call this method from a TT. I've tried to use this class it like Template plugin, w/o success :"(
I get error like this :
file error - parse error - stuff/list.tpl line 17: unexpected token (B +lah) [% use Blah %]<br>
PLUGIN_BASE is correctly set..
Do u know some other way to do this transparently ? They will be more method and classes that i will want to use this way..
and I will want to use not only object/instanse methods, but also class methods i.e. My::Blah->method()
tia

Replies are listed 'Best First'.
Re: Template::Toolkit and foreign classes ?
by merlyn (Sage) on Feb 05, 2005 at 23:23 UTC
      my stupid mistake,
      Now I get :
      plugin error - My::Blahcreate needs a hashref at /usr/lib/perl5/vendor +_perl/5.8.2/i686-linux/Template/Plugins.pm line 119
      hmm, thinking....i now wonder that i have to do it other way...or something like that I cant figure out what is the right approach to my problem..

      I have subclassed Class::DBI. In these subclases I created some methods like the shown above.
      On the other hand I made a View-object which use Template toolkit to do the HTML rendering.
      So now I want to access Class::DBI-subclasses methods from within the templates.
      This way baypassing data "retransmission" in my asp (ASP.pm) scipts. Here is snippet of example asp script :
      my $model = $class->retrieve_all(); $view->variables(data => $model, id => $Request->Params('id') ); $view->variables(list_columns => [ $class->cols('list') ] );

      I want to not use the last line, but have the ability to call them directly in the template.
      I'm also wondering 'cause this in some sense will break MVC approach, but on other hand these are just 'informational' methods (i.e. i do not plan yet :")) to do some html-rendering there..

      what is your advise ? PS.
      |<? FOR name = data.cols('list')?> <?name ?> <?END?>|
      doesnt seem to do it. Do I do something very stupid here or this is just my feeling :")