Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

automatic Template component wrapper

by marvell (Pilgrim)
on Mar 10, 2006 at 15:52 UTC ( [id://535732]=perlquestion: print w/replies, xml ) Need Help??

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

Gracious monks ...

I use Template::Toolkit and make great use of the INCLUDE dirctive. My designer has asked me to wrap each component with a comment so he can see how the end pages have been constructed.

I can give him a wrapper for each component like this:

somecomponent.tt: <!-- BEGIN $component.name ($component.modtime) --> the content of the template <!-- END $component.name -->

Of course, I can use WRAPPER and pass these variables through to the commenting wrapper, but that is more text and still requires and END a the end of the template.

In an ideal world, I'd have a flag in my config to turn this on and off, which is easy, but is there a way I can may TT wrap all my components automagically?

--
Steve Marvell

Replies are listed 'Best First'.
Re: automatic Template component wrapper
by Aristotle (Chancellor) on Mar 11, 2006 at 06:38 UTC

    I haven’t verified this, since I’m too lazy, but my perusal of the docs suggests that what you want is one of the DEBUG_FOO options combined with a suitable DEBUG_FORMAT, as documented in Template::Manual::Config.

    Makeshifts last the longest.

      DEBUG_DIRS provides the informaiton I need and more. Cheers.

      --
      Steve Marvell

Re: automatic Template component wrapper
by Herkum (Parson) on Mar 10, 2006 at 16:44 UTC

    When you define a template object, you have a large number of settings you can use.

    What I suggest is that you declare the variables that you want to pass to the component. Then declare a POST_PROCESS which will process this template at the end of each page.

    my $template = Template->new({
        VARIABLES => {
    	component => {
                name => 'script_name',
                time => time(),
            }
        },
        POST_PROCESS => 'component_info.tt'
    });
    

    Now you will not have to declare a WRAPPER after each web page. It will be done for you.

      It's not the web pages, or whole templates I'm interested in, it's the components, ie. the bits included. For example:

      thing.tt: I am thing.
      thang.tt: I am thang.
      fred.tt: % INCLUDE thing.tt % % INCLUDE thang.tt %

      I ultimately want this from processing fred

      <! -- begin fred.tt --> <! -- begin thing.tt --> I am thing. <! -- end thing.tt --> <! -- begin thang.tt --> I am thang. <! -- end thang.tt --> <! -- end fred.tt -->

      In this way, my designer can see which template components have been used to create a specific page

      component is a special variable which is automatically set for each included component. The reason I said I had to pass it through is that it is set to the wrapper name if I don't pass it through from the component I want the information for.

      --
      Steve Marvell

Re: automatic Template component wrapper
by jeffa (Bishop) on Mar 10, 2006 at 16:55 UTC

    I would indeed use WRAPPER here instead of rolling my own:

    use Template; my $tt = Template->new; $tt->process(\*DATA, { comp => { name => 'foo', modtime => scalar(localtime) } }) || die $tt->error(); __DATA__ [% WRAPPER wrap comp = comp %] content goes here [% END %] [% BLOCK wrap %] <!-- BEGIN [% comp.name %] ([% comp.modtime %]) --> [% content %] <!-- END [% comp.name %] --> [% END %]
    Using WRAPPER may be a bit more wordy than your header/footer solution, but it also has the added advatange of escaping any content contained in the WRAPPER for you.

    Not that this information solves your problem, but HTML::Mason has an autohandler, which not only acts as a WRAPPER, but also gives you finer control at the directory lever: all Mason components in a directory are handled by the autohandler -- and can be used to wrap all components automagically. I haven't found how to do this in TT yet, but i would be surprised if you can't. Hopefully another monk will know the answer, have you tried the TT mailing list?

    Update: Herkum++ (that's what i get for getting pulled away from the problem and not refreshing to see if anyone had answered the question :P)

    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)
    
      Using WRAPPER may be a bit more wordy than your header/footer solution, but it also has the added advatange of escaping any content contained in the WRAPPER for you.

      It's also more wordy, becuase is has to have the special variable passed through.

      Again, you example is atthe template level, not the component. I'm looking for a sub-template oriented solution.

      --
      ¤ Steve Marvell

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535732]
Approved by jeffa
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found