summaryrefslogtreecommitdiff
path: root/docs/index.txt
blob: a9b68144f10285da87d3cdb0b5ea7289607292c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
Tempita
+++++++

.. toctree::
   :maxdepth: 1

   license
   modules/tempita

.. contents::

:author: Ian Bicking <ianb@colorstudy.com>

Status & License
================

Tempita is available under an `MIT-style license <license.html>`_.

It is actively developed, but not an ambitious project.  It does not
seek to take over the templating world, or adopt many new features.
I just wanted a small templating language for cases when ``%`` and
``string.Template`` weren't enough.

Discussion should go the the `Paste-users mailing list
<http://pythonpaste.org/community/mailing-list.html>`_ and bugs in the
`Paste Trac instance <http://trac.pythonpaste.org/>`_.

Why Another Templating Language
===============================

Surely the world has enough templating languages?  So why did I write
another.

I initially used `Cheetah <http://cheetahtemplate.org/>`_ as the
templating language for `Paste Script
<http://pythonpaste.org/script/>`_, but this caused quite a few
problems.  People frequently had problems installing Cheetah because
it includes a C extension.  Also, the errors and invocation can be a
little confusing.  This might be okay for something that used
Cheetah's features extensively, except that the templating was a very
minor feature of the system, and many people didn't even understand or
care about where templating came into the system.

At the same time, I was starting to create reusable WSGI components
that had some templating in them.  Not a lot of templating, but enough
that ``string.Template`` had become too complicated -- I need if
statements and loops.

Given this, I started looking around for a very small templating
language, and I didn't like anything I found.  Many of them seemed
awkward or like toys that were more about the novelty of the
implementation than the utility of the language.

So one night when I felt like coding but didn't feel like working on
anything I was already working on, I wrote this.  It was first called
``paste.util.template``, but I decided it deserved a life of its own,
hence Tempita.

The Interface
=============

The interface is intended to look a lot like ``string.Template``.  You
can create a template object like::

    >>> import tempita
    >>> tmpl = tempita.Template("""Hello {{name}}""")
    >>> tmpl.substitute(name='Bob')
    'Hello Bob'

Or if you want to skip the class::

    >>> tempita.sub("Hello {{name}}", name='Alice')
    'Hello Alice'

Note that the language allows arbitrary Python to be executed, so
your templates must be trusted.

You can give a name to your template, which is handy when there is an
error (the name will be displayed)::

    >>> tmpl = tempita.Template('Hi {{name}}', name='tmpl')
    >>> tmpl.substitute()
    Traceback (most recent call last):
        ...
    NameError: name 'name' is not defined at line 1 column 6 in file tmpl

You can also give a namespace to use by default, which
``.substitute(...)`` will augment::

    >>> tmpl = tempita.Template(
    ...     'Hi {{upper(name)}}',
    ...     namespace=dict(upper=lambda s: s.upper()))
    >>> tmpl.substitute(name='Joe')
    'Hi JOE'

Lastly, you can give a dictionary-like object as the argument to
``.substitute``, like::

    >>> name = 'Jane'
    >>> tmpl.substitute(locals())
    'Hi JANE'

There's also an `HTMLTemplate`_ class that is more appropriate for
templates that produce HTML.

You can also instantiate a template from a filename with
``Template.from_filename(filename, namespace={}, encoding=None)``.
This is like calling::

    Template(open(filename, 'rb').read().decode(encoding), 
             name=filename, namespace=namespace)

Unicode
-------

Tempita tries to handle unicode gracefully, for some value of
"graceful".  ``Template`` objects have a ``default_encoding``
attribute.  It will try to use that encoding whenever ``unicode`` and
``str`` objects are mixed in the template.  E.g.::

    >>> tmpl = tempita.Template(u'Hi {{name}}')
    >>> tmpl.substitute(name='Jos\xc3\xa9')
    u'Hi Jos\xe9'
    >>> tmpl = tempita.Template('Hi {{name}}')
    >>> tmpl.substitute(name=u'Jos\xe9')
    'Hi Jos\xc3\xa9'

The default encoding is UTF8.

The Language
============

The language is fairly simple; all the constructs look like
``{{stuff}}``.  

Substitution
------------

To insert a variable or expression, use ``{{expression}}``.  You can't
use ``}}`` in your expression, but if it comes up just use ``} }``
(put a space between them).  You can pass your expression through
*filters* with ``{{expression | filter}}``, for instance
``{{expression | repr}}``.  This is entirely equivalent to
``{{repr(expression)}}``.  But it might look nicer to some people; I
took it from Django because I liked it.  There's a shared namespace,
so ``repr`` is just an object in the namespace.

If you want to have ``{{`` or ``}}`` in your template, you must use
the built-in variables like ``{{start_braces}}`` and
``{{end_braces}}``.  There's no escape character.

