There is no reason to have a one-to-one relationship between templates and pages. I use different templates for each section of my pages. Use one for your standard header and footer and one for each main body portion. If your body can be broken down into multiple logical units, represent each in a separate template. (But I think this is heading in the other direction - this will create more templates, not fewer)
The real advantage to HTML::Template is separation of form and function. Your code resides in your perl and is edited by a programmer. Your presentation resides in your template and is edited by a web designer.
It's also easier to edit HTML in a template than editing CGI generated HTML. If you just want that amount of separation, you should be able to embed your HTML in your perl by placing it after the __DATA__ marker.
---
print map { my ($m)=1<<hex($_)&11?' ':'';
$m.=substr('AHJPacehklnorstu',hex($_),1) }
split //,'2fde0abe76c36c914586c';
| [reply] [d/l] |
Do I actually have to have multiple templates?
No, you can include multiple templates within a single file using
<TMPL_IF template1>...</TMPL_IF>
<TMPL_IF template2>...</TMPL_IF>
etc.
but it isn't generally advisable. I keep mine separate template files.
I guess what I'm trying to say is that I don't see the benefit of using HTML::Template if I have X number of templates, where each has to be edited manually later.
Some might call that an advantage. If you have a lot of repetive code, <TMPL_INCLUDE> can ease the pain.
| [reply] [d/l] [select] |
| [reply] |
Aristotle,
That actually makes sense to me .... I was wondering if you could explain a little more in this regard.
Would greatly appreciate it.
Thanks,
Surya
| [reply] |
Just what is unclear? There's not much to explain I think, so I'm not sure what to clarify. What you'd do is basically something like this:
# ...
my @valid_page = qw(order cart shelf intro register);
my %collected_data = (foo => 1, bar => 2, );
my $requested_page = $cgi->param('page');
my ($actual_page) = grep $requested_page eq $_, @valid_page;
print("Content-type: text/plain\n\nNo valid page requested."), exit
unless $actual_page;
my $container = HTML::Template->new_file('container.tmpl');
my $template = HTML::Template->new_file($actual_page . '.tmpl');
$template->param(%collected_data); # fill in actual page
$container->param(
%collected_data,
CONTENT => $template->output, # insert into container
);
print "Content-type: text/html\n\n", $container->output; # we're done
You would then have order.tmpl, cart.tmpl, shelf.tmpl, intro.tmpl, register.tmpl and container.tmpl in a directory, and the latter would have a <TMPL_VAR NAME="CONTENT"> somewhere, so that the other template's output is inserted at that point. If you need any pointers, tell me what you have trouble with.
Makeshifts last the longest. | [reply] [d/l] |
Ever thought of having your PERL-script spit out XML which then gets transformed into HTML or XHTML by XSLT? I find it the cleanest way to separate logic and form. CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] |
Question? How well does XSLT handle complex logic? I myself
have been dabbling in the arts of XML/XSLT transformation,
and it just seems:
- too slow (i anticiapte faster processing the future
however)
- possibly incapable of handling very complex decisions
(for example, lot's of nested if-else conditions and loops)
- only good when you need to transform XML to more than
just HTML
I have done quite a bit
with XSLT, but nothing that involved complex decision
trees. I am curious as to which is really the
"cleanest way." (and so far ... i still don't get why
TemplateToolkit is as popular as it is.)
jeffa
"PERL" ne
"Perl"
| [reply] |
I have used XML and XSLT to present some insurance statistics. A PERL-script collects the statistical data out of the database and sends it out as XML. It then gets transformed to HTML in nice tables and graphs, sums and ratios are calculated, ... all by XSLT. I now rarely have to change the PERL-scripts and can do most of the work through the XSLT-scripts. CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] |