Having a dispatch table to handle multiple operations is the basic strategy. But I think you want,
my %dispatch = ( home => \&home, donate => \&donate, news => \&news, faq => \&faq, .... );
instead of
my %dispatch = ( home => sub { \&home(); }, donate => sub { \&donate(); }, news => sub { \&news(); }, faq => sub { \&faq(); }, .... );
So you can say, for example,
my $default_action = 'home'; my $action = $cgi->param('a') || ''; # is $action recognized? my $executor = $dispatch{$action} || $dispatch{$default_action}; &$executor();
When you specify \&some_func(), you actually mean to call some_func() and make a reference of whatever it returns. And that reference is what you get when you dispatch, for example, $dispatch{home}->(). Some thing I'm sure not what you want.
Since I can't put a template inside a template
What's your exact needs regarding this? If you have another html file, wheter is static or another template, you can use the <tmpl_include> command. This way, you need to process the placeholders in the included template file at the same level of the main template file. See the manpage for details.
... some html.... <tmpl_include name="other-template.html"> <tmpl_include name="other-static.html">
If what you need is some kind of multi processing template then you need to work this out yourself. One way is by having a number of template objects according to the number of included templates.
<!-- news.html --> ...some placeholders here... <!-- end of news.html --> <!-- faq.html --> ... placeholders for FAQ ... <!-- end of faq.html --> <!-- main.html --> <html> .... Today: <tmpl_var name="today"> <p><tmpl_var name="news"> <p><tmpl_var name="faq"> ... </html> <!-- end of main.html -->
Now, you have to process the news.html and faq.html separately, getting their outputs and putting it in their respective placeholders when processing main.html. Something like (imaginatively),
# assuming load_template() is defined somewhere # to return a template object my $faq = load_template('faq.html'); $faq->param(PARAMS_FOR_FAQ_PLACEHOLDERS); my $news = load_template('news.html'); $news->param(PARAMS_FOR_NEWS_PLACEHOLDERS); my $template = load_template('main.html'); $template->param( today => scalar(localtime), news => $news->output, faq => $faq->output, ); print $template->output;
Having said all of that, I really like to suggest you to try CGI::Application. It would make your life easier :-) It shifts the dispatch table to some higher extend.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: Help with Template Workflow by naikonta
in thread Help with Template Workflow by hacker

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.