None, as a special case, is substituted as the empty string.

Also there is a command for setting default values in your template::

    {{default width = 100}}

You can use this so that the ``width`` variable will always have a
value in your template (the number ``100``).  If someone calls
``tmpl.substitute(width=200)`` then this will have no effect; only if
the variable is undefined will this default matter.  You can use any
expression to the right of the ``=``.

if
--

You can do an if statement with::

    {{if condition}}
      true stuff
    {{elif other_condition}}
      other stuff
    {{else}}
      final stuff
    {{endif}}

Some of the blank lines will be removed when, as in this case, they
only contain a single directive.  A trailing ``:`` is optional.

for
---

Loops should be unsurprising::

    {{for a, b in items}}
        {{a}} = {{b | repr}}
    {{endfor}}

See?  Unsurprising.  Note that nested tuples (like ``for a, (b, c)
in...``) are not supported.

inherit & def
-------------

You can do template inheritance.  To inherit from another template
do::

    {{inherit "some_other_file"}}

From Python you must also pass in, to `Template`, a `get_template`
function; the implementation for ``Template.from_filename(...)`` is::

    def get_file_template(name, from_template):
        path = os.path.join(from_template.name, name)
        return from_template.__class__.from_filename(
            path, namespace=from_template.namespace,
            get_template=from_template.get_template)

You can also pass in a constructor argument `default_inherit`, which
will be the inherited template name when no ``{{inherit}}`` is in the
template.

The inherited template is executed with a variable ``self``, which
includes ``self.body`` which is the text of the child template.  You
can also put in definitions in the child, like::

    {{def sidebar}}
      sidebar links...
    {{enddef}}

Then in the parent/inherited template::

    {{self.sidebar}}

If you want to make the sidebar method optional, in the inherited
template use::

    {{self.get.sidebar}}

If ``sidebar`` is not defined then this will just result in an object
that shows up as the empty string (but is also callable).

This can be called like ``self.sidebar`` or ``self.sidebar()`` -- defs
can have arguments (like ``{{def sidebar(name)}}``), but when there
are no arguments you can leave off ``()`` (in the call and
definition).

Python blocks
-------------

For anything more complicated, you can use blocks of Python code,
like::

    {{py:x = 1}}
    
    {{py:
    lots of code
    }}

