comparison markup/template.py @ 61:448792ab1303 trunk

Use a different namespace than Kid uses.
author cmlenz
date Fri, 07 Jul 2006 17:54:52 +0000
parents 1f3cd91325d9
children b3fdf93057ab
comparison
equal deleted inserted replaced
60:2cb5b54d87ff 61:448792ab1303
197 197
198 The value of the `py:attrs` attribute should be a dictionary. The keys and 198 The value of the `py:attrs` attribute should be a dictionary. The keys and
199 values of that dictionary will be added as attributes to the element: 199 values of that dictionary will be added as attributes to the element:
200 200
201 >>> ctxt = Context(foo={'class': 'collapse'}) 201 >>> ctxt = Context(foo={'class': 'collapse'})
202 >>> tmpl = Template('''<ul xmlns:py="http://purl.org/kid/ns#"> 202 >>> tmpl = Template('''<ul xmlns:py="http://markup.edgewall.org/">
203 ... <li py:attrs="foo">Bar</li> 203 ... <li py:attrs="foo">Bar</li>
204 ... </ul>''') 204 ... </ul>''')
205 >>> print tmpl.generate(ctxt) 205 >>> print tmpl.generate(ctxt)
206 <ul> 206 <ul>
207 <li class="collapse">Bar</li> 207 <li class="collapse">Bar</li>
242 242
243 This directive replaces the content of the element with the result of 243 This directive replaces the content of the element with the result of
244 evaluating the value of the `py:content` attribute: 244 evaluating the value of the `py:content` attribute:
245 245
246 >>> ctxt = Context(bar='Bye') 246 >>> ctxt = Context(bar='Bye')
247 >>> tmpl = Template('''<ul xmlns:py="http://purl.org/kid/ns#"> 247 >>> tmpl = Template('''<ul xmlns:py="http://markup.edgewall.org/">
248 ... <li py:content="bar">Hello</li> 248 ... <li py:content="bar">Hello</li>
249 ... </ul>''') 249 ... </ul>''')
250 >>> print tmpl.generate(ctxt) 250 >>> print tmpl.generate(ctxt)
251 <ul> 251 <ul>
252 <li>Bye</li> 252 <li>Bye</li>
278 278
279 A named template function can be used just like a normal Python function 279 A named template function can be used just like a normal Python function
280 from template expressions: 280 from template expressions:
281 281
282 >>> ctxt = Context(bar='Bye') 282 >>> ctxt = Context(bar='Bye')
283 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 283 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
284 ... <p py:def="echo(greeting, name='world')" class="message"> 284 ... <p py:def="echo(greeting, name='world')" class="message">
285 ... ${greeting}, ${name}! 285 ... ${greeting}, ${name}!
286 ... </p> 286 ... </p>
287 ... ${echo('hi', name='you')} 287 ... ${echo('hi', name='you')}
288 ... </div>''') 288 ... </div>''')
292 hi, you! 292 hi, you!
293 </p> 293 </p>
294 </div> 294 </div>
295 295
296 >>> ctxt = Context(bar='Bye') 296 >>> ctxt = Context(bar='Bye')
297 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 297 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
298 ... <p py:def="echo(greeting, name='world')" class="message"> 298 ... <p py:def="echo(greeting, name='world')" class="message">
299 ... ${greeting}, ${name}! 299 ... ${greeting}, ${name}!
300 ... </p> 300 ... </p>
301 ... <div py:replace="echo('hello')"></div> 301 ... <div py:replace="echo('hello')"></div>
302 ... </div>''') 302 ... </div>''')
351 class ForDirective(Directive): 351 class ForDirective(Directive):
352 """Implementation of the `py:for` template directive for repeating an 352 """Implementation of the `py:for` template directive for repeating an
353 element based on an iterable in the context data. 353 element based on an iterable in the context data.
354 354
355 >>> ctxt = Context(items=[1, 2, 3]) 355 >>> ctxt = Context(items=[1, 2, 3])
356 >>> tmpl = Template('''<ul xmlns:py="http://purl.org/kid/ns#"> 356 >>> tmpl = Template('''<ul xmlns:py="http://markup.edgewall.org/">
357 ... <li py:for="item in items">${item}</li> 357 ... <li py:for="item in items">${item}</li>
358 ... </ul>''') 358 ... </ul>''')
359 >>> print tmpl.generate(ctxt) 359 >>> print tmpl.generate(ctxt)
360 <ul> 360 <ul>
361 <li>1</li><li>2</li><li>3</li> 361 <li>1</li><li>2</li><li>3</li>
391 class IfDirective(Directive): 391 class IfDirective(Directive):
392 """Implementation of the `py:if` template directive for conditionally 392 """Implementation of the `py:if` template directive for conditionally
393 excluding elements from being output. 393 excluding elements from being output.
394 394
395 >>> ctxt = Context(foo=True, bar='Hello') 395 >>> ctxt = Context(foo=True, bar='Hello')
396 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 396 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
397 ... <b py:if="foo">${bar}</b> 397 ... <b py:if="foo">${bar}</b>
398 ... </div>''') 398 ... </div>''')
399 >>> print tmpl.generate(ctxt) 399 >>> print tmpl.generate(ctxt)
400 <div> 400 <div>
401 <b>Hello</b> 401 <b>Hello</b>
410 410
411 411
412 class MatchDirective(Directive): 412 class MatchDirective(Directive):
413 """Implementation of the `py:match` template directive. 413 """Implementation of the `py:match` template directive.
414 414
415 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 415 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
416 ... <span py:match="greeting"> 416 ... <span py:match="greeting">
417 ... Hello ${select('@name')} 417 ... Hello ${select('@name')}
418 ... </span> 418 ... </span>
419 ... <greeting name="Dude" /> 419 ... <greeting name="Dude" />
420 ... </div>''') 420 ... </div>''')
447 447
448 This directive replaces the element with the result of evaluating the 448 This directive replaces the element with the result of evaluating the
449 value of the `py:replace` attribute: 449 value of the `py:replace` attribute:
450 450
451 >>> ctxt = Context(bar='Bye') 451 >>> ctxt = Context(bar='Bye')
452 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 452 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
453 ... <span py:replace="bar">Hello</span> 453 ... <span py:replace="bar">Hello</span>
454 ... </div>''') 454 ... </div>''')
455 >>> print tmpl.generate(ctxt) 455 >>> print tmpl.generate(ctxt)
456 <div> 456 <div>
457 Bye 457 Bye
459 459
460 This directive is equivalent to `py:content` combined with `py:strip`, 460 This directive is equivalent to `py:content` combined with `py:strip`,
461 providing a less verbose way to achieve the same effect: 461 providing a less verbose way to achieve the same effect:
462 462
463 >>> ctxt = Context(bar='Bye') 463 >>> ctxt = Context(bar='Bye')
464 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 464 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
465 ... <span py:content="bar" py:strip="">Hello</span> 465 ... <span py:content="bar" py:strip="">Hello</span>
466 ... </div>''') 466 ... </div>''')
467 >>> print tmpl.generate(ctxt) 467 >>> print tmpl.generate(ctxt)
468 <div> 468 <div>
469 Bye 469 Bye
480 """Implementation of the `py:strip` template directive. 480 """Implementation of the `py:strip` template directive.
481 481
482 When the value of the `py:strip` attribute evaluates to `True`, the element 482 When the value of the `py:strip` attribute evaluates to `True`, the element
483 is stripped from the output 483 is stripped from the output
484 484
485 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 485 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
486 ... <div py:strip="True"><b>foo</b></div> 486 ... <div py:strip="True"><b>foo</b></div>
487 ... </div>''') 487 ... </div>''')
488 >>> print tmpl.generate() 488 >>> print tmpl.generate()
489 <div> 489 <div>
490 <b>foo</b> 490 <b>foo</b>
493 Leaving the attribute value empty is equivalent to a truth value. 493 Leaving the attribute value empty is equivalent to a truth value.
494 494
495 This directive is particulary interesting for named template functions or 495 This directive is particulary interesting for named template functions or
496 match templates that do not generate a top-level element: 496 match templates that do not generate a top-level element:
497 497
498 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#"> 498 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
499 ... <div py:def="echo(what)" py:strip=""> 499 ... <div py:def="echo(what)" py:strip="">
500 ... <b>${what}</b> 500 ... <b>${what}</b>
501 ... </div> 501 ... </div>
502 ... ${echo('foo')} 502 ... ${echo('foo')}
503 ... </div>''') 503 ... </div>''')
533 directives are tested for truth. The first true `py:when` body is output. 533 directives are tested for truth. The first true `py:when` body is output.
534 If no `py:when` directive is matched then the fallback directive 534 If no `py:when` directive is matched then the fallback directive
535 `py:otherwise` will be used. 535 `py:otherwise` will be used.
536 536
537 >>> ctxt = Context() 537 >>> ctxt = Context()
538 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#" 538 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/"
539 ... py:choose=""> 539 ... py:choose="">
540 ... <span py:when="0 == 1">0</span> 540 ... <span py:when="0 == 1">0</span>
541 ... <span py:when="1 == 1">1</span> 541 ... <span py:when="1 == 1">1</span>
542 ... <span py:otherwise="">2</span> 542 ... <span py:otherwise="">2</span>
543 ... </div>''') 543 ... </div>''')
547 </div> 547 </div>
548 548
549 If the `py:choose` directive contains an expression, the nested `py:when` 549 If the `py:choose` directive contains an expression, the nested `py:when`
550 directives are tested for equality to the `py:choose` expression: 550 directives are tested for equality to the `py:choose` expression:
551 551
552 >>> tmpl = Template('''<div xmlns:py="http://purl.org/kid/ns#" 552 >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/"
553 ... py:choose="2"> 553 ... py:choose="2">
554 ... <span py:when="1">1</span> 554 ... <span py:when="1">1</span>
555 ... <span py:when="2">2</span> 555 ... <span py:when="2">2</span>
556 ... </div>''') 556 ... </div>''')
557 >>> print tmpl.generate(ctxt) 557 >>> print tmpl.generate(ctxt)
613 613
614 class Template(object): 614 class Template(object):
615 """Can parse a template and transform it into the corresponding output 615 """Can parse a template and transform it into the corresponding output
616 based on context data. 616 based on context data.
617 """ 617 """
618 NAMESPACE = Namespace('http://purl.org/kid/ns#') 618 NAMESPACE = Namespace('http://markup.edgewall.org/')
619 619
620 EXPR = StreamEventKind('EXPR') # an expression 620 EXPR = StreamEventKind('EXPR') # an expression
621 SUB = StreamEventKind('SUB') # a "subprogram" 621 SUB = StreamEventKind('SUB') # a "subprogram"
622 622
623 directives = [('def', DefDirective), 623 directives = [('def', DefDirective),
Copyright (C) 2012-2017 Edgewall Software