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)
  <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Document title</title>
      <!-- meta -->
      <meta content="Document generator" name="generator" />
      <meta content="Document description" name="description" />
      <meta content="Document keywords" name="keywords" />
      <style type="text/css"><!-- @import url(example.css); --></style>
      <link href="example.kss" rel="kukit" type="text/kss" />
      <script src="example.js" type="text/javascript">
      </script>
    </head>
    <body>
      <div id="content">Document content</div>
      <img src="example.png" />
    </body>
  </html>

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())
  <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Example layout</title>
      <!-- meta -->
      <meta content="Emacs" name="generator" />
      <meta content="This is an example layout." name="description" />
      <meta content="example, layout, xss." name="keywords" />
      <style type="text/css"><!-- @import url(http://host/example.css); --></style>
      <link href="http://host/example.kss" rel="kukit" type="text/kss" />
      <script src="http://host/example.js" type="text/javascript">      
      </script>
    </head>
    <body>
      <div id="content">
        This is the content area.
      </div>
      <img src="http://host/example.png" />
    </body>
  </html>

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"})
  <html xmlns="http://www.w3.org/1999/xhtml">
    ...
    <title>Title from macro</title>
    ...
  </html>
  
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())
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <link href="http://host/one.css" rel="stylesheet" type="text/css" />
        <link href="http://host/two.css" rel="stylesheet" type="text/css" />
        <script src="http://host/example.js" type="text/javascript">
        </script>
        <script src="http://host/foo.js" type="text/javascript">
        </script>
      </head>
      <body>
        <div id="content">
          This is the content area.
        </div>
      </body>
    </html>

