On 3/1/2010 10:26 PM, Jim Battle wrote:
> Perhaps I'm making things too difficult, but I've googled the maillist
> and searched the badger book, but I'm still not getting it.
>
> 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 %]
>
> CSS goes through my compressor, but the stdout isn't captured, it just
> spews to the screen and is gone.  I've used PERLOUT to capture the
> output of an external perl program before, but in those cases I wasn't
> trying to pipe in anything.
>
> I guess I could write $txt to a temp file, then run my filter, then
> delete the tmp, but I was hoping for something more elegant.
>
> Thanks in advance for any suggestions on the right way to do this.

I've fixed the errors in the code above (the cases other than css hadn't 
been exercised), using a tmp file.  It works.  Still, if someone can 
suggest how to do this without a tmp file, I'd like to learn the right way.

[%- SWITCH type -%]

     [%- CASE 'text' -%]

         [% content %]

     [%- CASE 'css' -%]

         [%- PERL -%]

        # save 'content' to tmp file
        my $tmp = "lib/tmp.css";
        open(FH, ">$tmp") or die "Couldn't open $!";
        print FH $context->stash()->get('content');
        close(FH);

        # filter the tmp file, sending the output back to TT
        open(FH, "perl lib/compact_css.pl $tmp |") or die "Couldn't open $!";
        print PERLOUT <FH>;
        close(FH);

        # kill off the tmp file
        unlink $tmp;

         [%- END -%]

     [%- CASE -%]

         [% content WRAPPER html_wrapper.tt %]

[%- END -%]


_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to