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.


In reply to CGI::Application, Template Toolkit and global parameters by Fang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.