|Don't ask to ask, just ask|
Code and html separation - always or mostly doable?by kiat (Vicar)
|on Jun 16, 2004 at 10:16 UTC||Need Help??|
kiat has asked for the
wisdom of the Perl Monks concerning the following question:
I've been using HTML::Template for quite a while now. It has helped greatly in moving a lot of html (and sometimes logic, as when using tmpl_if) out of my code. The result is a more readable code as well as separation of code and html.
When the layout is simple, such as pulling rows of data from a db and displaying them on an html page, it's trivial to achieve a clean separation of code and html.
However, when the page's layout involves quite a bit of logic, I find it sometimes difficult to filter out the html from the code. What I tend to do in such cases is to build up an html string, and then feed that string, together with other tmpl outputs, to the template file.
Do you have that sort of problems? Or are you able to always cleanly compartmentize code and html? I would love to hear from you :)
Thanks in anticipation.
Update: Many thanks to all for sharing :)
It appears the majority find it always possible to separate the program code from the html. There's however some support for the inclusion of html in the code in cases where their separation proves difficult or impossible (in the eyes of the beholder).
Update2: Reading some of the comments, it seems that it's quite common to move the logic from the Perl code to the template (nodes 367304, 367252). amw1 says it's display logic vs processing logic. So it appears to me that some mixing of code and html is inavoidable. It's where that mixing takes place. If part of the logic is moved to the template, then you inevitably have a more complicated template. If the (display) logic resides in the Perl code, then you inevitably have a more complicated Perl code. So perhaps it's a matter of which type of complication you are more comfortable with?