comparison doc/text-templates.txt @ 510:ca7d707d51b0

Use syntax highlighting on all the other doc pages, too.
author cmlenz
date Wed, 06 Jun 2007 10:41:41 +0000
parents 2abf0a19058b
children 7145e4eba2ec f0bb2c5ea0ff
comparison
equal deleted inserted replaced
509:1997f7af845c 510:ca7d707d51b0
55 .. _`#if`: 55 .. _`#if`:
56 56
57 ``#if`` 57 ``#if``
58 --------- 58 ---------
59 59
60 The content is only rendered if the expression evaluates to a truth value:: 60 The content is only rendered if the expression evaluates to a truth value:
61
62 .. code-block:: genshitext
61 63
62 #if foo 64 #if foo
63 ${bar} 65 ${bar}
64 #end 66 #end
65 67
80 ``#otherwise`` provides advanced contional processing for rendering one of 82 ``#otherwise`` provides advanced contional processing for rendering one of
81 several alternatives. The first matching ``#when`` branch is rendered, or, if 83 several alternatives. The first matching ``#when`` branch is rendered, or, if
82 no ``#when`` branch matches, the ``#otherwise`` branch is be rendered. 84 no ``#when`` branch matches, the ``#otherwise`` branch is be rendered.
83 85
84 If the ``#choose`` directive has no argument the nested ``#when`` directives 86 If the ``#choose`` directive has no argument the nested ``#when`` directives
85 will be tested for truth:: 87 will be tested for truth:
88
89 .. code-block:: genshitext
86 90
87 The answer is: 91 The answer is:
88 #choose 92 #choose
89 #when 0 == 1 93 #when 0 == 1
90 0 94 0
101 105
102 The answer is: 106 The answer is:
103 1 107 1
104 108
105 If the ``#choose`` does have an argument, the nested ``#when`` directives will 109 If the ``#choose`` does have an argument, the nested ``#when`` directives will
106 be tested for equality to the parent ``#choose`` value:: 110 be tested for equality to the parent ``#choose`` value:
111
112 .. code-block:: genshitext
107 113
108 The answer is: 114 The answer is:
109 #choose 1 115 #choose 1
110 #when 0 116 #when 0
111 0 117 0
130 .. _`#for`: 136 .. _`#for`:
131 137
132 ``#for`` 138 ``#for``
133 ---------- 139 ----------
134 140
135 The content is repeated for every item in an iterable:: 141 The content is repeated for every item in an iterable:
142
143 .. code-block:: genshitext
136 144
137 Your items: 145 Your items:
138 #for item in items 146 #for item in items
139 * ${item} 147 * ${item}
140 #end 148 #end
156 ``#def`` 164 ``#def``
157 ---------- 165 ----------
158 166
159 The ``#def`` directive can be used to create macros, i.e. snippets of template 167 The ``#def`` directive can be used to create macros, i.e. snippets of template
160 text that have a name and optionally some parameters, and that can be inserted 168 text that have a name and optionally some parameters, and that can be inserted
161 in other places:: 169 in other places:
170
171 .. code-block:: genshitext
162 172
163 #def greeting(name) 173 #def greeting(name)
164 Hello, ${name}! 174 Hello, ${name}!
165 #end 175 #end
166 ${greeting('world')} 176 ${greeting('world')}
170 180
171 Hello, world! 181 Hello, world!
172 Hello, everyone else! 182 Hello, everyone else!
173 183
174 If a macro doesn't require parameters, it can be defined as well as called 184 If a macro doesn't require parameters, it can be defined as well as called
175 without the parenthesis. For example:: 185 without the parenthesis. For example:
186
187 .. code-block:: genshitext
176 188
177 #def greeting 189 #def greeting
178 Hello, world! 190 Hello, world!
179 #end 191 #end
180 ${greeting} 192 ${greeting}
189 201
190 ``#include`` 202 ``#include``
191 ------------ 203 ------------
192 204
193 To reuse common parts of template text across template files, you can include 205 To reuse common parts of template text across template files, you can include
194 other files using the ``#include`` directive:: 206 other files using the ``#include`` directive:
207
208 .. code-block:: genshitext
195 209
196 #include "base.txt" 210 #include "base.txt"
197 211
198 Any content included this way is inserted into the generated output. The 212 Any content included this way is inserted into the generated output. The
199 included template sees the context data as it exists at the point of the 213 included template sees the context data as it exists at the point of the
206 the included file at "``myapp/base.txt``". You can also use Unix-style 220 the included file at "``myapp/base.txt``". You can also use Unix-style
207 relative paths, for example "``../base.txt``" to look in the parent directory. 221 relative paths, for example "``../base.txt``" to look in the parent directory.
208 222
209 Just like other directives, the argument to the ``#include`` directive accepts 223 Just like other directives, the argument to the ``#include`` directive accepts
210 any Python expression, so the path to the included template can be determined 224 any Python expression, so the path to the included template can be determined
211 dynamically:: 225 dynamically:
226
227 .. code-block:: genshitext
212 228
213 #include '%s.txt' % filename 229 #include '%s.txt' % filename
214 230
215 Note that a ``TemplateNotFound`` exception is raised if an included file can't 231 Note that a ``TemplateNotFound`` exception is raised if an included file can't
216 be found. 232 be found.
230 be used to make expressions inside the directive less verbose and more 246 be used to make expressions inside the directive less verbose and more
231 efficient. For example, if you need use the expression ``author.posts`` more 247 efficient. For example, if you need use the expression ``author.posts`` more
232 than once, and that actually results in a database query, assigning the results 248 than once, and that actually results in a database query, assigning the results
233 to a variable using this directive would probably help. 249 to a variable using this directive would probably help.
234 250
235 For example:: 251 For example:
252
253 .. code-block:: genshitext
236 254
237 Magic numbers! 255 Magic numbers!
238 #with y=7; z=x+10 256 #with y=7; z=x+10
239 $x $y $z 257 $x $y $z
240 #end 258 #end
Copyright (C) 2012-2017 Edgewall Software