Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Tidy for Template Toolkit Files

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

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://887632]
Approved by marto
Front-paged by Corion
help
Chatterbox?
[Corion]: marto: Yep, but I didn't make it up north, sorry ;)
[Corion]: But LPW was enjoyable, even though it's always a bit hectic due to cramming everything into one day
[Corion]: I have no feeling about how my talk was received, but I'm happy with it, which counts the most to me ;-D
[Corion]: Also, London is always a nice visit, as I got to meet a friend there, and spent some time offline, working on "minor" features of the shadertoy thing ;)
[marto]: maybe next year when the kids are a little older I'll have time to attend perl events in Europe

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2016-12-07 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (125 votes). Check out past polls.