I'm coming from a python CGI background and was wanting to move into something more contemporary and think I have decided upon web.py
as the framework I would like to use. In regards to templating, previously I used formatted strings and the string.Template
module to effect most of my templating needs. After reading through a few of the templating options I have heard mentioned, I began wondering what the main benefits of using something like the Django
or jinja
templating options over "native" Python templating options were? Am I just going to be replacing $tmpl_var
with {{ tmpl_var }}
and s.substitute(tmpl_var=value)
with t.render(s)
, i.e., alternate syntax? or will I gain additional advantages from using these templating systems?
-
Syntax highlighting, for one...– IzkataCommented Nov 24, 2012 at 2:33
1 Answer
The first thing that you will notice is that if you want to have a repeated template (for instance think about having search results, with a block of text per entry), the solution that you're used to requires you to build up an array in Python, and then build up the final output. But any decent templating system will let you code it inline, making it much easier to scan through the template and get a direct idea of the final output.
However if you talk to any experienced programmer, the first thing that they will cite is that you get the opportunity to separate presentation from content. Just having different types of things in different places helps you organize your code better. Read virtually any introduction to MVC (a random Google search said that http://tomdalling.com/blog/software-design/model-view-controller-explained/ was useful for some) for more on how you might want to organize your code.
To get a more precise idea what templating systems do and how they work, I really like http://perl.apache.org/docs/tutorials/tmpl/comparison/comparison.html. Ignore all of the specific templating systems it discusses - they are all old systems for a different language. But the first half of it provides a lot of context to understand the differences between different kinds of templating systems that you will run across in many languages. And likewise to see the similarities between apparently different template systems.