comparison doc/templates.txt @ 811:b1e9e3209c6f

Add doc section on expression escaping. Closes #282.
author cmlenz
date Mon, 09 Mar 2009 08:57:43 +0000
parents 211cd26d6fd9
children 4376010bb97e
comparison
equal deleted inserted replaced
809:92c5d936616b 811:b1e9e3209c6f
201 rather an exception of the type ``UndefinedError``. The same kind of error is 201 rather an exception of the type ``UndefinedError``. The same kind of error is
202 raised when you try to use a top-level variable that is not in the context data. 202 raised when you try to use a top-level variable that is not in the context data.
203 See `Error Handling`_ below for details on how such errors are handled. 203 See `Error Handling`_ below for details on how such errors are handled.
204 204
205 205
206 Escaping
207 ========
208
209 If you need to include a literal dollar sign in the output where Genshi would
210 normally detect an expression, you can simply add another dollar sign:
211
212 .. code-block:: pycon
213
214 >>> from genshi.template import MarkupTemplate
215 >>> tmpl = MarkupTemplate('<em>$foo</em>') # Wanted "$foo" as literal output
216 >>> print tmpl.generate()
217 Traceback (most recent call last):
218 ...
219 UndefinedError: "foo" not defined
220 >>> tmpl = MarkupTemplate('<em>$$foo</em>')
221 >>> print tmpl.generate()
222 <em>$foo</em>
223
224 But note that this is not necessary if the characters following the dollar sign
225 do not qualify as an expression. For example, the following needs no escaping:
226
227 .. code-block:: pycon
228
229 >>> tmpl = MarkupTemplate('<script>$(function() {})</script>')
230 >>> print tmpl.generate()
231 <script>$(function() {})</script>
232
233 On the other hand, Genshi will always replace two dollar signs in text with a
234 single dollar sign, so you'll need to use three dollar signs to get two in the
235 output:
236
237 .. code-block:: pycon
238
239 >>> tmpl = MarkupTemplate('<script>$$$("div")</script>')
240 >>> print tmpl.generate()
241 <script>$$("div")</script>
242
243
206 .. _`code blocks`: 244 .. _`code blocks`:
207 245
208 Code Blocks 246 Code Blocks
209 =========== 247 ===========
210 248
Copyright (C) 2012-2017 Edgewall Software