http://www.perlmonks.org?node_id=887632

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."

Replies are listed 'Best First'.
Re: Tidy for Template Toolkit Files
by CountZero (Bishop) 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.

    CountZero

    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 (Bishop) 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 sundialsvc4 (Abbot) 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 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 %]
Re: Tidy for Template Toolkit Files
by Anonymous Monk on Feb 12, 2011 at 10:03 UTC