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 | |
by Fang (Pilgrim) on Nov 06, 2005 at 11:55 UTC | |
|
Re: CGI::Application, Template Toolkit and global parameters
by cees (Curate) on Nov 06, 2005 at 23:29 UTC |