views:

49

answers:

4

I am building a perl cgi::application using html::template.

I am using 7-8 different templates having the same layout - header, footer, left column etc. How can I separate this html out of the template files into a single layout file. What perl modules do I need in addition to cgi::app and html::template.

Thanks

+1  A: 

I'd switch out HTML::Template for Template-Toolkit and make use of it's WRAPPER directive.

David Dorward
+6  A: 

I agee that Template-Tookit is better.

If you absolutely have to use HTML::Template you can use the TMPL_INCLUDE directive. It'll search your defined template paths or you can specify a full path to another template. It'll process the variables in it as well.

You can create seperate template files for the header, footer and such and in your page templates just TMPL_INCLUDE them. It's less elegant and more repetative than Template Toolkit's WRAPPER (You'll have to TMPL_INCLUDE in each page several times for all shared elements) but it'll get the job done.

If you can, invest the time and use Template Toolkit.

Dave Doyle
This did the job! I'll check out the toolkit - thanks
Prateek
A: 

Can the people who don't like HTML::Template please say why? While the Wrapper idea seems helpful to this particular poster, there's nothing wrong with the idea of includes: they're more flexible, and many web developers will already be familiar with the concept from non-dynamic publishing.

AmbroseChapel
HTML::Template is just very limited. Template Toolkit is much more flexible. There are some that argue that Template Toolkit does too much (and it can... there's a lot of logic you can squeeze into it if you're inclined) but it's just a better system. Particularly, you can pass objects in whereas you can't with H::T. There's many more formatting options in TT and many extensions to provide formatting and filters.
Dave Doyle
Interesting ... I guess I will keep on using HTML::Template until I find something it can't do. When I looked at TT, it seemed like learning another whole sub-language.
AmbroseChapel
+1  A: 

I don't know about Template-Toolkit. So i won't discuss about which solution is the most convenient. I can just give you another solution, which is dependant of the server your running your cgi's on. With Apache server, you can use includes in your html :

<!--#include virtual="/includes/header/header.htm"-->

you may call htm (static pages) as well as dynamic pages :

<!--#include virtual="/perl/includes/dynamic.pl"-->

but you have to do some apache tweaking. see Apache Tutorial: Introduction to Server Side Includes

Hope this will help, or at least give some ideas

benzebuth