This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
forked from kapt/pybars
/
PKG-INFO
150 lines (108 loc) · 5.9 KB
/
PKG-INFO
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
Metadata-Version: 1.1
Name: pybars
Version: 0.0.1
Summary: handlebars.js templating for Python.
Home-page: https://launchpad.net/pybars
Author: Launchpad Developers
Author-email: launchpad-dev@lists.launchpad.net
License: UNKNOWN
Description: *****************************
pybars: handlebars for python
*****************************
Copyright (c) 2012, Canonical Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, version 3 only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
GNU Lesser General Public License version 3 (see the file LICENSE).
pybars provides a template system for Python which is compatible with
handlebars.js.
Dependencies
============
* Python 2.6+
* PyMeta (https://launchpad.net/pymeta)
Testing Dependencies
====================
* subunit (http://pypi.python.org/pypi/python-subunit) (optional)
* testtools (http://pypi.python.org/pypi/testtools)
Usage
=====
For details on the template language see the http://handlebarsjs.com/
documentation.
Translating the engine to python required slightly different calling
conventions to the JS version:
* block helpers take (this, options, *args, **kwargs)
* other helpers take (this, *args, **kwargs)
* closures in the context take (this, *args, **kwargs)
A template like '{{foo bar quux=1}}' will pass bar as a positional argument and
quux as a keyword argument. Keyword arguments have to be non-reserved words in
Python. For instance, 'print' as a keyword argument will fail.
Templates with literal boolean arguments like '{{foo true}}' will have the
argument mapped to Python's True or False as appropriate.
For efficiency, rather that passing strings round, pybars passes a subclass of
list ('liststr') which has a __unicode__ implementation that returns
u"".join(self). Template helpers can return any of list, tuple, unicode or
liststr instances. liststr exists to avoid quadratic overheads in string
processing during template rendering. Helpers that are in inner loops *should*
return list or liststr for the same reason.
**NOTE** The liststr takes the position of SafeString in the js implementation:
when returning a liststr it will not be escaped, even in a regular {{}}
expansion.
The 'data' facility from the JS implementation has not been ported at this
point, if there is demand for it it would be quite easy to add. Similarly
the stringParams feature has not been ported - quote anything you wish to force
to a string in a helper call.
Typical usage:
* Grab a compiler:
>>> from pybars import Compiler
>>> compiler = Compiler()
* Register any extensions you need:
>>> def _list(this, options, items):
... result = [u'<ul>']
... for thing in items:
... result.append(u'<li>')
... result.extend(options['fn'](thing))
... result.append(u'</li>')
... result.append(u'</ul>')
... return result
>>> compiler.register_helper(u'list', _list)
* And compile your template:
>>> source = u"{{#list people}}{{firstName}} {{lastName}}{{/list}}"
>>> template = compiler.compile(source)
* You can now render it:
>>> template({
... 'people': [
... {'firstName': "Yehuda", 'lastName': "Katz"},
... {'firstName': "Carl", 'lastName': "Lerche"},
... {'firstName': "Alan", 'lastName': "Johnson"}
... ]})
<ul><li>Yehuda Katz</li><li>Carl Lerche</li><li>Alan Johnson</li></ul>
More details should be found by reading the API docs (e.g. pydoc pybars).
Installation
============
Either run setup.py in an environment with all the dependencies available, or
add the working directory to your PYTHONPATH.
Development
===========
Upstream development takes place at https://launchpad.net/pybars.
To setup a working area for development, if the dependencies are not
immediately available, you can use ./bootstrap.py to create bin/buildout, then
bin/py to get a python interpreter with the dependencies available.
To run the tests use the runner of your choice, the test suite is
pybars.tests.test_suite.
For instance::
$ bin/py -m testtools.run pybars.tests.test_suite
pybars is testrepository enabled, so you can just do:
$ testr init
$ testr run
Platform: UNKNOWN
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python