The first form allows statements, like an assignment or raising an
exception.  The second form is for multiple lines.  If you have
multiple lines, then ``{{py:`` must be on a line of its own and the
code can't be indented (except for normal indenting in ``def x():``
etc).

These can't output any values, but they can calculate values and
define functions.  So you can do something like::

    {{py:
    def pad(s):
        return s + ' '*(20-len(s))
    }}
    {{for name, value in kw.items()}}
    {{s | pad}} {{value | repr}}
    {{endfor}}

As a last detail ``{{# comments...}}`` doesn't do anything at all,
because it is a comment.

bunch and looper
----------------

There's two kinds of objects provided to help you in your templates.
The first is ``tempita.bunch``, which is just a dictionary that also
lets you use attributes::

    >>> bunch = tempita.bunch(a=1)
    >>> bunch.a
    1
    >>> bunch.items()
    [('a', 1)]
    >>> bunch.default = None
    >>> print bunch.b
    None

This can be nice for passing options into a template.  

The other object is for use inside the template, and is part of the
default namespace, ``looper``.  This can be used in ``for`` loops in
some convenient ways.  You basically use it like::

    {{for loop, item in looper(seq)}}
      ...
    {{endfor}}

The ``loop`` object has a bunch of useful methods and attributes:

    ``.index``
      The index of the current item (like you'd get with
      ``enumerate()``)
    ``.number``
      The number: ``.index + 1``
    ``.item``
      The item you are looking at.  Which you probably already have,
      but it's there if you want it.
    ``.next``
      The next item in the sequence, or None if it's the last item.
    ``.previous``
      The previous item in the sequence, or None if it's the first
      item.
    ``.odd``
      True if this is an odd item.  The first item is even.
    ``.even``
      True if it's even.
    ``.first``
      True if this is the first item.
    ``.last``
      True if this is the last item.
    ``.length``
      The total length of the sequence.
    ``.first_group(getter=None)``
      Returns true if this item is the first in the group, where the
      group is either of equal objects (probably boring), or when you
      give a getter.  getter can be ``'.attribute'``, like
      ``'.last_name'`` -- this lets you group people by their last
      name.  Or a method, like ``'.birth_year()'`` -- which calls the
      method.  If it's just a string, it is expected to be a key in a
      dictionary, like ``'name'`` which groups on ``item['name']``.
      Or you can give a function which returns the value to group on.
      This always returns true when ``.first`` returns true.
    ``.last_group(getter=None)``
      Like ``first_group``, only returns True when it's the last of
      the group.  This always returns true when ``.last`` returns true.

Note that there's currently a limitation in the templating language,
so you can't do ``{{for loop, (key, value) in looper(d.items())}}``.
You'll have to do::

    {{for loop, key_value in looper(d.items())}}
      {{py:key, value = key_value}}
      ...
    {{endfor}}

HTMLTemplate
============

In addition to ``Template`` there is a template specialized for HTML,
``HTMLTemplate`` (and the substitution function ``sub_html``).

The basic thing that it adds is automatic HTML quoting.  All values
substituted into your template will be quoted unless they are
specially marked.

You mark objects as instances of ``tempita.html``.  The easiest way is
``{{some_string | html}}``, though you can also use
``tempita.html(string)`` in your functions.

An example::

    >>> tmpl = tempita.HTMLTemplate('''\
    ... Hi {{name}}!
    ... <a href="{{href}}">{{title|html}}</a>''')
    >>> name = tempita.html('<img src="bob.jpg">')
    >>> href = 'Attack!">'
    >>> title = '<i>Homepage</i>'
    >>> tmpl.substitute(locals())
    'Hi <img src="bob.jpg">!\n<a href="Attack!&quot;&gt;"><i>Homepage</i></a>'

It also adds a couple handy builtins:

    ``html_quote(value)``:
        HTML quotes the value.  Turns all unicode values into
        character references, so it always returns ASCII text.  Also
        it calls ``str(value)`` or ``unicode(value)``, so you can do
        things like ``html_quote(1)``.

    ``url(value)``:
        Does URL quoting, similar to ``html_quote()``.

    ``attr(**kw)``:
        Inserts attributes.  Use like::

            <div {{attr(width=width, class_=div_class)}}>

        Then it'll put in something like ``width="{{width}}"
        class={{div_class}}``.  But any attribute that is None is left
        out entirely.

Extending Tempita
=================

It's not really meant for extension.  Instead you should just write
Python functions and classes that do what you want, and use them in
the template.  You can either add the namespace to the constructor, or
extend ``default_namespace`` in your own subclass.

The extension that ``HTMLTemplate`` uses is to subclass and override
the ``_repr(value, pos)`` function.  This is called on each object
just before inserting it in the template.

Two other methods you might want to look at are ``_eval(code, ns,
pos)`` and ``_exec(code, ns, pos)``, which evaluate and execute
expressions and statements.  You could probably make this language
safe with appropriate implementations of those methods.

Including Tempita In Your Project
=================================

If you don't want to use Setuptools and have Tempita as a dependency,
you can add the ``svn:external``
``http://svn.pythonpaste.org/Tempita/trunk/tempita tempita`` (or link
to a tag).  It's about 730 lines of code (not counting comments and
whatnot), and 30Kb (60Kb with .pyc files).

Command-line Use
================

There's also a command-line version of the program.  In Python 2.5 you
can run ``python -m tempita``; in previous versions you must run
``python path/to/tempita/__init__.py``.

The usage::

    Usage: __init__.py [OPTIONS] TEMPLATE arg=value

    Use py:arg=value to set a Python value; otherwise all values are
    strings.


    Options:
      --version             show program's version number and exit
      -h, --help            show this help message and exit
      -o FILENAME, --output=FILENAME
                            File to write output to (default stdout)
      --html                Use HTML style filling (including automatic HTML
                            quoting)
      --env                 Put the environment in as top-level variables

So you can use it like::

    $ python2.5 -m tempita --html mytemplate.tmpl \
    >     var1="$var1" var2="$var2" > mytemplate.html


Still To Do
===========

* Currently nested structures in ``for`` loop assignments don't work,
  like ``for (a, b), c in x``.  They should.

* There's no way to handle exceptions, except in your ``py:`` code.
  I'm not sure what there should be, if anything.

* Probably I should try to dedent ``py:`` code.

* There should be some way of calling a function with a chunk of the
  template.  Maybe like::

    {{call expr}}
      template code...
    {{endcall}}

  That would mean ``{{expr(result_of_template_code)}}``.  But maybe
  there should be another assignment form too, if you don't want to
  immediately put the output in the code.  Probably defs could be used
  for this, like::

    {{def something}}
      template code...
    {{enddef}}
    {{expr(something)}}

News
====

svn trunk
---------

* Instead of defining ``__name__`` in template namespaces (which has special
  rules, and must be a module name) the template name is put into
  ``__template_name__``.  This became important in Python 2.5.

0.3
---

* Added ``{{inherit}}`` and ``{{def}}`` for doing template inheritance.

* Make error message annotation slightly more robust.

* Fix whitespace stripping for the beginning and end of lines.

0.2
---

* Added ``html_quote`` to default functions provided in
  ``HTMLTemplate``.

* HTML literals have an ``.__html__()`` method, and the presence of
  that method is used to determine if values need to be quoted in
  ``HTMLTemplate``.