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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Templayer 1.4 Tutorial</title>
<style type="text/css">
.title { text-align: center; }
.sectionhead { margin: 40px 0 0 0; padding: 10px; background: #6d96e8;}
.subsectionhead { margin: 0 0 3px 0; padding: 12px 6px 6px 6px; background: #efef96;}
.code { background: #eeeecc; padding: 5px; margin: 7px 20px; }
.html { background: #aaaaee; padding: 5px; margin: 7px 20px; }
.eghead { background: #222233; color: white; padding: 5px; margin: 7px 20px; }
.egwrap {}
.result { border: solid black 2px; padding: 5px; margin: 7px 20px; }
.l1 { margin: 12px 0 0 0; }
.l2 { margin-left: 20px; }
.shot { padding: 5px 20px 5px 0px; float: left; }
.back { font-size:small; padding-left: 20px; }
</style>
<body>
<a name="top"></a>
<h1 class="title">Templayer 1.4 Tutorial</h1>
<div style="text-align: center;">
<a href="http://excess.org/templayer/">Templayer Home Page</a> /
Tutorial /
<a href="reference.html">Reference</a>
</div>
<br>
<table width="100%"><tr><td width="50%" valign="top">
<div class="l1">1. Gordon's Lawn Happenings</div>
<div class="l2"><a href="#lawn1">1.1. Static HTML — Before Templayer</a></div>
<div class="l2"><a href="#lawn2">1.2. Minimal Templayer Template</a></div>
<div class="l2"><a href="#lawn3">1.3. Using Slots</a></div>
<div class="l2"><a href="#lawn4">1.4. Using a Simple Layer</a></div>
<div class="l2"><a href="#lawn5">1.5. Using Nested Layers</a></div>
<div class="l2"><a href="#lawn6">1.6. Advanced Nested Layers</a></div>
</td><td width="50%" valign="top">
</td></tr></table>
<h2 class="sectionhead">1. Gordon's Lawn Happenings</h2>
<h3 class="subsectionhead"><a name="lawn1">1.1. Static HTML — Before Templayer</a>
<span class="back">[<a href="#top">back to top</a>]</span></h3>
<p>
Our examples will be based on a fictional web site that has a
single page:</p>
<div class="egwrap">
<div class="eghead">lawn1.html</div>
<pre class="html">
<html>
<head><title>Gordon's Lawn Happenings</title></head>
<body>
<h1>Gordon's Lawn Happenings</h1>
<h3>Sunday</h3>
<p>We've got a groundhog. I will have to stay alert.</p>
<p>I lost half a tomato plant to that furry guy.</p>
<h3>Monday</h3>
<p>The grass grew - I saw it.</p>
</body>
</html>
</pre>
</div>
<div class="result">
<div>
<html>
<head><title>Gordon's Lawn Happenings</title></head>
<body>
<h1>Gordon's Lawn Happenings</h1>
<h3>Sunday</h3>
<p>We've got a groundhog. I will have to stay alert.</p>
<p>I lost half a tomato plant to that furry guy.</p>
<h3>Monday</h3>
<p>The grass grew - I saw it.</p>
</body>
</html>
</div>
</div>
<p>
All the example source code may be extracted from the docgen_tutorial.py
script that is included with Templayer by running:
<pre>python docgen_tutorial.py -s</pre>
</p>
<br clear="left">
<br>
<h3 class="subsectionhead"><a name="lawn2">1.2. Minimal Templayer Template</a>
<span class="back">[<a href="#top">back to top</a>]</span></h3>
<p>The first thing we need to do to use this page as a Templayer template
file is to add a contents layer to it. This is done by adding the
strings "{contents}" and "{/contents}" to the file:</p>
<div class="egwrap">
<div class="eghead">lawn2.html</div>
<pre class="html">
<html>
<head><title>Gordon's Lawn Happenings</title></head>
<body>
<h1>Gordon's Lawn Happenings</h1>
{contents}
{/contents}
<h3>Sunday</h3>
<p>We've got a groundhog. I will have to stay alert.</p>
<p>I lost half a tomato plant to that furry guy.</p>
<h3>Monday</h3>
<p>The grass grew - I saw it.</p>
</body>
</html>
</pre>
</div>
<p>Templayer searches the template file for the first occurrence of
"{contents}" and the last occurrence of "{/contents}". This splits the
file into three parts: the header, the content and the footer.</p>
<p>The header and footer will appear in Templayer's output. The
content is described in sections below.</p>
<div class="egwrap">
<div class="eghead">lawn2.py</div>
<pre class="code">import templayer
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
tmpl = templayer.HTMLTemplate("lawn2.html")
file_layer = tmpl.start_file()
file_layer.open()
file_layer.close()
</pre>
</div>
<p>This is a minimal CGI script that uses Templayer to display the
web site. The result of running this script will be the same as
if we just used the static page above.</p>
<p>We first create an <a href="reference.html#HTMLTemplate">HTMLTemplate</a>
object to handle parsing of the template above.</p>
<p>The <a href="reference.html#HTMLTemplate-start_file">start_file</a> function
creates a <a href="reference.html#FileLayer">FileLayer</a> object that will
use the our HTMLTemplate object. The FileLayer object handles a single
output run for our template. The start_file function allows you
to pass in a file-like object to use instead of the
standard output. For a CGI script we use the default.</p>
<p>The FileLayer object's open function will be explained below.</p>
<p>No output is written until the
<a href="reference.html#FileLayer-close">close</a> function is called
at the end of the script. If you don't get any output from your script
it might be because you forgot to call the close function on your FileLayer
object.</p>
<br clear="left">
<br>
<h3 class="subsectionhead"><a name="lawn3">1.3. Using Slots</a>
<span class="back">[<a href="#top">back to top</a>]</span></h3>
<p>The next step is to add slots to the header and footer of the template.
Slots are like variables that can be filled in by the application.
In the template file slot names are wrapped in "%" characters:
</p>
<div class="egwrap">
<div class="eghead">lawn3.html</div>
<pre class="html">
<html>
<head><title>%title%</title></head>
<body>
<h1>%title%</h1>
{contents}
{/contents}
<h3>Sunday</h3>
<p>We've got a groundhog. I will have to stay alert.</p>
<p>I lost half a tomato plant to that furry guy.</p>
<h3>Monday</h3>
<p>The grass grew - I saw it.</p>
<hr>
<p>Generated on %date%.</p>
</body>
</html>
</pre>
</div>
<p>We have added two %title% slots to the header and one
%date% slot to the footer of this template.</p>
<p>Please note: "%" is <u>not</u> a special character in Templayer templates.
You do not need to escape "%" characters that aren't part of a slot.
When filling slots Templayer has a list of slot names and it only replaces
those slots, leaving all the other "%" characters in the template intact.
</p>
<div class="egwrap">
<div class="eghead">lawn3.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
tmpl = templayer.HTMLTemplate("lawn3.html")
file_layer = tmpl.start_file()
file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
file_layer.close()
</pre>
</div>
<p>Essentially only one line has changed in the code.
The <a href="reference.html#FileLayer-open">open</a> function takes
keyword parameters where the names of the parameters correspond to slots
in the header and footer. Here <i>both</i> %title% slots will be filled
with "Gordon's Lawn Happenings", and the %date% slot will be filled with
the current date and time.</p>
<p>The values passed in to fill slots are interpreted by our HTMLTemplate
object. The HTMLTemplate object expects HTML Markup described by the
<a href="reference.html#expand_html_markup">expand_html_markup</a> function.
Plain strings are treated as unsafe in HTML Markup and any characters
that might be interpreted as HTML are escaped by this function.
This means that Templayer's default behaviour is to escape values
being filled into HTML templates.</p>
<div class="result">
<div>
<html>
<head><title>Gordon's Lawn Happenings</title></head>
<body>
<h1>Gordon's Lawn Happenings</h1>
<h3>Sunday</h3>
<p>We've got a groundhog. I will have to stay alert.</p>
<p>I lost half a tomato plant to that furry guy.</p>
<h3>Monday</h3>
<p>The grass grew - I saw it.</p>
<hr>
<p>Generated on Mon Sep 25 10:10:39 2006.</p>
</body>
</html>
</div>
</div>
<br clear="left">
<br>
<h3 class="subsectionhead"><a name="lawn4">1.4. Using a Simple Layer</a>
<span class="back">[<a href="#top">back to top</a>]</span></h3>
<p>Now we look at what we can do with the content of a template file.
You will recall that this refers to everything between the first occurrence
of "{contents}" and the last occurrence of "{/contents}" in the template
file.</p>
<div class="egwrap">
<div class="eghead">lawn4.html</div>
<pre class="html">
<html>
<head><title>%title%</title></head>
<body>
<h1>%title%</h1>
{contents}
{report}
<h3>%day%</h3>
%happenings%
{/report}
{/contents}
<hr>
<p>Generated on %date%.</p>
</body>
</html>
</pre>
</div>
<p>We have now added a {report} layer to the template, and we have removed
the "Lawn Happenings" from the template so that the application can fill
them in. Layers are placed one after the next in the template content.
The {report} layer extends from the first occurrence of "{report}" to the
last occurrence of "{/report}" in the template content. Layers have slots
just like the template header and footer.</p>
<p>Splitting HTML into separate layers lets us reuse the layers
throughout the generated HTML, consolidating duplicated layout information.
This template format can also be loaded in a web browser to test changes
without running the application. Most templates can also be validated
for compliance with HTML and CSS specs.</p>
<div class="egwrap">
<div class="eghead">lawn4a.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
tmpl = templayer.HTMLTemplate("lawn4.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
main_layer.write_layer('report', day="Sunday", happenings=[
"We've got a groundhog. I will have to stay alert.",
"I lost half a tomato plant to that furry guy."])
main_layer.write_layer('report', day="Monday", happenings=[
"The grass grew - I saw it."])
file_layer.close()
</pre>
</div>
<p>We are now storing the return value of our FileLayer object's open
function. The return value is a <a href="reference.html#Layer">Layer</a>
object that represents the whole file. It knows the value of the header
and footer which were filled in the FileLayer object's
<a href="reference.html#FileLayer-open">open</a> function, but
it can still have text filled into its content between the header and
footer.<p>
<p>Layer objects have a
<a href="reference.html#Layer-write_layer">write_layer</a> function that
is passed a layer name followed by keyword parameters that correspond to
slots in that layer. Here we are adding two instances of our {report}
layer to the content of the template's main Layer object. Recall that
values passed in to fill slots are interpreted as HTML Markup by the
<a href="reference.html#expand_html_markup">expand_html_markup</a> function.
The "happenings" lists are escaped, then concatenated by this function.</p>
<p>Of course, we can separate the "Lawn Happenings" data from the code
by creating a structure for it:</p>
<div class="egwrap">
<div class="eghead">lawn4b.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
reports = [
('Sunday', ["We've got a groundhog. I will have to stay alert.",
"I lost half a tomato plant to that furry guy."]),
('Monday', ["The grass grew - I saw it."]),
]
tmpl = templayer.HTMLTemplate("lawn4.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
for d, h in reports:
main_layer.write_layer('report', day=d, happenings=h)
file_layer.close()
</pre>
</div>
<p>This structure is a list of (day, happenings) tuples that is iterated
through in the script. The day and happenings values are treated as
HTML Markup</p>
<div class="result">
<div>
<html>
<head><title>Gordon's Lawn Happenings</title></head>
<body>
<h1>Gordon's Lawn Happenings</h1>
<h3>Sunday</h3>
We've got a groundhog. I will have to stay alert.I lost half a tomato plant to that furry guy.
<h3>Monday</h3>
The grass grew - I saw it.
<hr>
<p>Generated on Mon Sep 25 10:10:39 2006.</p>
</body>
</html>
</div>
</div>
<p>Unfortunately, the HTML code that this generates is different than before
we added the {report} layer — the "Happenings" are no longer in separate
paragraphs. One way to fix this is to use
HTML Markup and avoid mixing actual HTML into the code:</p>
<div class="egwrap">
<div class="eghead">lawn4c.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
reports = [
('Sunday', [('p',"We've got a groundhog. I will have to stay alert."),
('p',"I lost half a tomato plant to that furry guy.")]),
('Monday', [('p',"The grass grew - I saw it.")]),
]
tmpl = templayer.HTMLTemplate("lawn4.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
for d, h in reports:
main_layer.write_layer('report', day=d, happenings=h)
file_layer.close()
</pre>
</div>
<p>The <a href="reference.html#expand_html_markup">expand_html_markup</a>
function will take the ('p', text) tuples,
escape the text, wrap them in HTML paragraph tags then concatenate them.</p>
<div class="result">
<div>
<html>
<head><title>Gordon's Lawn Happenings</title></head>
<body>
<h1>Gordon's Lawn Happenings</h1>
<h3>Sunday</h3>
<p>We've got a groundhog. I will have to stay alert.</p><p>I lost half a tomato plant to that furry guy.</p>
<h3>Monday</h3>
<p>The grass grew - I saw it.</p>
<hr>
<p>Generated on Mon Sep 25 10:10:39 2006.</p>
</body>
</html>
</div>
</div>
<br clear="left">
<br>
<h3 class="subsectionhead"><a name="lawn5">1.5. Using Nested Layers</a>
<span class="back">[<a href="#top">back to top</a>]</span></h3>
<p>HTML Markup is not intended for anything beyond very simple formatting.
A more flexible solution is to create another layer to nest within the
first.</p>
<div class="egwrap">
<div class="eghead">lawn5.html</div>
<pre class="html">
<html>
<head><title>%title%</title></head>
<body>
<h1>%title%</h1>
{contents}
{report}
<h3>%day%</h3>
%happenings%
{/report}
{happening}<p>%what%</p>
{/happening}
{/contents}
<hr>
<p>Generated on %date%.</p>
</body>
</html>
</pre>
</div>
<p>Now the decision to format individual "Happenings" as paragraphs has
been moved to the template in a {happening} layer.</p>
<div class="egwrap">
<div class="eghead">lawn5.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
reports = [
('Sunday', ["We've got a groundhog. I will have to stay alert.",
"I lost half a tomato plant to that furry guy."]),
('Monday', ["The grass grew - I saw it."]),
]
tmpl = templayer.HTMLTemplate("lawn5.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
for d, h in reports:
happening_list = []
for w in h:
formatted = tmpl.format('happening', what=w)
happening_list.append(formatted)
main_layer.write_layer('report', day=d, happenings=happening_list)
file_layer.close()
</pre>
</div>
<p>Within the new inner loop we are calling our HTMLTemplate object's
<a href="reference.html#HTMLTemplate-format">format</a> function. This
function is similar to the write_layer function except that it returns
a <a href="reference.html#RawHTML">RawHTML</a> object instead of adding
it to the content of a Layer object. The
<a href="reference.html#expand_html_markup">expand_html_markup</a> function
will leave the contents of the RawHTML object intact, so we can use this
object within HTML Markup.
</p>
<br clear="left">
<br>
<h3 class="subsectionhead"><a name="lawn6">1.6. Advanced Nested Layers</a>
<span class="back">[<a href="#top">back to top</a>]</span></h3>
<p>Since it is common to nest layers in Templayer another method is provided
that is similar to FileLayer's open function. First we need to rename a
slot in the {report} to %contents%:</p>
<div class="egwrap">
<div class="eghead">lawn6.html</div>
<pre class="html">
<html>
<head><title>%title%</title></head>
<body>
<h1>%title%</h1>
{contents}
{report}
<h3>%day%</h3>
%contents%
{/report}
{happening}<p>%contents%</p>
{/happening}
{/contents}
<hr>
<p>Generated on %date%.</p>
</body>
</html>
</pre>
</div>
<p>Now that we have a %contents% slot we can "open" this layer and write
into it:</p>
<div class="egwrap">
<div class="eghead">lawn6a.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
reports = [
('Sunday', ["We've got a groundhog. I will have to stay alert.",
"I lost half a tomato plant to that furry guy."]),
('Monday', ["The grass grew - I saw it."]),
]
tmpl = templayer.HTMLTemplate("lawn6.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
for d, h in reports:
report_layer = main_layer.open_layer('report', day=d)
for happening in h:
report_layer.write_layer('happening', contents=happening)
file_layer.close()
</pre>
</div>
<p>The Layer object has an
<a href="reference.html#Layer-open_layer">open_layer</a> function that
returns a new layer object. In this case the new Layer object represents
a {report} layer that is being written. The Layer object stores everything
above the %contents% slot as its header and everything below as its footer.
It can have text filled into its content in the same way as our main
Layer object. Here we are now using
<a href="reference.html#Layer-write_layer">write_layer</a> to fill
{happening} layers into {report} layers.</p>
<p>We also renamed the slot in {happening} to %contents%, so we can use
the open_layer function on that as well:</p>
<div class="egwrap">
<div class="eghead">lawn6b.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
reports = [
('Sunday', ["We've got a groundhog. I will have to stay alert.",
"I lost half a tomato plant to that furry guy."]),
('Monday', ["The grass grew - I saw it."]),
]
tmpl = templayer.HTMLTemplate("lawn6.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
for d, h in reports:
report_layer = main_layer.open_layer('report', day=d)
for happening in h:
happening_layer = report_layer.open_layer('happening')
happening_layer.write(happening)
file_layer.close()
</pre>
</div>
<p>We want to send text into this new Layer object, so instead of calling
write_layer we call the <a href="reference.html#Layer-write">write</a>
function. It takes a single parameter that is interpreted as HTML Markup.</p>
<p>When using Templayer to produce very large files, or when parts of an HTML
page take longer to complete, you might want to flush your output part
of the way through:</p>
<div class="egwrap">
<div class="eghead">lawn6c.py</div>
<pre class="code">import templayer
import time
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
reports = [
('Sunday', ["We've got a groundhog. I will have to stay alert.",
"I lost half a tomato plant to that furry guy."]),
('Monday', ["The grass grew - I saw it."]),
]
tmpl = templayer.HTMLTemplate("lawn6.html")
file_layer = tmpl.start_file()
main_layer = file_layer.open(title="Gordon's Lawn Happenings",
date=time.asctime())
for d, h in reports:
report_layer = main_layer.open_layer('report', day=d)
for happening in h:
happening_layer = report_layer.open_layer('happening')
happening_layer.write(happening)
main_layer.close_child()
file_layer.flush()
file_layer.close()
</pre>
</div>
<p>Layer objects have a
<a href="reference.html#Layer-close_child">close_child</a> function that
forces any open "child" layers to finalize their output. In this code
we are closing the {report} layer. FileLayer objects have a
<a href="reference.html#FileLayer-flush">flush</a> function that will
send all the output possible. We are first closing our {report} layer
so that when we flush the output the whole report will appear.</p>
<br clear="left">
<br>
</body>
</html>
|