Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Tidy for Template Toolkit Files

by roho (Canon)
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."

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.


    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

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
[ambrus]: it's slow as hell, and the food costs significantly more.
[ambrus]: We no longer eat there.
LanX Everybody quotes it, some people read it, and a few people go by it
[ambrus]: LanX: why would I read it? isn't it a book for managers? I don't want to be a manager.
[ambrus]: I don't quote it either.
[erix]: it's really a software book, albeit about experiences from the 60s-80s. People haven't changed all that much though.
[erix]: a software project book, is probably more exact
[ambrus]: Yes, but I'm not managing software teams either.
[ambrus]: I'm not only not a restaurant manager, but also not a software manager.
[erix]: Peter's principle says it' sonly a matter of time ;)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2017-09-22 13:57 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (264 votes). Check out past polls.