restish.templating¶
Templating support.
- class restish.templating.Templating(renderer)¶
Bases: object
- args(request)¶
- Return a dict of args that should always be present.
- element_args(request, element)¶
- Return a dict of args that should be present when rendering elements.
- page_args(request, page)¶
- Return a dict of args that should be present when rendering pages.
- render(request, template, args=None, encoding=None)¶
- Render the template and args, optionally encoding to a byte string.
- restish.templating.element(template)¶
Convenience decorator that calls render_element, passing the dict returned from calling the decorated method as the template ‘args’.
The decorated method’s first argument must be a http.Request instance. All arguments (including the request) are passed on as-is.
The decorated method must return a dict that will be passed to the render_element methods as the args parameter.
Parameters: - template – Name of the template file.
- restish.templating.page(template, type='text/html', encoding='utf-8')¶
Convenience decorator that calls render_response, passing the dict returned from calling the decorated method as the template ‘args’.
The decorated method’s first argument must be a http.Request instance. All arguments (including the request) are passed on as-is.
The decorated method must return a dict that will be passed to the render_response method as the args parameter.
Note: if the decorator does not allow full control consider calling render_response directly.
Parameters: - template – Name of the template file.
- type – Optional mime type of content, defaults to ‘text/html’
- encoding – Optional encoding of output, default to ‘utf-8’.
- restish.templating.render(request, template, args={}, encoding=None)¶
Render the template and args using the configured templating engine.
Parameters: - request – Request instance.
- template – Name of the template file.
- args – Dictionary of args to pass to the template renderer.
- encoding – Optional output encoding, defaults to None, i.e. output will be unicode (or unicode-safe).
- restish.templating.render_element(request, element, template, args={})¶
Render a page element using the template and args.
Parameters: - request – Request instance.
- element – Element being rendered (hint, it’s often self).
- template – Name of the template file.
- args – Dictionary of args to pass to the template renderer.
- restish.templating.render_page(request, page, template, args={}, encoding='utf-8')¶
Render a page using the template and args.
Parameters: - request – Request instance.
- page – Page being rendered (hint, it’s often self).
- template – Name of the template file.
- args – Dictionary of args to pass to the template renderer.
- encoding – Optional encoding of output, default to ‘utf-8’.
- restish.templating.render_response(request, page, template, args={}, type='text/html', encoding='utf-8')¶
Render a page, using the template and args, and return a ‘200 OK’ response. The response’s Content-Type header will be constructed from the type and encoding.
Parameters: - request – Request instance.
- page – Page being rendered (hint, it’s often self).
- template – Name of the template file.
- args – Dictionary of args to pass to the template renderer.
- type – Optional mime type of content, defaults to ‘text/html’
- encoding – Optional encoding of output, default to ‘utf-8’.