17

I'd like to fill a HTML template with data from Python. While I don't know any HTML, a colleague of mine would prepare the HTML template whereas I provide the data. However, he knows only little about Python.

Could you suggest a very basic template framework which handles this separation of HTML and Python nicely (i.e. not much special syntax in HTML; and not much HTML for me to know in the Python data code)? Ideally the HTML template should be very close to pure HTML.

I will also have tables as data, so it would be nice if I didn't have to prepare HTML table code, but instead he could do it with some minimal markup logic. Ideally the system should work even if my colleague applies more advanced concepts like CSS.

I've found the extensive list, but it's really hard to judge which of the system is closest to my requirements as I don't know the technical language.

EDIT: the point is we are newbies with that, and we only need a basic solution :) - yet ideally no HTML for me and not much beyond plain HTML for him.

3
  • 1
    Chameleon and Zope Page Templates (both implement the same attribute language) provide this; clear separation and templates that still work in a browser unrendered. Unfortunately, this kind of question does not fit the format of this site, see the FAQ. Questions like these tend to lead to vague answers that are also outdated very quickly. If we can help you with a specific problem, feel free to post another question though! Commented Jul 25, 2012 at 9:44
  • Thanks for the explanation. The specific requirement that it should be easy to set up for complete newbies should restrict the possible answers... provided everyone reads all of the question. I don't know how to reformulate it :(
    – Gere
    Commented Jul 25, 2012 at 9:51
  • 1
    Also you can take a look at "Mako" and "Jinja2". If you need more than just the HTML renderer take a look at Flask (its very small and provides nice integration for Jinja and Mako).
    – javex
    Commented Jul 25, 2012 at 9:52

3 Answers 3

24

Well django has very nice powerful templating engine, that's purpose is separating HTML from python logic (but that would require you to use django altogether, so it might be an overkill if you just want templating).

If your templates are really easy (no loops) you might use native python string.format function (I used it in a side project that generated config files to some Fortran code, and it wasn't so bad).

Other choice might be jinja2 that is really close to django templates, but standalone and more powerful.

2
  • Thanks. string.format addresses the needed simplicity, however it might be too simple, as I still would have to generate HTML table markup on the Python side. That's the only reason why I didn't go for the simplest solution.
    – Gere
    Commented Jul 25, 2012 at 9:53
  • 2
    Just tried Jinja2 after reading your answer @jb: it is really awesome. Thanks a lot! Commented Dec 1, 2018 at 15:45
6

Might I suggest looking at web2py as well? At the most basic level you could simple have your colleague create the html file then where ever you need data replace it with {{=var}} the var can be retrieved by the web2py controller which is written in a python function. From the function you could put together your data and return it the html view with "return dict(var=var)".

If your view looked like this:

<html>
    <head>
        <title>some title</title>
    </head>
    <body>
        <h1>{{=message}}</h1>
    </body>
</html>

The controller could look like this:

def index():
    message = "Hello World"
    return dict(message=message)

You could also use django, as other have mentioned but check the link for some web2py documentation

1
  • If the OP is looking for controller as well (not only templating engine) I strongly suggest using django.
    – jb.
    Commented Jul 25, 2012 at 10:40
4

The simple template in python is: https://docs.python.org/3.6/library/string.html#template-strings

It comes with the language, no need for external libraries.

Here is an example of how to use a Template:

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'
1
  • Please don't use link only answer. Please provide some example with context. In the future the link may not work, and your answer would be useless Commented Nov 7, 2020 at 19:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.