On 3/1/2010 9:26 PM, Jim Battle wrote:
> I have a wrapper which I want to (if the file type is right) run my
> "content" through an external program, specifically it is a perl script
> that compresses my css files.
>
> Run standalone, I can use this script as a typical unix filter:
>
> compress_css.pl big.css> small.css
> or
> cat big.css | compress_css.pl> small.css
>
> Now I want to wire this into my TT code. I should add that I generate
> static files, and all my website is generated by ttree.
>
> In wrapper.tt I have:
>
> [% SWITCH type %]
> [% CASE 'text' %]
> content
> [% CASE 'css' %]
> [% PERL %]
> my $txt = $context->stash()->get('content');
> open(FH, "|perl lib/compact_css.pl") or die "Couldn't open css
> compressor";
> print FH $txt;
> close(FH);
> [% END %]
> [% CASE %]
> content WRAPPER html_wrapper.tt
> [% END %]
There is always more than one way to do these things. Here's another way I
don't think anyone has mentioned, but is a possibility.
Command line: ttree --pre_process=pre_process.tt
File pre_process.tt
---------------------------------------------
[% PERL %]
$context->define_filter('minimize_css', sub {
my $text = shift;
#do stuff; # i would make compact_css.pl into a module you could use and
access
return $text
});
[% END %]
---------------------------------------------
Then your code would become:
---------------------------------------------
[% SWITCH type %]
[% CASE 'text' %]
content
[% CASE 'css' %]
[% content | minimize_css %]
[% CASE %]
content WRAPPER html_wrapper.tt
[% END %]
---------------------------------------------
Note: I didn't test this, but it seems like a good/simple way to go about it.
Plus you can easily reuse the filter in the rest of your code.
-- Josh
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates