1
<?xml version="1.0" encoding="ascii"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3
"DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>genshi.template.eval.Expression</title>
7
<link rel="stylesheet" href="epydoc.css" type="text/css" />
8
<script type="text/javascript" src="epydoc.js"></script>
11
<body bgcolor="white" text="black" link="blue" vlink="#204080"
13
<!-- ==================== NAVIGATION BAR ==================== -->
14
<table class="navbar" border="0" width="100%" cellpadding="0"
15
bgcolor="#a0c0ff" cellspacing="0">
18
<th> <a
19
href="genshi-module.html">Home</a> </th>
22
<th> <a
23
href="module-tree.html">Trees</a> </th>
26
<th> <a
27
href="identifier-index.html">Indices</a> </th>
30
<th> <a
31
href="help.html">Help</a> </th>
33
<!-- Project homepage -->
34
<th class="navbar" align="right" width="100%">
35
<table border="0" cellpadding="0" cellspacing="0">
36
<tr><th class="navbar" align="center"
37
><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
41
<table width="100%" cellpadding="0" cellspacing="0">
44
<span class="breadcrumbs">
45
<a href="genshi-module.html">Package genshi</a> ::
46
<a href="genshi.template-module.html">Package template</a> ::
47
<a href="genshi.template.eval-module.html">Module eval</a> ::
52
<table cellpadding="0" cellspacing="0">
53
<!-- hide/show private -->
58
<!-- ==================== CLASS DESCRIPTION ==================== -->
59
<h1 class="epydoc">Class Expression</h1><br /><br />
60
<pre class="base-tree">
63
<a href="genshi.template.eval.Code-class.html">Code</a> --+
65
<strong class="uidshort">Expression</strong>
69
<p>Evaluates Python expressions used in templates.</p>
70
<pre class="py-doctest">
71
<span class="py-prompt">>>> </span>data = dict(test=<span class="py-string">'Foo'</span>, <span class="py-builtin">items</span>=[1, 2, 3], dict={<span class="py-string">'some'</span>: <span class="py-string">'thing'</span>})
72
<span class="py-prompt">>>> </span>Expression(<span class="py-string">'test'</span>).evaluate(data)
73
<span class="py-output">'Foo'</span></pre>
74
<pre class="py-doctest">
75
<span class="py-prompt">>>> </span>Expression(<span class="py-string">'items[0]'</span>).evaluate(data)
76
<span class="py-output">1</span>
77
<span class="py-output"></span><span class="py-prompt">>>> </span>Expression(<span class="py-string">'items[-1]'</span>).evaluate(data)
78
<span class="py-output">3</span>
79
<span class="py-output"></span><span class="py-prompt">>>> </span>Expression(<span class="py-string">'dict["some"]'</span>).evaluate(data)
80
<span class="py-output">'thing'</span></pre>
81
<p>Similar to e.g. Javascript, expressions in templates can use the dot
82
notation for attribute access to access items in mappings:</p>
83
<pre class="py-doctest">
84
<span class="py-prompt">>>> </span>Expression(<span class="py-string">'dict.some'</span>).evaluate(data)
85
<span class="py-output">'thing'</span></pre>
86
<p>This also works the other way around: item access can be used to access
87
any object attribute:</p>
88
<pre class="py-doctest">
89
<span class="py-prompt">>>> </span><span class="py-keyword">class</span> <span class="py-defname">MyClass</span>(object):
90
<span class="py-more">... </span> myattr = <span class="py-string">'Bar'</span>
91
<span class="py-prompt">>>> </span>data = dict(mine=MyClass(), key=<span class="py-string">'myattr'</span>)
92
<span class="py-prompt">>>> </span>Expression(<span class="py-string">'mine.myattr'</span>).evaluate(data)
93
<span class="py-output">'Bar'</span>
94
<span class="py-output"></span><span class="py-prompt">>>> </span>Expression(<span class="py-string">'mine["myattr"]'</span>).evaluate(data)
95
<span class="py-output">'Bar'</span>
96
<span class="py-output"></span><span class="py-prompt">>>> </span>Expression(<span class="py-string">'mine[key]'</span>).evaluate(data)
97
<span class="py-output">'Bar'</span></pre>
98
<p>All of the standard Python operators are available to template expressions.
99
Built-in functions such as <tt class="rst-docutils literal"><span class="pre">len()</span></tt> are also available in template
101
<pre class="py-doctest">
102
<span class="py-prompt">>>> </span>data = dict(<span class="py-builtin">items</span>=[1, 2, 3])
103
<span class="py-prompt">>>> </span>Expression(<span class="py-string">'len(items)'</span>).evaluate(data)
104
<span class="py-output">3</span></pre><br /><br />
106
<!-- ==================== INSTANCE METHODS ==================== -->
107
<a name="section-InstanceMethods"></a>
108
<table class="summary" border="1" cellpadding="3"
109
cellspacing="0" width="100%" bgcolor="white">
110
<tr bgcolor="#70b0f0" class="table-header">
111
<td align="left" colspan="2" class="table-header">
112
<span class="table-header">Instance Methods</span></td>
115
<td width="15%" align="right" valign="top" class="summary">
116
<span class="summary-type"> </span>
117
</td><td class="summary">
118
<table width="100%" cellpadding="0" cellspacing="0" border="0">
120
<td><span class="summary-sig"><a href="genshi.template.eval.Expression-class.html#evaluate" class="summary-sig-name">evaluate</a>(<span class="summary-sig-arg">self</span>,
121
<span class="summary-sig-arg">data</span>)</span><br />
122
Evaluate the expression against the given data dictionary.</td>
123
<td align="right" valign="top">
133
<td colspan="2" class="summary">
134
<p class="indent-wrapped-lines"><b>Inherited from <code><a href="genshi.template.eval.Code-class.html">Code</a></code></b>:
135
<code><a href="genshi.template.eval.Code-class.html#__eq__">__eq__</a></code>,
136
<code><a href="genshi.template.eval.Code-class.html#__hash__">__hash__</a></code>,
137
<code><a href="genshi.template.eval.Code-class.html#__init__">__init__</a></code>,
138
<code><a href="genshi.template.eval.Code-class.html#__ne__">__ne__</a></code>,
139
<code><a href="genshi.template.eval.Code-class.html#__repr__">__repr__</a></code>
141
<p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
142
<code>__delattr__</code>,
143
<code>__getattribute__</code>,
144
<code>__new__</code>,
145
<code>__reduce__</code>,
146
<code>__reduce_ex__</code>,
147
<code>__setattr__</code>,
153
<!-- ==================== CLASS VARIABLES ==================== -->
154
<a name="section-ClassVariables"></a>
155
<table class="summary" border="1" cellpadding="3"
156
cellspacing="0" width="100%" bgcolor="white">
157
<tr bgcolor="#70b0f0" class="table-header">
158
<td align="left" colspan="2" class="table-header">
159
<span class="table-header">Class Variables</span></td>
162
<td width="15%" align="right" valign="top" class="summary">
163
<span class="summary-type"> </span>
164
</td><td class="summary">
165
<a name="mode"></a><span class="summary-name">mode</span> = <code title="'eval'"><code class="variable-quote">'</code><code class="variable-string">eval</code><code class="variable-quote">'</code></code>
169
<!-- ==================== PROPERTIES ==================== -->
170
<a name="section-Properties"></a>
171
<table class="summary" border="1" cellpadding="3"
172
cellspacing="0" width="100%" bgcolor="white">
173
<tr bgcolor="#70b0f0" class="table-header">
174
<td align="left" colspan="2" class="table-header">
175
<span class="table-header">Properties</span></td>
178
<td colspan="2" class="summary">
179
<p class="indent-wrapped-lines"><b>Inherited from <code><a href="genshi.template.eval.Code-class.html">Code</a></code></b>:
180
<code><a href="genshi.template.eval.Code-class.html#code">code</a></code>,
181
<code><a href="genshi.template.eval.Code-class.html#source">source</a></code>
183
<p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
184
<code>__class__</code>
189
<!-- ==================== METHOD DETAILS ==================== -->
190
<a name="section-MethodDetails"></a>
191
<table class="details" border="1" cellpadding="3"
192
cellspacing="0" width="100%" bgcolor="white">
193
<tr bgcolor="#70b0f0" class="table-header">
194
<td align="left" colspan="2" class="table-header">
195
<span class="table-header">Method Details</span></td>
198
<a name="evaluate"></a>
200
<table class="details" border="1" cellpadding="3"
201
cellspacing="0" width="100%" bgcolor="white">
203
<table width="100%" cellpadding="0" cellspacing="0" border="0">
204
<tr valign="top"><td>
205
<h3 class="epydoc"><span class="sig"><span class="sig-name">evaluate</span>(<span class="sig-arg">self</span>,
206
<span class="sig-arg">data</span>)</span>
208
</td><td align="right" valign="top"
213
Evaluate the expression against the given data dictionary.
216
<dd><ul class="nomargin-top">
217
<li><strong class="pname"><code>data</code></strong> - a mapping containing the data to evaluate against</li>
220
<dd>the result of the evaluation</dd>
225
<!-- ==================== NAVIGATION BAR ==================== -->
226
<table class="navbar" border="0" width="100%" cellpadding="0"
227
bgcolor="#a0c0ff" cellspacing="0">
230
<th> <a
231
href="genshi-module.html">Home</a> </th>
234
<th> <a
235
href="module-tree.html">Trees</a> </th>
238
<th> <a
239
href="identifier-index.html">Indices</a> </th>
242
<th> <a
243
href="help.html">Help</a> </th>
245
<!-- Project homepage -->
246
<th class="navbar" align="right" width="100%">
247
<table border="0" cellpadding="0" cellspacing="0">
248
<tr><th class="navbar" align="center"
249
><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
253
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
255
<td align="left" class="footer">
256
Generated by Epydoc 3.0beta1 on Mon Apr 16 16:02:14 2007
258
<td align="right" class="footer">
259
<a href="http://epydoc.sourceforge.net">http://epydoc.sourceforge.net</a>
264
<script type="text/javascript">
266
// Private objects are initially displayed (because if
267
// javascript is turned off then we want them to be
268
// visible); but by default, we want to hide them. So hide
269
// them unless we have a cookie that says to show them.