Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

CGI::Application, Template Toolkit and global parameters

by Fang (Pilgrim)
on Nov 06, 2005 at 11:23 UTC ( [id://506098]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am currently discovering the bliss that is CGI::Application, though I have just bumped into a small issue.

Using CGI::Application::Plugin::TT, I built a nicely running start of my project. After I had 4 run modes, I wanted to make the navigation between them and future modes easier with the addition of a simple menu at the start of every page. Here are the related config and template files:

# The CGI::Application::Plugin::TT configuration __PACKAGE__->tt_config( TEMPLATE_OPTIONS => { INCLUDE_PATH => '/path/to/templates', WRAPPER => 'wrapper.tmpl', PRE_CHOMP => 2, POST_CHOMP => 2, }, ); # wrapper.tmpl [% WRAPPER html.tmpl + layout.tmpl; content; END; %] # layout.tmpl <div id="container"> <div id="menu"> [% PROCESS main_menu.tmpl %] </div> <div id="main"> [% content %] </div> </div> # html.tmpl is just a simple <html></html> wrapper, nothing relevant t +o my issue

So, the heart of the problem is how I can fill the main_menu.tmpl template. At first, I thought about using constants, adding the definition of the menu to the TEMPLATE_OPTIONS hashref, but it didn't work.

__PACKAGE__->tt_config( TEMPLATE_OPTIONS => { # same as above, plus: CONSTANTS => { menu => [ { text => 'Foo', link => 'foo.html', }, { text => 'Bar', link => 'Bar.html', }, ], foo => 'foo!', }, }, ); # main_menu.tmpl <ul> <li>[% constants.foo %]</li> [% FOREACH constants.menu %] <li><a href="[% link %]">[% text %]</a></li> [% END %] </ul>

This results in a list with only one item, 'foo!'. I didn't read anything in the Template Toolkit doc about user-defined constants needing to only be strings or numbers, but I guess it has to.

I then tried another way:

__PACKAGE__->tt_config( TEMPLATE_OPTIONS => { # same as the original one, plus: PRE_PROCESS => 'config.tmpl', }, ); # config.tmpl, in case I have to define more site-wide parameters in t +he future [% PROCESS menu_def.tmpl %] # menu_def.tmpl [% site.menu = [ { text => 'Foo', link => 'foo.html', }, { text => 'Bar', link => 'Bar.html', }, ]; %] # main_menu.tmpl obviously becomes <ul> [% FOREACH site.menu %] <li><a href="[% link %]">[% text %]</a></li> [% END %] </ul>

That last one works as expected, however I wouldn't be posting here if I didn't have any question, so are there other, more standard ways of doing what I'm trying to achieve? Or anything I missed about the usage of user-defined constants in TT?

Thank you for your time and help.

Replies are listed 'Best First'.
Re: CGI::Application, Template Toolkit and global parameters
by szbalint (Friar) on Nov 06, 2005 at 11:44 UTC
    Since you process the main_menu.tmpl, it acts like it is defined in the Template toolkit Manual.

    Basically if you 'PROCESS' a template, it can modify the current variable stash, it does not use localization. So it sees and can modify all the variables you "have"(can use) in layout.tmpl in your first example (you pass the variables when you call $self->tt_process() for example in your perl code).

    If you want to use it in a constant-like form though, why not just merge menu.def.tmpl and main_menu.tmpl together?

      If you want to use it in a constant-like form though, why not just merge menu.def.tmpl and main_menu.tmpl together?

      I guess that is one of those obvious solution you can only react to with "why didn't I think of that sooner?". Thanks for the suggestion.

Re: CGI::Application, Template Toolkit and global parameters
by cees (Curate) on Nov 06, 2005 at 23:29 UTC

    You already have a couple of options that you can use, but CGI::Application::Plugin::TT gives you some extra possibilities as well. You can create a 'tt_pre_process' method in your CGI::Application module, which will be called automatically everytime you call $self->tt_process. This is useful for adding extra global variables that you want to appear in every template you parse in your application.

    sub tt_pre_process { my ($self, $file, $vars) = @_; $vars->{user} = $ENV{REMOTE_USER}; $vars->{menu} = [ { text => 'Foo', link => 'foo.html', }, { text => 'Bar', link => 'Bar.html', }, ]; return; }

    Alternatively, you can setup a tt_pre_process method as a callback (requires CGI::Application 4.0 or greater).

    __PACKAGE__->add_callback('tt_pre_process', sub { my ($self, $file, $vars) = @_; $vars->{user} = $ENV{REMOTE_USER}; $vars->{menu} = [ { text => 'Foo', link => 'foo.html', }, { text => 'Bar', link => 'Bar.html', }, ]; return; });

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-12-08 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.