Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Why use templates?

by BUU (Prior)
on Apr 17, 2002 at 20:22 UTC ( #159975=perlmeditation: print w/replies, xml ) Need Help??

(Wee, more rants about all-loved modules,hopefully this one will make a little more sense) Basically my questions is, why use templates any more? My understanding, is that templates are basically like php/perlscript(etc..), allowing you to embed code into static html pages, then parse the page, allowing dynamic content. I guess the real question is, why is this needed any more? I could see back when people were still producing horribly abused, mangled, twisted, and just generally crappy html to make their nice neat designs (most notable abusing tables..). I could see if you had a page consisting of 3000 table tags you would want to seperate out the 'content' and the 'style'. But that leads me to today. No longer do we have to horribly mangle html. Which leads me to my arguement against templates. Namely: CSS. Use html exactly as its meant to be used, as a 'definition language'. You can just say, my code will produce output with the links in A tags, and the body in P tags. Then the 'client' can use style sheets to display the information however he wants. Think about it. (i guess there may be a few specific cases where a template is the best, but i frankly cant think of any now so..)

Replies are listed 'Best First'.
Re: Why use templates?
by ignatz (Vicar) on Apr 17, 2002 at 20:46 UTC
    A template is as useful as any other programming abstraction. It's a seperation of roles. Form from content. What is the same from what is different.

    If you have your basic web page with it's standard parts (head, top navbar, body, bottom) why type these things over and over again? Templates add power and flexibility. If your company goes from to all you have to do is change a few variables. Granted CSS does in many ways simplify things, but a complex web portal is more than just the color of the background.

    I generally seperate parts of a web site into content (nodes in PM), packaging (look and feel) and components (search boxes, nodelets, etc). Templates greatly facilitate the use of these parts by keeping each of them seperate. I can move my search box or my breadcrumb bar all over the place, or change the way it looks, or whatever, without worrying about how these changes will effect the content of my site.

    (Added) A programming design pattern that is used as a template for these sort of designs is the Model-View-Controller.

      `                                                   ` 
Re: Why use templates?
by perrin (Chancellor) on Apr 17, 2002 at 21:38 UTC
    CSS can't help factor out the "boiler-plate" text that surrounds the actual data (e.g. the word "Price:" to the left of a product price). CSS doesn't have conditionals, so I can't do things like make the price print out in red if it's less than $20. CSS can't do loops, so I can't print out the first three related products. (I could have my main program limit the list to 3, but then if that requirement changes to show 5 instead, I would have to change it in my program.) The list goes on. CSS can only change the appearance, not the actual structure of the page. In short, CSS isn't powerful enough to completely remove knowledge of the page layout from your program, but templates are.
Re: Why use templates?
by particle (Vicar) on Apr 17, 2002 at 20:56 UTC
    seperation of code and design. web designers play with html, web programmers play with perl, and an interface is designed between them (the template.) this seperation improves the development and maintenance processes for large development teams.

    as to CSS for a solution--the simple reason it's not used everywhere: netscape 4 and ie 3.

    neither of those browsers support css in any standard way. ie6, netscape6, and opera6 have much better support of css 1.0, and some of css 1.1, but they're not widely used yet.

    if you're interested in more info on css, check out a list apart

    ~Particle ;

      as to CSS for a solution--the simple reason it's not used everywhere: netscape 4 and ie 3.

      That's not the point, though... (I think you know that, but the BUU may not). CSS controls the L&F. Templating is about the factoring out the presentation layer ( which in this case, should be factored out again with CSS to separate the definition with L&F ) from the code logic....

Re: Why use templates?
by Kanji (Parson) on Apr 17, 2002 at 21:02 UTC
    Which leads me to my arguement against templates.

    Your argument is flawed because it assumes people only use templates to build HTML, when templating is applicable to so much more.


      Very good point. At a previous job I used perl to generate m4 (a `traditional Unix macro processor'), arguably a form of templating. The m4 was part of the Solaris package installation process for the product and was executed on the machine upon which the software was being installed (otherwise I probably would have just sent perl, this being in the days before perl shipped stock with Solaris :).

      Recently I've taken to using Template Toolkit in place of m4 for generating config files based on dynamic data (network monitoring using Nagios for 70+ (and growing quickly) remote boxen and routers). There's nothing remotely HTML related about it, it's just much simpler for me to say `type this to add a new host':

      [% host( name="foo", addr="" ) %]

      than it would be to make everyone learn how to (reliably, without typos) produce:

      define host{ host_name foo alias foo address check_command check-host-alive max_check_attempts 3 process_perf_data 0 retain_nonstatus_information 0 notifications_enabled 0 notification_interval 120 notification_period 24x7 notification_options d,u,r,n }

      (Yes, I'm aware Nagios has a form of templating internally, but that can't be driven externally with perl logic. This way I can make the macros smarter (e.g. you may see a definition for host bar, but it won't be live yet so there's no point in monitoring it so don't output anything).)

Re: Why use templates?
by shotgunefx (Parson) on Apr 17, 2002 at 21:06 UTC
    CSS is iffy. Our business is mainly putting companies products on the web for sale. Our view is that if you want someone to purchase from you, you should make as few demands as possible and remove as many barriers to purchase as your can.

    All of our sites will work and look at least decent on 3.0 up browsers. Even Web TV doesn't look half bad. When at some point, CSS implementation is matured enough and is ubiquitous we will start using it.

    Our customers have on average around 6000 products. Using a templating system makes it a breeze to update the look and feel for non-technical people.

    I think of it in the same way as Javascript vs Server Side processing. Server Side is almost always an order of magnitude more consistant and reliable way to go.


    "To be civilized is to deny one's nature."
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Why use templates?
by mr_mischief (Monsignor) on Apr 17, 2002 at 23:04 UTC
    There are lots of good arguments already about why templating is still useful despite CSS. I haven't seen the simplest, most direct, and one of the most important reasons said...

    CSS controls how a section of a page looks, and where those sections go. It doesn't separate the content into those sections. I can use CSS to make a paragraph pretty, to put a sidebar down the right of my page, and to make my header big and bold in a pretty color. I can't use it to divide the content into multiple <p> tags, to assign the main content to the main page body and its sidebar info into my sidebar. That's templating work.

    Specifically, before the last redesign of my website, I had a CGI that grabbed a story from a file, a sidebar from a different file, picked the interesting facts at the bottom of the sidebar randomly from a set of other files, and displayed it all using HTML and CSS. Next time I redesign, it'll be mostly static pages, but still generated through a template system (and probably a database for content next time instead of flat files).

    Update: Fixed a spelling error and an accidental StudlyCaps usage.

    Really late update (2008-09-25): added code tags around the <p> tag to fix the markup.

Re: Why use templates?
by tmiklas (Hermit) on Apr 18, 2002 at 05:43 UTC
    Web pages are to pass the information to others (in one way or another), templating system (whatever it would be) is just to provide logical content - something that gives the sense of existance to this page and the way the page is shown has nothing to do with templates IMHO. There actually *is* a template to fill with content - that's all.
    How does this template look?! It depends on who prepared it. I prefer the KISS rule - light background, without any special effects, without any special graphics, and almost never use CSS (i did once in my life ;-p). When i look at those simple pages i can always find what i'm looking for, without loading MB's of pictures, backgrounds, icons, etc...
    Templating with Perl or anything else privides content, while CSS is ment to provide 'more inteligent' look ;-) That's all!

    Greetz, Tom.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://159975]
Approved by Ovid
Front-paged by IlyaM
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-08-14 04:33 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (75 votes). Check out past polls.