Dynamic HTML template ===================== For this demonstration, an example template has been prepared. >>> filename = os.path.join(path, 'layouts', 'example.html') We instantiate the dynamic HTML template like a normal Chameleon template, passing the filename as an argument. >>> from chameleon.html.template import DynamicHTMLFile >>> template = DynamicHTMLFile(filename) Dynamic content and attributes are passed in as keyword arguments to the template. The ``content`` parameter should be passed a dict with keys corresponding to dynamic content names and values being unicode strings. >>> content = {'title': u"Document title", ... 'content': u"Document content"} The ``attributes`` parameters is a double dictionary where the first level of values are element attribute dictionaries, e.g. the second dictionary is a mapping of element attribute names and values. >>> attributes={'meta-generator': {'content': u"Document generator"}, ... 'meta-description': {'content': u"Document description"}, ... 'meta-keywords': {'content': u"Document keywords"}} Passing these arguments by keyword, we can verify the rendered output. >>> print template(content=content, attributes=attributes)
Document content
If we pass in ``context`` and ``request`` arguments, resources referenced in the template are rebased if a resource location component is available. >>> def resource_location(context, request, abs_path): ... return 'http://host/%s' % abs_path[len(path+'/layouts')+1:] >>> from chameleon.html.interfaces import IResourceLocation >>> component.provideAdapter( ... resource_location, ... (interface.Interface, interface.Interface, interface.Interface), ... IResourceLocation) Notice in the following that URLs of resource is rebased as defined in the adapter. >>> print template(context=object(), request=object())
Example layout
This is the content area.
Layouts as macros ----------------- Layouts may be used as macros in which case the slots are filled using METAL's `fill-slot` construct. >>> from chameleon.core.config import SYMBOLS >>> print template(**{ ... SYMBOLS.slot+"title": u"Title from macro"})
... Title from macro ...
Globbing support for easy resource inclusion -------------------------------------------- It's tedious to inlude all the resources for a template manually. Let's use simple globbing:: >>> filename = os.path.join(path, 'layouts', 'example2.html') >>> template = DynamicHTMLFile(filename) >>> print template(context=object(), request=object())
This is the content area.