logo       

preprocess [Was: Re: variable recursion in TT]: msg#00041

Subject: preprocess [Was: Re: variable recursion in TT]
Josh,

Thank you for your answer.

I looked into the code and it seems to me I'm better off with redefining _load. Something like:

sub _load {
# ....

       elsif (-f $name) {
           if (open(FH, $name)) {
               my $text = <FH>;
               $text = $self->_decode_unicode($text) if $self->{ UNICODE };
+              $self->_processMultilang(\$text);
               $data = {
                   name => $alias,
                   path => $name,
                   text => $text,
                   time => (stat $name)[9],
                   load => $now,
               };
           }
           elsif ($tolerant) {
($data, $error) = (undef, Template::Constants::STATUS_DECLINED);
           }
           else {
               $data  = "$alias: $!";
               $error = Template::Constants::STATUS_ERROR;
           }
       }
       else {
           ($data, $error) = (undef, Template::Constants::STATUS_DECLINED);
       }
   }

# ...



BTW, it would be nice to have something like a Template::Provider::_preprocess ()

sub _preporcess {
   my ($self, $textref) = @_;
   $self->{CUSTOM_PREPORCESS} && &{$self->{CUSTOM_PREPORCESS}}($textref);
}

in Provider.pm, called from _load() as $self->_preprocess(\$text), and Template::_init () as

#...
die("Nope, I need a coderef!") if ($self->{CUSTOM_PREPROCESS} && (ref $self->{CUSTOM_PREPROCESS} ne 'CODE'));
$self->{CUSTOM_PREPROCESS} = $config->{CUSTOM_PREPROCESS};
#...


:)) What do you think?



And another thing. Is there a mechanism to preload a list of templates into Template.pm? I have an application server that uses TT, and as far as I know, templates are (???) cached in $self, which is not very efficient in my case as all forked children have their separate copies of the compiled code.

- Fagzal

Hi, I just ran through the code and I do not believe there is a way to do this. I think you will need to subclass Provider.pm, and redefine the _fetch routine. You can probably just copy it over into your subclassed module, and then make the modifications you need.

There are a few examples how you can subclass Provider.pm on the web:
http://template-toolkit.org/pipermail/templates/2001-June/001037.html
http://66.102.7.104/search?q=cache:vBQNoGAjDa0J:www.grahamnet.org.uk/test/slash/Slash/Display/Provider/Provider.pm+%22use+base+qw(Template::Provider)%3B%22&hl=en
http://www.mail-archive.com/templates-MnLy+PD7ppGDO3lXZvfOKmD2FQJk+8+b@xxxxxxxxxxxxxxxx/msg05852.html


Here tells you how to use your subclassed Provider.pm I think.
http://www.template-toolkit.org/docs/plain/Manual/Config.html
I believe it is the LOAD_TEMPLATES config option, although I could be entirely wrong as I have not done this myself before.

Hopefully that helps you out. If anyone thinks what I've proposed is wrong, feel free to yell. :) This is all based on a quick run-thru of the code.

-- Josh

Fagyal Csongor wrote:

Hi folks,

I would like to kind of pre-process the templates I pass to TT. However, I want to keep all the nice caching features (disk+memory), so what I would like to do is to first filter the file TT is processing through a sub I wrote, but *only* when the file is changed on disk (which TT knows and handles anyway). So I need something like:

sub process {
   # ....
   # do lots of magic
   # ...
   # if (template_changed_on_disk) $text = slurp ....
   # ...

   &my_filter_hack(\$text);   # doing some multilingual preporcessing

   # ...
   # continue magic
   # ...
}

Is there an easy way to do what's above? Preferably without subclassing / rewriting Template.pm :)

Thanks,
- Fagzal



_______________________________________________
templates mailing list
templates-MnLy+PD7ppGDO3lXZvfOKmD2FQJk+8+b@xxxxxxxxxxxxxxxx
http://lists.template-toolkit.org/mailman/listinfo/templates



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
boot-loaders.gr...    php.pear.genera...    debugging.valgr...    kde.redhat.user...    text.xml.xsl.ge...    culture.languag...    hardware.microc...    java.servicemix...    redhat.release....    web.zope.plone....    user-groups.lin...    opendarwin.webk...    video.mjpeg.use...    sysutils.bcfg2....    encryption.gpg....    lx-office.devel...    xfree86.forum/2...    mail.mutt.devel...    acpi.devel/2003...    qnx.openqnx.dev...    network.irc.irs...    freebsd.devel.m...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe