Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

When to use templates?

by kidd (Curate)
on Nov 19, 2004 at 15:12 UTC ( #409012=perlquestion: print w/replies, xml ) Need Help??
kidd has asked for the wisdom of the Perl Monks concerning the following question:


I've been reading a lot of articles which talk about the best template systems in perl and how to use them. This articles give you the pros a cons of the most popular templating systems in perl (TT, Mason, Embperl, etc).

But then a question entered my mind and I've been trying to find the answer to it, right now without success.

When to use templates? When programming a CGI, should you always use templates? When is not a good idea to use templates?

Im looking to improve the perfomance of my CGI's, 'cause the main use I have with perl is Web-based programming. I hope some can share some knowledge with me, to help me find the best way to program my CGI's.

Thanks in advanced to all

If your a spanish spoken programmer go to my site: Perl en Espaņol

Replies are listed 'Best First'.
Re: When to use templates?
by dragonchild (Archbishop) on Nov 19, 2004 at 15:26 UTC
    Let's get one thing out of the way first - the single largest speed boost for Perl web applications is mod_perl. Everything else is second.

    My rules of thumb for using templates are:

    • If I intend on making more than two changes to any part of the system in the next year, I will want templates.
    • If I ever want to output the same page in more than one format, I will use templates.
    • If I ever want to support more than one language, I will use templates.
    • If I ever want to have someone else working on the same project, I will use templates.
    • If I'm going to use any JavaScript, I will use templates.

    In other words, I will not use templates if:

    • I will never make any changes
    • I will only ever use one format
    • I will only ever use one language
    • I will be the only person to ever work on this
    • I will not use any JavaScript whatsoever

    In other words, I have never seen a reason to avoid templates. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: When to use templates?
by Mutant (Priest) on Nov 19, 2004 at 15:41 UTC

    Except for very basic (probably one-shot) scripts, I always use templates for HTML. I usually use HTML::Template, which has low overhead (more in the sense of programming time than computer time), and is very simple to use. I can't think of too many cases where I wouldn't want to use HTML::Template (or another templating system).

    The main reason is getting the HTML as far away from your code as possible. Once you do this, a lot of problems just don't exist anymore.

    While sometimes templating systems can give you a performance hit (not that this is usually too important - network speed is far more of an issue for the vast majority of users hitting your scripts) they often make it easier to tune performance if you need to.

      I agree with Mutant. However, sometimes HTML::Template is quite limited (especially for complex "IF" statement).

      I'd advise to extend HTML::Template by using HTML::Template::Expr, that is fully compatible with the former (through object heritage I guess), but allows you to do more.


        *Purest Alert*

        Why do you need complex IF statements in your template? samtregar's idea for HTML::Template was to separate the two. If you introduce complexity into your template you create a Meta-language where code and data are again mixed. And designers get confused :)

        I believe its much better to leave complex stuff in Perl which is very good at handling it and output the appropriate data to the template from there.

        I do make use of HTML::Template's simple IF statements (and I wish there was an elseif) but beyond them I don't think its appropriate to further extend a meta-language.

        I think if it gets to the point where you really need more complex functionality, then you may want to look at Mason or Template Toolkit (or whatever you choose). The limitations of HTML::Template are by design.

Re: When to use templates?
by tilly (Archbishop) on Nov 19, 2004 at 19:38 UTC
    Templating is a good solution, but you have to define your problem first. If you just use it and don't have a good sense of why they are good, then you'll "use" them but not in a way that solves any problem - and then they'll seem useless.

    Re (tilly) 6: Code Critique tries to give an overview of why templating is good, what you need to do to make it succeed, and what you can hope for from it if it succeeds. Unfortunately it tends to be the case that you can hear all of the advice in the world but it won't "digest" properly until you've actually tried it in practice and you have some experience to put it into context. So the first time you try to use templating to clean up your code you probably won't realize the benefit that people talk about getting - but you will get the experience to be able to figure out what you could do differently to get a better benefit next time. (Which is still valuable. Plus you'll likely still get some benefit.)

Re: When to use templates?
by kappa (Chaplain) on Nov 19, 2004 at 16:15 UTC
    BTW, trying to figure out the reason of your question I can say that templates usually DO NOT impose any performance overhead at all. You won't lose anything. Most of the time, templated output will even speed you up by itself, because there's buffering, there's precompiling and all that staff.
      You are right, that was my main concern. That by using templates I would get a hit in perfomance. Specially when using CGI's that generate dynamic content. Thanks

      If your a spanish spoken programmer go to my site: Perl en Espaņol
      I've found that using TT does impose an overhead on CGI scripts, because every time the TT modules have to be recompiled, etc, which might take around 0.2 s (depends on the machine). But if you use mod_perl or speedycgi there is no problem.
Re: When to use templates?
by freddo411 (Chaplain) on Nov 19, 2004 at 18:08 UTC
    When to use templates? When programming a CGI, should you always use templates? When is not a good idea to use templates?
    Reasons to use Templates
    • You can edit templates in a GUI
    • You can seperate code (and coders) from the art (and artists)
    • Lots of other reasons, explained in the other messages

    Reasons NOT to use Templates
    • Keep everything in one file
    • Don't want to mess with modules
    • Writing very plain vanilla cgi with no decoration
    • Prototyping, so your script is very minimal
      Even then I sometimes use a template that contains only a pre tag.

    I use templates 95% of the time. Definitely a "best practice". My template of choice is HTML::Template because is it is so simple.



    Nothing is too wonderful to be true
    -- Michael Faraday

      Keep everything in one file

      There is no reason a template has to be in an external file. With HTML::Template, you can embed your template into the __DATA__ section, and pass DATA as the template filehandle. I do this often, and it works well. I'm sure other template modules have similar methods available.

      I tend to think of a template somewhat like a Perl module. Usually, it's cleaner and better to put these things in separate files. Sometimes, though, embedding them into the file where they're used is far simpler, while still retaining much of the benefit of using modules/templates in the first place.

Re: When to use templates?
by johnnywang (Priest) on Nov 19, 2004 at 17:51 UTC
    I use TT in almost everything I do with perl, not just web applications. I do lots of offline processing, whenever I need to output something, I try to use TT.
Re: When to use templates?
by rrwo (Friar) on Nov 20, 2004 at 12:52 UTC

    Templates allow you to separate your code from the content (web page, E-mail, form-letter, report, etc.). You can submit the template to a non-programmer to work on, or even give the non-programmer access rights to modify the template.

    It also allows for code re-use. Rather than having multiple scripts that generate similar output (such as the results of an SQL query), you have one script with several templates. This makes maintenance so much easier!

Re: When to use templates?
by kidd (Curate) on Nov 20, 2004 at 16:59 UTC
    Thanks for all your advice. It really has helped me a lot.

    As I said before, one of my first concerns when using templates was the hit on perfomance, but as I see it really doesn't happen, and I really like the idea of having the design part of the CGI in a differente file, which can be very friendly to webmasters.

    Im going to try templates, and in the future I will tell you how it went.

    Thanks again for all your advice... ;-)

    If your a spanish spoken programmer go to my site: Perl en Espaņol

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://409012]
Approved by Joost
Front-paged by skx
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-06-25 00:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.