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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>5. Templating languages: CHTL and CGTL</title>
<META NAME="description" CONTENT="5. Templating languages: CHTL and CGTL">
<META NAME="keywords" CONTENT="tut">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="tut.css" type='text/css'>
<link rel="first" href="tut.html">
<link rel="contents" href="contents.html" title="Contents">
<LINK REL="next" HREF="node8.html">
<LINK REL="previous" HREF="node6.html">
<LINK REL="up" HREF="tut.html">
<LINK REL="next" HREF="node8.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node6.html"><img src="../icons/previous.gif"
border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="tut.html"><img src="../icons/up.gif"
border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node8.html"><img src="../icons/next.gif"
border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy Tutorial</td>
<td><A HREF="node2.html"><img src="../icons/contents.gif"
border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node6.html">4. Creating a first</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node8.html">6. Views, functions and</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="node7.html#SECTION007100000000000000000">5.1 Tags</a>
<UL>
<LI><A href="node7.html#SECTION007110000000000000000">5.1.1 py-eval</a>
<LI><A href="node7.html#SECTION007120000000000000000">5.1.2 py-attr</a>
<LI><A href="node7.html#SECTION007130000000000000000">5.1.3 py-exec and py-code</a>
<LI><A href="node7.html#SECTION007140000000000000000">5.1.4 py-if and py-else</a>
<LI><A href="node7.html#SECTION007150000000000000000">5.1.5 py-for</a>
</ul>
<LI><A href="node7.html#SECTION007200000000000000000">5.2 Putting it together</a>
</ul>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION007000000000000000000">
5. Templating languages: CHTL and CGTL</A>
</H1>
<P>
CherryPy comes with TWO templating languages. They are very similar to each other, but they are used in
different situations. They have been designed to be very easy to use, and yet very powerful.
<UL>
<LI><b>CherryPy HTML Templating Language (CHTL)</b>: It is used to produce HTML output. It has been designed so that
HTML editors keep the dynamic informations in the page. This way, webdesigners can edit the pages using their
favorite HTML editor without losing the information that the developers put in them. The trick is to hide code
in the attributes of HTML tags.
</LI>
<LI><b>CherryPy Generic Templating Language (CGTL)</b>: It is used to produce non-HTML output (for instance
Javascript, CSS, XML, ...). It is very close to CHTL, except that we cannot use HTML tags to hide the code, and the
syntax is a bit simpler. <b>If you don't use any HTML editor to create your pages, then you should probably use
CGTL only as it is simpler than CHTL</b>.
</LI>
</UL>
<P>
Note that you can also use other templating languages if you want (for instance, there is a HowTo that explains how to use Cheetah), or you can just use plain python to generate your pages.
<P>
<H1><A NAME="SECTION007100000000000000000">
5.1 Tags</A>
</H1>
Both languages use only 6 tags: <code>py-eval</code>, <code>py-attr</code>, <code>py-exec</code>, <code>py-code</code>, <code>py-if</code> (with <code>py-else</code>)
and <code>py-for</code>.
<P>
All tags are used the same way: <code>CherryPyTag="Some Python code"</code>. For instance:
<div class="verbatim"><pre>
py-eval="2*2"
py-exec="a=1"
py-if="2==0"
py-for="i in range(10)"
...
</pre></div>
<P>
If you want to use double-quotes inside the python code, you need to escape them using a backslash, like this:
<div class="verbatim"><pre>
py-eval="'I love \"cherry pies\"'"
</pre></div>
<P>
Let's see what each of these tags is used for:
<P>
<H2><A NAME="SECTION007110000000000000000">
5.1.1 py-eval</A>
</H2>
This tag is used to evaluate a Python expression, like this:
<div class="verbatim"><pre>
Hello, the sum is <py-eval="1+2+3">
</pre></div>
<P>
This line will be rendered as:
<div class="verbatim"><pre>
Hello, the sum is 6
</pre></div>
<P>
What happens is that CherryPy first evaluates the expression (using <var>eval</var>) and then uses <var>str</var> on the result to convert it to a string.
<P>
All the CGTL tags can be closed using <var>/></var> or / >, like this:
<div class="verbatim"><pre>
<py-eval="'abcd' * 2" />
<py-eval="'Hello, %s' % name"/>
</pre></div>
<P>
In its CHTL form, the <var>py-eval</var> tag can be used with any pair of opening/closing tags, like this:
<P>
<div class="verbatim"><pre>
<span class=myClass py-eval="2*3"></span>
<a href="myHref" py-eval="'Click here'"></a>
<u py-eval="'I\'m with an underline'"></u>
...
</pre></div>
<P>
If you don't want to use any HTML tag around the expression, the trick is to use the <code><div></code> HTML tag:
<div class="verbatim"><pre>
This is a long string with a <div py-eval="'variable'"></div> in it
</pre></div>
<P>
<H2><A NAME="SECTION007120000000000000000">
5.1.2 py-attr</A>
</H2>
This tag is like <code>py-eval</code>, except it's used for HTML tag attributes. The way it is used is as follows:
<div class="verbatim"><pre>
<td py-attr="'red'" bgColor="">
</pre></div>
This will be rendered as
<div class="verbatim"><pre>
<td bgColor="red">
</pre></div>
Note that this is equivalent to:
<div class="verbatim"><pre>
<td bgColor="<py-eval="'red'">">
</pre></div>
But the first syntax is preferred.
<P>
<H2><A NAME="SECTION007130000000000000000">
5.1.3 py-exec and py-code</A>
</H2>
These tags are used to execute some Python code. <code>py-exec</code> is used to execute one line of Python code, and
<code>py-code</code> is used to execute blocks of Python code.
For instance, the following code:
<div class="verbatim"><pre>
<py-exec="a=2">
<py-code="
if a==2:
b=1
else:
b==2
">
b equals <py-eval="b">
</pre></div>
Will be rendered as:
<div class="verbatim"><pre>
b equals 1
</pre></div>
<P>
In the CHTL syntax, both tags have to be embedded in <div> and </div> tags as follow:
<div class="verbatim"><pre>
<div py-exec="a=2"></div>
<div py-code="
if a==2:
b=1
else:
b=2
"></div>
</pre></div>
<P>
If you want to render some data inside the Python code, you must append it to the <var>_page</var> variable:
<div class="verbatim"><pre>
<html><body>
Integers from 0 to 9:
<py-code="
for i in range(10):
_page.append("%s "%i)
">
</body></html>
</pre></div>
<P>
This will be rendered as:
<div class="verbatim"><pre>
<html><body>
Integers from 0 to 9:
0 1 2 3 4 5 6 7 8 9
</body></html>
</pre></div>
<P>
<H2><A NAME="SECTION007140000000000000000">
5.1.4 py-if and py-else</A>
</H2>
These tags are used like <code>if</code> and <code>else</code> in Python. The syntax is as follows:
<div class="verbatim"><pre>
<py-if="1==1">
OK
</py-if><py-else>
Not OK
</py-else>
</pre></div>
<P>
This will be rendered as
<div class="verbatim"><pre>
OK
</pre></div>
<P>
Note that if there is an <code>else</code> clause, the <code><div py-else></code> tag must follow the <code></div></code> tag
closing the <code><div py-if</code> tag, with no significant characters in between (ie: only separators are allowed)
<P>
The CHTL equivalent is:
<div class="verbatim"><pre>
<div py-if="1==1">
OK
</div>
<div py-else>
Not OK
</div>
</pre></div>
<P>
<H2><A NAME="SECTION007150000000000000000">
5.1.5 py-for</A>
</H2>
This tag is used like <code>for</code> in Python. The syntax is as follows:
<div class="verbatim"><pre>
<py-for="i in range(10)">
<py-eval="i">
</py-for>
</pre></div>
This will be rendered as
<div class="verbatim"><pre>
0 1 2 3 4 5 6 7 8 9
</pre></div>
<P>
Note that you can loop over a list of tuples:
<div class="verbatim"><pre>
<py-for="i,j in [(0,0), (0,1), (1,0), (1,1)]">
<py-eval="i+j">
</py-for>
</pre></div>
This will be rendered as
<div class="verbatim"><pre>
0 1 1 2
</pre></div>
<P>
The CGTL equivalent is:
<div class="verbatim"><pre>
<div py-for="i,j in [(0,0), (0,1), (1,0), (1,1)]">
<div py-eval="i+j"></div>
</div>
</pre></div>
<P>
In a <var>py-for</var> loop, CherryPy sets two handy special variables for you: <var>_index</var> and <var>_end</var>. The former
is an integer containing the current number of iterations (from 0 to n-1). The latter contains the total number of
iteration minus one.
<P>
For instance, if you want to display a list with the first item in bold and the last item underlined, you can use
the following code:
<div class="verbatim"><pre>
<py-exec="myList=[1,5,3,2,5,4,5]">
<py-for="item in myList">
<py-if="_index==0"><b py-eval="item"></b>
</py-if><py-else>
<py-if="_index==_end"><u py-eval="item"></u>
</py-if><py-else><py-eval="item"></py-else>
</div>
</py-else>
</py-for>
</pre></div>
This will be rendered as:
<div class="verbatim"><pre>
<b>1</b> 5 3 2 5 4 <u>5</u>
</pre></div>
<P>
In the next section, we'll see how to use all these tags together...
<P>
<H1><A NAME="SECTION007200000000000000000">
5.2 Putting it together</A>
</H1>
<P>
We are going to make a web page that displays a table with all HTML colors.
Edit the Hello.cpy file and change it to:
<P>
<div class="verbatim"><pre>
CherryClass Root:
mask:
def index(self):
<html><body>
<a py-attr="request.base+'/webColors'" href="">
Click here to see a nice table with all web colors
</a>
</body></html>
def webColors(self):
<html><body>
<py-exec="codeList=['00', '33', '66', '99', 'CC', 'FF']">
<table border=1>
<py-for="r in codeList">
<py-for="g in codeList">
<tr>
<py-for="b in codeList">
<py-exec="color='#%s%s%s'%(r,g,b)">
<td py-attr="color" bgColor="" py-eval="'&nbsp;&nbsp;'+color+'&nbsp;'"></td>
</py-for>
</tr>
</py-for>
</py-for>
</body></html>
</pre></div>
<P>
Recompile the file, restart the server and refresh the page in your browser. Click on the link and you should see
a nice table with all web colors.
<P>
How does it work ?
<P>
The <code>webColors</code> method is a pretty straight forward use of the CHTL tags.
One more interesting line is:
<div class="verbatim"><pre>
<a py-attr="request.base+'/webColors'" href="">
</pre></div>
<P>
<var>request</var> is a global variable set by CherryPy for each request of a client. It is an instance of a class with
several variables. One of them is called <code>base</code> and contains the base URL of the website (in our case: <a class="url" href="http://localhost:8000">http://localhost:8000</a>). So,
the line
<div class="verbatim"><pre>
<a py-attr="request.base+'/webColors'" href="">
</pre></div>
will be rendered as:
<div class="verbatim"><pre>
<a href="http://localhost:8000/webColors">
</pre></div>
This also tells us that when the browser requests the URL <a class="url" href="http://localhost:8000/webColors">http://localhost:8000/webColors</a>, the <var>webColors</var> method
of the <var>Root</var> class gets called.
<P>
In the next chapter, we'll learn how to use views, functions and when we should use them...
<P>
<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node6.html"><img src="../icons/previous.gif"
border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="tut.html"><img src="../icons/up.gif"
border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node8.html"><img src="../icons/next.gif"
border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy Tutorial</td>
<td><A HREF="node2.html"><img src="../icons/contents.gif"
border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node6.html">4. Creating a first</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node8.html">6. Views, functions and</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
|