/usr/share/pyshared/z3c.template-1.4.1.egg-info/PKG-INFO is in python-z3c.template 1.4.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 | Metadata-Version: 1.1
Name: z3c.template
Version: 1.4.1
Summary: A package implementing advanced Page Template patterns.
Home-page: http://pypi.python.org/pypi/z3c.template
Author: Roger Ineichen and the Zope Community
Author-email: zope-dev@zope.org
License: ZPL 2.1
Description: ------------
Z3C template
------------
This package allows you to register templates independently from view code.
In Zope 3, when registering a `browser:page` both presentation and computation
are registered together. Unfortunately the registration tangles presentation
and computation so tightly that it is not possible to re-register a different
template depending on context. (You can override the whole registration but
this is not the main point of this package.)
With z3c.template the registration is split up between the view and the
template and allows to differentiate the template based on the skin layer and
the view.
In addition this package lays the foundation to differentiate between
templates that provide specific presentation templates and generic layout
templates.
.. contents::
=============
Z3C Templates
=============
This package allows us to separate the registration of the view code and the
layout.
A template is used for separate the HTML part from a view. This is done in
z3 via a page templates. Such page template are implemented in the view,
registered included in a page directive etc. But they do not use the adapter
pattern which makes it hard to replace existing templates.
Another part of template is, that they normaly separate one part presenting
content from a view and another part offer a layout used by the content
template.
How can this package make it simpler to use templates?
Templates can be registered as adapters adapting context, request where the
context is a view implementation. Such a template get adapted from the view
if the template is needed. This adaption makes it very pluggable and modular.
We offer two base template directive for register content producing templates
and layout producing tempaltes. This is most the time enough but you also
can register different type of templates using a specific interface. This
could be usefull if your view implementation needs to separate HTMl in
more then one template. Now let's take a look how we an use this templates.
Content template
----------------
First let's show how we use a template for produce content from a view:
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt')
>>> open(contentTemplate, 'w').write('''<div>demo content</div>''')
And register a view class implementing a interface:
>>> import zope.interface
>>> from z3c.template import interfaces
>>> from zope.pagetemplate.interfaces import IPageTemplate
>>> from zope.publisher.browser import BrowserPage
>>> class IMyView(zope.interface.Interface):
... pass
>>> class MyView(BrowserPage):
... zope.interface.implements(IMyView)
... template = None
... def render(self):
... if self.template is None:
... template = zope.component.getMultiAdapter(
... (self, self.request), interfaces.IContentTemplate)
... return template(self)
... return self.template()
Let's call the view and check the output:
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> view = MyView(root, request)
Since the template is not yet registered, rendering the view will fail:
>>> print view.render()
Traceback (most recent call last):
...
ComponentLookupError: ......
Let's now register the template (commonly done using ZCML):
>>> from zope import component
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> from z3c.template.template import TemplateFactory
The template factory allows us to create a ViewPageTeplateFile instance.
>>> factory = TemplateFactory(contentTemplate, 'text/html')
>>> factory
<z3c.template.template.TemplateFactory object at ...>
We register the factory on a view interface and a layer.
>>> component.provideAdapter(
... factory,
... (zope.interface.Interface, IDefaultBrowserLayer),
... interfaces.IContentTemplate)
>>> template = component.getMultiAdapter((view, request),
... interfaces.IPageTemplate)
>>> template
<...ViewPageTemplateFile...>
Now that we have a registered layout template for the default layer we can
call our view again.
>>> print view.render()
<div>demo content</div>
Now we register a new template on the specific interface of our view.
>>> myTemplate = os.path.join(temp_dir, 'myTemplate.pt')
>>> open(myTemplate, 'w').write('''<div>My content</div>''')
>>> factory = TemplateFactory(myTemplate, 'text/html')
>>> component.provideAdapter(
... factory,
... (IMyView, IDefaultBrowserLayer), interfaces.IContentTemplate)
>>> print view.render()
<div>My content</div>
It is possible to provide the template directly.
We create a new template.
>>> viewContent = os.path.join(temp_dir, 'viewContent.pt')
>>> open(viewContent, 'w').write('''<div>view content</div>''')
and a view:
>>> from z3c.template import ViewPageTemplateFile
>>> class MyViewWithTemplate(BrowserPage):
... zope.interface.implements(IMyView)
... template = ViewPageTemplateFile(viewContent)
... def render(self):
... if self.template is None:
... template = zope.component.getMultiAdapter(
... (self, self.request), interfaces.IContentTemplate)
... return template(self)
... return self.template()
>>> contentView = MyViewWithTemplate(root, request)
If we render this view we get the implemented layout template and not the
registered one.
>>> print contentView.render()
<div>view content</div>
Layout template
---------------
First we nee to register a new view class calling a layout template. Note,
that this view uses the __call__ method for invoke a layout template:
>>> class ILayoutView(zope.interface.Interface):
... pass
>>> class LayoutView(BrowserPage):
... zope.interface.implements(ILayoutView)
... layout = None
... def __call__(self):
... if self.layout is None:
... layout = zope.component.getMultiAdapter(
... (self, self.request), interfaces.ILayoutTemplate)
... return layout(self)
... return self.layout()
>>> view2 = LayoutView(root, request)
Define and register a new layout template:
>>> layoutTemplate = os.path.join(temp_dir, 'layoutTemplate.pt')
>>> open(layoutTemplate, 'w').write('''<div>demo layout</div>''')
>>> factory = TemplateFactory(layoutTemplate, 'text/html')
We register the template factory on a view interface and a layer providing the
ILayoutTemplate interface.
>>> component.provideAdapter(factory,
... (zope.interface.Interface, IDefaultBrowserLayer),
... interfaces.ILayoutTemplate)
>>> layout = component.getMultiAdapter(
... (view2, request), interfaces.ILayoutTemplate)
>>> layout
<...ViewPageTemplateFile...>
Now that we have a registered layout template for the default layer we can
call our view again.
>>> print view2()
<div>demo layout</div>
Now we register a new layout template on the specific interface of our view.
>>> myLayout = os.path.join(temp_dir, 'myLayout.pt')
>>> open(myLayout, 'w').write('''<div>My layout</div>''')
>>> factory = TemplateFactory(myLayout, 'text/html')
>>> component.provideAdapter(factory,
... (ILayoutView, IDefaultBrowserLayer),
... interfaces.ILayoutTemplate)
>>> print view2()
<div>My layout</div>
It is possible to provide the layout template directly.
We create a new template.
>>> viewLayout = os.path.join(temp_dir, 'viewLayout.pt')
>>> open(viewLayout, 'w').write('''<div>view layout</div>''')
>>> class LayoutViewWithLayoutTemplate(BrowserPage):
... zope.interface.implements(ILayoutView)
... layout = ViewPageTemplateFile(viewLayout)
... def __call__(self):
... if self.layout is None:
... layout = zope.component.getMultiAdapter((self, self.request),
... interfaces.ILayoutTemplate)
... return layout(self)
... return self.layout()
>>> layoutView = LayoutViewWithLayoutTemplate(root, request)
If we render this view we get the implemented layout template and not the
registered one.
>>> print layoutView()
<div>view layout</div>
Since we return the layout template in the sample views above, how can we get
the content from the used view? This is not directly a part of this package
but let's show some pattern were can be used for render content in a used
layout template. Note, since we offer to register each layout template for
a specific view, you can always very selectiv this layout pattern. This means
you can use the defualt z3 macro based layout registration in combination with
this layout concept if you register a own layout template.
The simplest concept is calling the content from the view in the layout
template is to call it from a method. Let's define a view providing a layout
template and offer a method for call content.
>>> class IFullView(zope.interface.Interface):
... pass
>>> class FullView(BrowserPage):
... zope.interface.implements(IFullView)
... layout = None
... def render(self):
... return u'rendered content'
... def __call__(self):
... if self.layout is None:
... layout = zope.component.getMultiAdapter((self, self.request),
... interfaces.ILayoutTemplate)
... return layout(self)
... return self.layout()
>>> completeView = FullView(root, request)
Now define a layout for the view and register them:
>>> completeLayout = os.path.join(temp_dir, 'completeLayout.pt')
>>> open(completeLayout, 'w').write('''
... <div tal:content="view/render">
... Full layout
... </div>
... ''')
>>> factory = TemplateFactory(completeLayout, 'text/html')
>>> component.provideAdapter(factory,
... (IFullView, IDefaultBrowserLayer), interfaces.ILayoutTemplate)
Now let's see if the layout template can call the content via calling render
on the view:
>>> print completeView.__call__()
<div>rendered content</div>
Content and Layout
------------------
Now let's show how we combine this two templates in a real use case:
>>> class IDocumentView(zope.interface.Interface):
... pass
>>> class DocumentView(BrowserPage):
... zope.interface.implements(IDocumentView)
... template = None
... layout = None
... attr = None
... def update(self):
... self.attr = u'content updated'
... def render(self):
... if self.template is None:
... template = zope.component.getMultiAdapter(
... (self, self.request), IPageTemplate)
... return template(self)
... return self.template()
... def __call__(self):
... self.update()
... if self.layout is None:
... layout = zope.component.getMultiAdapter((self, self.request),
... interfaces.ILayoutTemplate)
... return layout(self)
... return self.layout()
Define and register a content template...
>>> template = os.path.join(temp_dir, 'template.pt')
>>> open(template, 'w').write('''
... <div tal:content="view/attr">
... here comes the value of attr
... </div>
... ''')
>>> factory = TemplateFactory(template, 'text/html')
>>> component.provideAdapter(factory,
... (IDocumentView, IDefaultBrowserLayer), IPageTemplate)
and define and register a layout template:
>>> layout = os.path.join(temp_dir, 'layout.pt')
>>> open(layout, 'w').write('''
... <html>
... <body>
... <div tal:content="structure view/render">
... here comes the rendered content
... </div>
... </body>
... </html>
... ''')
>>> factory = TemplateFactory(layout, 'text/html')
>>> component.provideAdapter(factory,
... (IDocumentView, IDefaultBrowserLayer), interfaces.ILayoutTemplate)
Now call the view and check the result:
>>> documentView = DocumentView(root, request)
>>> print documentView()
<html>
<body>
<div>
<div>content updated</div>
</div>
</body>
</html>
Macros
------
Use of macros.
>>> macroTemplate = os.path.join(temp_dir, 'macroTemplate.pt')
>>> open(macroTemplate, 'w').write('''
... <metal:block define-macro="macro1">
... <div>macro1</div>
... </metal:block>
... <metal:block define-macro="macro2">
... <div>macro2</div>
... <div tal:content="options/div2">the content of div 2</div>
... </metal:block>
... ''')
>>> factory = TemplateFactory(macroTemplate, 'text/html', 'macro1')
>>> print factory(view, request)()
<div>macro1</div>
>>> m2factory = TemplateFactory(macroTemplate, 'text/html', 'macro2')
>>> print m2factory(view, request)(div2="from the options")
<div>macro2</div>
<div>from the options</div>
Why didn't we use named templates from the ``zope.formlib`` package?
While named templates allow us to separate the view code from the template
registration, they are not registrable for a particular layer making it
impossible to implement multiple skins using named templates.
Use case ``simple template``
----------------------------
And for the simplest possible use we provide a hook for call registered
templates. Such page templates can get called with the getPageTemplate method
and return a registered bound ViewTemplate a la ViewPageTemplateFile or
NamedTemplate.
The getViewTemplate allows us to use the new template registration
system with all existing implementations such as `zope.formlib` and
`zope.viewlet`.
>>> from z3c.template.template import getPageTemplate
>>> class IUseOfViewTemplate(zope.interface.Interface):
... pass
>>> class UseOfViewTemplate(object):
... zope.interface.implements(IUseOfViewTemplate)
...
... template = getPageTemplate()
...
... def __init__(self, context, request):
... self.context = context
... self.request = request
By defining the "template" property as a "getPageTemplate" a lookup for
a registered template is done when it is called.
>>> simple = UseOfViewTemplate(root, request)
>>> print simple.template()
<div>demo content</div>
Because the demo template was registered for any ("None") interface we see the
demo template when rendering our new view. We register a new template
especially for the new view. Also note that the "macroTemplate" has been
created earlier in this test.
>>> factory = TemplateFactory(contentTemplate, 'text/html')
>>> component.provideAdapter(factory,
... (IUseOfViewTemplate, IDefaultBrowserLayer), IPageTemplate)
>>> print simple.template()
<div>demo content</div>
Context-specific templates
--------------------------
The ``TemplateFactory`` can be also used for (view, request, context)
lookup. It's useful when you want to override a template for specific
content object or type.
Let's define a sample content type and instantiate a view for it.
>>> class IContent(zope.interface.Interface):
... pass
>>> class Content(object):
... zope.interface.implements(IContent)
>>> content = Content()
>>> view = UseOfViewTemplate(content, request)
Now, let's provide a (view, request, context) adapter using TemplateFactory.
>>> contextTemplate = os.path.join(temp_dir, 'context.pt')
>>> open(contextTemplate, 'w').write('<div>context-specific</div>')
>>> factory = TemplateFactory(contextTemplate, 'text/html')
>>> component.provideAdapter(factory,
... (IUseOfViewTemplate, IDefaultBrowserLayer, IContent),
... interfaces.IContentTemplate)
First. Let's try to simply get it as a multi-adapter.
>>> template = zope.component.getMultiAdapter((view, request, content),
... interfaces.IContentTemplate)
>>> print template(view)
<div>context-specific</div>
The ``getPageTemplate`` and friends will try to lookup a context-specific
template before doing more generic (view, request) lookup, so our view
should already use our context-specific template:
>>> print view.template()
<div>context-specific</div>
Use case ``template by interface``
----------------------------------
Templates can also get registered on different interfaces then IPageTemplate
or ILayoutTemplate.
>>> from z3c.template.template import getViewTemplate
>>> class IMyTemplate(zope.interface.Interface):
... """My custom tempalte marker."""
>>> factory = TemplateFactory(contentTemplate, 'text/html')
>>> component.provideAdapter(factory,
... (zope.interface.Interface, IDefaultBrowserLayer), IMyTemplate)
Now define a view using such a custom template registration:
>>> class IMyTemplateView(zope.interface.Interface):
... pass
>>> class MyTemplateView(object):
... zope.interface.implements(IMyTemplateView)
...
... template = getViewTemplate(IMyTemplate)
...
... def __init__(self, context, request):
... self.context = context
... self.request = request
>>> myTempalteView = MyTemplateView(root, request)
>>> print myTempalteView.template()
<div>demo content</div>
Use case ``named template``
----------------------------------
Templates can also get registered on names. In this expample we use a named
template combined with a custom template marker interface.
>>> class IMyNamedTemplate(zope.interface.Interface):
... """My custom template marker."""
>>> factory = TemplateFactory(contentTemplate, 'text/html')
>>> component.provideAdapter(factory,
... (zope.interface.Interface, IDefaultBrowserLayer), IMyNamedTemplate,
... name='my template')
Now define a view using such a custom named template registration:
>>> class IMyNamedTemplateView(zope.interface.Interface):
... pass
>>> class MyNamedTemplateView(object):
... zope.interface.implements(IMyNamedTemplateView)
...
... template = getViewTemplate(IMyNamedTemplate, 'my template')
...
... def __init__(self, context, request):
... self.context = context
... self.request = request
>>> myNamedTempalteView = MyNamedTemplateView(root, request)
>>> print myNamedTempalteView.template()
<div>demo content</div>
Use case ``named layout template``
----------------------------------
We can also register a new layout template by name and use it in a view:
>>> from z3c.template.template import getLayoutTemplate
>>> editLayout = os.path.join(temp_dir, 'editLayout.pt')
>>> open(editLayout, 'w').write('''
... <div>Edit layout</div>
... <div tal:content="view/render">content</div>
... ''')
>>> factory = TemplateFactory(editLayout, 'text/html')
>>> component.provideAdapter(factory,
... (zope.interface.Interface, IDefaultBrowserLayer),
... interfaces.ILayoutTemplate, name='edit')
Now define a view using such a custom named template registration:
>>> class MyEditView(BrowserPage):
...
... layout = getLayoutTemplate('edit')
...
... def render(self):
... return u'edit content'
...
... def __call__(self):
... if self.layout is None:
... layout = zope.component.getMultiAdapter((self, self.request),
... interfaces.ILayoutTemplate)
... return layout(self)
... return self.layout()
>>> myEditView = MyEditView(root, request)
>>> print myEditView()
<div>Edit layout</div>
<div>edit content</div>
Cleanup
-------
>>> import shutil
>>> shutil.rmtree(temp_dir)
Pagelet
-------
See z3c.pagelet for another template based layout generating implementation.
==================
Template directive
==================
Show how we can use the template directive. Register the meta configuration for
the directive.
>>> import sys
>>> from zope.configuration import xmlconfig
>>> import z3c.template
>>> context = xmlconfig.file('meta.zcml', z3c.template)
PageTemplate
------------
We need a custom content template
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> file = os.path.join(temp_dir, 'content.pt')
>>> open(file, 'w').write('''<div>content</div>''')
and a interface
>>> import zope.interface
>>> class IView(zope.interface.Interface):
... """Marker interface"""
and a view class:
>>> from zope.publisher.browser import TestRequest
>>> class View(object):
... zope.interface.implements(IView)
... def __init__(self, context, request):
... self.context = context
... self.request = request
>>> request = TestRequest()
>>> view = View(object(), request)
Make them available under the fake package ``custom``:
>>> sys.modules['custom'] = type(
... 'Module', (),
... {'IView': IView})()
and register them as a template within the ``z3c:template`` directive:
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... template="%s"
... for="custom.IView"
... />
... </configure>
... """ % file, context=context)
Let's get the template
>>> import zope.component
>>> from z3c.template.interfaces import IContentTemplate
>>> template = zope.component.queryMultiAdapter(
... (view, request),
... interface=IContentTemplate)
and check them:
>>> from z3c.template.template import ViewPageTemplateFile
>>> isinstance(template, ViewPageTemplateFile)
True
>>> print template(view)
<div>content</div>
Layout template
---------------
Define a layout template
>>> file = os.path.join(temp_dir, 'layout.pt')
>>> open(file, 'w').write('''<div>layout</div>''')
and register them as a layout template within the ``z3c:layout`` directive:
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:layout
... template="%s"
... for="custom.IView"
... />
... </configure>
... """ % file, context=context)
Let's get the template
>>> from z3c.template.interfaces import ILayoutTemplate
>>> layout = zope.component.queryMultiAdapter((view, request),
... interface=ILayoutTemplate)
and check them:
>>> isinstance(template, ViewPageTemplateFile)
True
>>> print layout(view)
<div>layout</div>
Context-specific template
-------------------------
Most of views have some object as their context and it's ofter very
useful to be able register context-specific template. We can do that
using the ``context`` argument of the ZCML directive.
Let's define some content type:
>>> class IContent(zope.interface.Interface):
... pass
>>> class Content(object):
... zope.interface.implements(IContent)
>>> sys.modules['custom'].IContent = IContent
Now, we can register a template for this class. Let's create one and
register:
>>> file = os.path.join(temp_dir, 'context.pt')
>>> open(file, 'w').write('''<div>i'm context-specific</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... template="%s"
... for="custom.IView"
... context="custom.IContent"
... />
... </configure>
... """ % file, context=context)
We can now lookup it using the (view, request, context) discriminator:
>>> content = Content()
>>> view = View(content, request)
>>> template = zope.component.queryMultiAdapter((view, request, content),
... interface=IContentTemplate)
>>> print template(view)
<div>i'm context-specific</div>
The same will work with layout registration directive:
>>> file = os.path.join(temp_dir, 'context_layout.pt')
>>> open(file, 'w').write('''<div>context-specific layout</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:layout
... template="%s"
... for="custom.IView"
... context="custom.IContent"
... />
... </configure>
... """ % file, context=context)
>>> layout = zope.component.queryMultiAdapter((view, request, content),
... interface=ILayoutTemplate)
>>> print layout(view)
<div>context-specific layout</div>
Named template
--------------
Its possible to register template by name. Let us register a pagelet with the
name edit:
>>> editTemplate = os.path.join(temp_dir, 'edit.pt')
>>> open(editTemplate, 'w').write('''<div>edit</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... name="edit"
... template="%s"
... for="custom.IView"
... />
... </configure>
... """ % editTemplate, context=context)
And call it:
>>> from z3c.template.interfaces import ILayoutTemplate
>>> template = zope.component.queryMultiAdapter(
... (view, request),
... interface=IContentTemplate, name='edit')
>>> print template(view)
<div>edit</div>
Custom template
---------------
Or you can define own interfaces and register templates for them:
>>> from zope.pagetemplate.interfaces import IPageTemplate
>>> class IMyTemplate(IPageTemplate):
... """My template"""
Make the template interface available as a custom module class.
>>> sys.modules['custom'].IMyTemplate = IMyTemplate
Dfine a new template
>>> interfaceTemplate = os.path.join(temp_dir, 'interface.pt')
>>> open(interfaceTemplate, 'w').write('''<div>interface</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... template="%s"
... for="custom.IView"
... provides="custom.IMyTemplate"
... />
... </configure>
... """ % interfaceTemplate, context=context)
Let's see if we get the template by the new interface:
>>> from z3c.template.interfaces import ILayoutTemplate
>>> template = zope.component.queryMultiAdapter((view, request),
... interface=IMyTemplate,)
>>> print template(view)
<div>interface</div>
Cleanup
-------
Now we need to clean up the custom module.
>>> del sys.modules['custom']
=======
CHANGES
=======
1.4.1 (2012-02-15)
------------------
- Remove hooks to use ViewPageTemplateFile from z3c.pt because this breaks when
z3c.pt is available, but z3c.ptcompat is not included. As recommended by notes
below.
1.4.0 (2011-10-29)
------------------
- Moved z3c.pt include to extras_require chameleon. This makes the package
independent from chameleon and friends and allows to include this
dependencies in your own project.
- Upgrade to chameleon 2.0 template engine and use the newest z3c.pt and
z3c.ptcompat packages adjusted to work with chameleon 2.0.
See the notes from the z3c.ptcompat package:
Update z3c.ptcompat implementation to use component-based template engine
configuration, plugging directly into the Zope Toolkit framework.
The z3c.ptcompat package no longer provides template classes, or ZCML
directives; you should import directly from the ZTK codebase.
Note that the ``PREFER_Z3C_PT`` environment option has been
rendered obsolete; instead, this is now managed via component
configuration.
Also note that the chameleon CHAMELEON_CACHE environment value changed from
True/False to a path. Skip this property if you don't like to use a cache.
None or False defined in buildout environment section doesn't work. At least
with chameleon <= 2.5.4
Attention: You need to include the configure.zcml file from z3c.ptcompat
for enable the z3c.pt template engine. The configure.zcml will plugin the
template engine. Also remove any custom built hooks which will import
z3c.ptcompat in your tests or other places.
1.3.0 (2011-10-28)
------------------
- Update to z3c.ptcompat 1.0 (and as a result, to the z3c.pt 2.x series).
- Using Python's ``doctest`` module instead of depreacted
``zope.testing.doctest``.
1.2.1 (2009-08-22)
------------------
* Corrected description of ``ITemplateDirective.name``.
* Added `zcml.txt` to ``long_description`` to show up on pypi.
* Removed zpkg helper files and zcml slugs.
1.2.0 (2009-02-26)
------------------
* Add support for context-specific templates. Now, templates can be
registered and looked up using (view, request, context) triple.
To do that, pass the ``context`` argument to the ZCML directives.
The ``getPageTemplate`` and friends will now try to lookup context
specific template first and then fall back to (view, request) lookup.
* Allow use of ``z3c.pt`` using ``z3c.ptcompat`` compatibility layer.
* Forward the template kwargs to the options of the macro
* Changed package's mailing list address to zope-dev at zope.org
instead of retired one.
1.1.0 (2007-10-08)
------------------
* Added an ``IContentTemplate`` interface which is used for
``<z3c:template>``.
1.0.0 (2007-??-??)
------------------
* Initial release.
Keywords: zope3 template layout zpt pagetemplate
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Zope Public License
Classifier: Programming Language :: Python
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Framework :: Zope3
|