On 3/2/2010 1:15 AM, Andy Wardley wrote:
> Hi Jim,
>
> I think the best thing you can do is to move the Perl code out of the
> template.
>
> You can define a css_minify filter using a plugin like so (untested):
...
I'm not trying to be a pest, but here I am again. My inline PERL code
approach works, but I'm trying to take your advice and learn the right
way to do things.
I've created the plugin and installed it as
c:/Perl/site/lib/Template/Plugin/CrunchCss.pm and it works. That does
make for cleaner code than I had been doing. Great. But in the process
of setting it up, I had a couple questions.
Question #1:
If I want to create a plugin which is local to a given website and not
in the system-wide perl module library, what is the mechanism to add
another directory for searching for plugins? In pure perl code, I can
add that path to @INC, but how does this work under a ttree invocation?
ttree has the --plugin_base directive, but if I set it TT will then
not find anything in the conventional directory location.
If it matters, I'm running in a winxp activestate perl environment.
Question #2:
Before I tried the plugin approach, I tried just declaring a perl
subroutine and then declaring the filter in a file called
lib/utilities.tt, which is loaded as pre_process file. This file
contains things like MACRO definitions.
(-- excerpt from utilities.tt --)
[% PERL %]
sub mycrunch {
use IPC::Run3 qw(run3);
my $text = shift;
my $output;
run3 ['perl', 'lib/compact_css.pl'], \$text, \$output;
return $output;
}
$context->define_filter(
MyCrunch => \&mycrunch,
);
[% END %]
(-- end of utilities.tt --)
the wrapper would then have
context | MyCrunch
but I don't even get that far. Just attempting to declare and install
this filter fails:
C:\websites\generate>build_html.bat
+ basic-2.html
+ basic_comparison.html
Subroutine mycrunch redefined at (eval 24) line 3.
+ cpus.html
Subroutine mycrunch redefined at (eval 26) line 3.
+ css/site.css
...
Apparently utilities.tt is processed for each web page template. I
attempted to circumvent that by putting the mycrunch declaration and the
call to define_filter inside of a BEGIN ( ... } block, but that didn't
change anything.
Although I have the solution I want, I'd like to understand why this
other approach failed so I can be smarter about things in the future.
Thanks again for your help and patience.
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates