Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Template Toolkit 2 and subroutines

by marcink (Monk)
on Oct 09, 2001 at 17:06 UTC ( [id://117720]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

is it possible to define a subroutine within a template? All the examples I've seen so far assume that the template is being processed from a piece of perl code that prepares variables, while I'd prefer to stick with ttree.

What I'm looking for is some kind of tt2 equivalent of named PERL blocks that are planned for tt3:

[% PERL my_own_func %] ..function code.. [% END %] [% my_own_func(args) %]

Thanks,

-marcink

Replies are listed 'Best First'.
Re: Template Toolkit 2 and subroutines
by merlyn (Sage) on Oct 09, 2001 at 17:51 UTC
    I find it very useful when trying to write inline Perl to turn on $Template::Parser::DEBUG and look at the resulting code for a similar construct. Here, I did it for:
    [% MACRO my_own_func BLOCK %] ... [% END %]
    because that's close to what you want. The resulting structure looks something as if you would say:
    [% PERL %] $stash->set('my_own_func', sub { ... }); [% END %]
    Of course, the real thing I've gotta ask is why not just create a Plugin, or even just a coderef in the initial stash? Embedded Perl kinda defeats the whole point of TT.

    -- Randal L. Schwartz, Perl hacker

Re: Template Toolkit 2 and subroutines
by perrin (Chancellor) on Oct 09, 2001 at 17:54 UTC
    There are all kinds of things you CAN do in TT, but that doesn't mean you should do them. It sounds like you want macros (which are in the docs), but if these are doing actual Perl processing (as opposed to just encapsulating a display rule) you'd be better off doing it before running the template or at least putting it in a plugin.

    If you start shoving lots of subroutines and stuff into your templates, you'll lose the separation that is the whole point of TT. On the other hand, if you really think this is a more appropriate way to write your templates, maybe you should use an in-line Perl templating tool like Text::Template.

      There are all kinds of things you CAN do in TT, but that doesn't mean you should do them.

      I'm quite well aware of that, but thanks for reminding anyway ;)

      What I want to do is really simple output processing (like substituting   for spaces in menus). I think that's something I can safely do in a template -- if not, I'll be happy to hear why before I go too far in a wrong direction :)

      I checked merlyn's examples. Here are the results:

      Template:

      --cut here-- [% MACRO tr_test BLOCK %] join ':', @_; [% END %] [% PERL %] $stash->set( 'tr_test_2', sub { join ':', @_ } ); [% END %] 1: [% tr_test( 'test', '1' ) %] 2: [% tr_test_2( 'test', '2' ) %] --cut here--


      And the result:

      --cut here-- 1: join ':', @_; 2: test:2 --cut here--


      So the latter does exactly what I need. Thanks for both answers.

      -marcink
        What I want to do is really simple output processing (like substituting   for spaces in menus).

        Isn't that what FILTERs are for?

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

        What I want to do is really simple output processing (like substituting &nbsp; for spaces in menus). I think that's something I can safely do in a template -- if not, I'll be happy to hear why before I go too far in a wrong direction :)
        Oh, then be sure to read Template::Filters to see how to do that properly. In fact, there might already be a filter in the standard collection to do that!

        -- Randal L. Schwartz, Perl hacker

        In addition to the use of filters, you can pass a reference to any sub in as part of the stash. I sometimes pass in a URI escape subroutine from Apache::Util and use it like this in a template: [% uri(foo.bar) %]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found