comparison doc/text-templates.txt @ 429:6911f3c5a7e8

Updated docs for code blocks and changed error handling.
author cmlenz
date Thu, 22 Mar 2007 17:00:09 +0000
parents ebc7c1a3bc4d
children ff7c72b52fb2
comparison
equal deleted inserted replaced
428:540dd825d072 429:6911f3c5a7e8
90 >>> from genshi.template import TextTemplate 90 >>> from genshi.template import TextTemplate
91 >>> tmpl = TextTemplate('${dict.foo}') 91 >>> tmpl = TextTemplate('${dict.foo}')
92 >>> print tmpl.generate(dict={'foo': 'bar'}) 92 >>> print tmpl.generate(dict={'foo': 'bar'})
93 bar 93 bar
94 94
95 Another difference is that you can access variables that are not defined, and 95 Because there are two ways to access either attributes or items, expressions
96 won't get a ``NameError`` exception:: 96 do not raise the standard ``AttributeError`` or ``IndexError`` exceptions, but
97 97 rather an exception of the type ``UndefinedError``. The same kind of error is
98 >>> from genshi.template import TextTemplate 98 raised when you try to access a top-level variable that is not in the context
99 >>> tmpl = TextTemplate('${doh}') 99 data.
100 >>> print tmpl.generate() 100
101 <BLANKLINE> 101 Built-in Functions & Types
102 102 ==========================
103 You **will** however get a ``NameError`` if you try to call an undefined 103
104 variable, or do anything else with it, such as accessing its attributes. If you 104 The following functions and types are available by default in template code, in
105 need to know whether a variable is defined, you can check its type against the 105 addition to the standard built-ins that are available to all Python code.
106 ``Undefined`` class, for example in an `#if`_ directive:: 106
107 107 ``defined(name)``
108 >>> from genshi.template import TextTemplate 108 -----------------
109 >>> tmpl = TextTemplate('${type(doh) is Undefined}') 109
110 >>> print tmpl.generate() 110 This function determines whether a variable of the specified name exists in
111 True 111 the context data, and returns ``True`` if it does.
112
113 ``value_of(name, default=None)``
114 --------------------------------
115
116 This function returns the value of the variable with the specified name if
117 such a variable is defined, and returns the value of the ``default``
118 parameter if no such variable is defined.
119
120 ``Markup(text)``
121 ----------------
122
123 The ``Markup`` type marks a given string as being safe for inclusion in markup,
124 meaning it will *not* be escaped in the serialization stage. Use this with care,
125 as not escaping a user-provided string may allow malicious users to open your
126 web site to cross-site scripting attacks.
112 127
113 128
114 .. _`directives`: 129 .. _`directives`:
115 130
116 ------------------- 131 -------------------
Copyright (C) 2012-2017 Edgewall Software