cjoy has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to have Template Toolkit output a HTML comment containing the file path when each template is called, without specifically declaring it in every single file / block?

The idea is to more easily track down what template a piece of markup in the browser came from (usage context is Bugzilla 5.0).

A quick search in both, the TT documentation and the Badger book yielded no results.

  • Comment on Template Toolkit - Output Template Name / Filepath as HTML Comment

Replies are listed 'Best First'.
Re: Template Toolkit - Output Template Name / Filepath as HTML Comment
by 1nickt (Canon) on Jun 25, 2015 at 16:19 UTC

      Thanks for the FAQ link. I did not see that before.

      I am already aware that I can print the template name this way,
      what I try to learn is how I can have this happen at every invocation of any template / include
      _without_ having to add a print statement to each.

      In many web-centric templating systems there is a config setting to make this happen.
      I fail to find such a setting or a feasible workaround for TT.

        I hope I understand what you want ... just to avoid actually typing the tags? Because if they are going to be in the outputted HTML file, they are going to get printed somehow, by somebody :-)

        One thing to consider is that you can use TT2 to make TT2 templates ... So your default template for making an .html.tmpl could include the TT2 tags to display the debug info you want ...

        Or, if you make your templates by hand, since the text strings are going to be at the top and bottom of every template file you have, it doesn't seem necessary for TT2 to magically insert them. Instead, you could have the lines pre-inserted in the actual text files you create for templates, for example by using a macro in vim:

        # in ~/.vimrc " Set up templates for new files autocmd BufNewFile * 0r ~/.vim/templates/%:e.template

        and then

        # in ~/.vim/templates/tmpl.template <!-- Start output from [% component.name %] --> <!-- End output from [% component.name %] -->

        and then whenever you open a new file in vim it will have the tags pre-inserted.

        Hope this is close to the mark of what you needed!

Re: Template Toolkit - Output Template Name / Filepath as HTML Comment
by jeffa (Bishop) on Jun 25, 2015 at 16:18 UTC

    Although i am quite sure this is possible (see Template::Provider) you probably do not want output these comments in your production environment as you will give clues away to any would-be attackers. Instead, why not just grep or ack for the markup in question? I have found this to be a very workable solution for tracking down templates. No need to add more code to your base.

    UPDATE: this might work for you, but i still recommend using grep or ack:

    use strict; use warnings; use Data::Dumper; use Template; my $file = 'src/greeting.html'; my $vars = { message => "Hello World\n" }; my $tt = Template->new; $tt->process($file, $vars); my ($found) = keys %{ $tt->{SERVICE}{CONTEXT}{LOAD_TEMPLATES}[0]->{LOO +KUP} }; print "$found\n";
    Just touch any file to src/greeting.html relative to the location of this script.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Thanks for the assist.

      To grep for parts of the markup is possible, but a lot slower
      when compared to just reading the template names inline.

      Basically, what I'd hope to see is something like this:

      ... <!-- BEGIN OUTPUT from 'template/en/bug/bug_edit.html.tmpl' --> <ul> <li># lots of markup ....</li> </ul> <!-- END OUTPUT from 'template/en/bug/bug_edit.html.tmpl' --> <!-- BEGIN OUTPUT from 'template/en/global/footer.html.tmpl' --> ...

      Strictly for development of course, none of this clutter would get anywhere near production ;-)

Re: Template Toolkit - Output Template Name / Filepath as HTML Comment
by codiac (Beadle) on Jun 26, 2015 at 10:03 UTC

    If you are lucky you can add PROCESS to the Template config in Bugzilla/Template.pm and it will run when ever you put in there whenever it processes a template.

    PROCESS => 'tricksy',

    and make templates/en/default/tricksy

    <!-- [% template.title %] --> [% PROCESS $template %]

    If you are not lucky, then you will need to sub-class Template::Service and set $Template::Config::SERVICE = $mysubclass; before the Template is instantiated.

Re: Template Toolkit - Output Template Name / Filepath as HTML Comment
by petdance (Parson) on Jun 29, 2015 at 18:32 UTC
    You may want to take a look at my Template::Timer module. Template-Timer

    It gives you output like this in your HTML:

    <!-- TIMER START: L1 process mainmenu/mainmenu.tt --> <!-- TIMER START: L2 include mainmenu/cssindex.tt --> <!-- TIMER START: L3 process mainmenu/cssindex.tt --> <!-- TIMER END: L3 process mainmenu/cssindex.tt (17.279 ms) --> <!-- TIMER END: L2 include mainmenu/cssindex.tt (17.401 ms) --> .... <!-- TIMER END: L3 process mainmenu/footer.tt (3.016 ms) --> <!-- TIMER END: L2 include mainmenu/footer.tt (3.104 ms) --> <!-- TIMER END: L1 process mainmenu/mainmenu.tt (400.409 ms) -->

    I know you didn't ask for timings, but at least it shows nesting levels.

    xoxo,
    Andy