Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot

Tidy for Template Toolkit Files

by roho (Monsignor)
on Feb 11, 2011 at 15:13 UTC ( #887632=perlquestion: print w/ replies, xml ) Need Help??
roho has asked for the wisdom of the Perl Monks concerning the following question:

I have been searching for the equivalent of perltidy, HTML::Tidy, etc. for Template Toolkit files.

I have inherited some rather challenging tt files to maintain, and they are sorely in need of reformatting. There are many nested control statements (see examples below) that are not indented, which makes deciphering the logic flow next to impossible.

[% IF %] [% ELSE %] [% END %] [% WHILE %] [% FOREACH %] etc.

Does anyone know of a utility to reformat tt files? I tried Template::Plugin::PerlTidy, but unfortunately its reformatting is no better (in many ways worse) than the original tt file.

"Its not how hard you work, its how much you get done."

Comment on Tidy for Template Toolkit Files
Download Code
Re: Tidy for Template Toolkit Files
by sundialsvc4 (Monsignor) on Feb 11, 2011 at 15:55 UTC

    I am not aware of a “pretty-printer” utility for TT files, but it would certainly be a nice thing to have.

Re: Tidy for Template Toolkit Files
by CountZero (Chancellor) on Feb 11, 2011 at 16:18 UTC
    Template Toolkit files are of course an ungainly --but performant-- marriage between HTML-code and TT-code and it is my experience that most of the time any pretty-formatting of the HTML-code makes the TT-code unreadable and vice-versa.

    Of course, TT-files are not limited to HTML (I use them a lot to write LaTeX documents) but any mixture of different languages will always be awkward.

    Oh, and Template::Plugin::PerlTidy is not made for pretty-printing TT-files. Rather it will pretty-print Perl-code in web-pages.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Tidy for Template Toolkit Files
by toolic (Chancellor) on Feb 11, 2011 at 16:20 UTC
      Thanks, but I've already tried that and it does not do what I want.

      "Its not how hard you work, its how much you get done."

Re: Tidy for Template Toolkit Files
by Anonymous Monk on Feb 12, 2011 at 10:03 UTC
Re: Tidy for Template Toolkit Files
by Khen1950fx (Canon) on Feb 12, 2011 at 11:42 UTC
    Would this help?
    #!/usr/bin/perl use strict; use warnings; use Template; my $template = Template->new(); my ($a, $b, $c, $d, $e) = qw( IF ELSE END WHILE ECHO); my $params = { 'a' => $a, 'b' => $b, 'c' => $c, 'd' => $d, 'e' => $e, }; print $template->process(\*DATA, $params); __DATA__ [% USE format('%s') %] [% FOREACH item = [ a b c d e] %] [% format(item) +%] [% END %]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://887632]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-04-21 10:55 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (493 votes), past polls