plastic.rendering — Content rendering

This module provides generic content rendering to make response using requested Accept mimetypes. In HTTP terminology, it’s called as content negotiation.

Plastic provides two types of rendering methods:

Template engines
It’s for making responses that contains HTML. A typical case of use this is showing web pages to clients using their web browser.
Serializers
It’s for making responses that have structural data like JSON. A typical case of use this is providing RESTful API to clients.
plastic.rendering.render(request, value, path, values={}, **keywords)

Renders the suitable response using content negotiation. It’s aware of given request‘s Accept header.

If there’s no matched mimetype to request‘s Accept list, it raises NotAcceptable error.

If chosen mimetype is associated to serializer (see BaseApp.add_serializer()) it passes value to the serializer and rest of arguments are just ignored.

If chosen mimetype is associated to suffix (see BaseApp.associate_mimetypes() method) it appends the suffix to the given template path and then calls render_template() function with the same arguments except value.

Parameters:
  • request (Request) – a request which make it to render
  • value – a value to serialize
  • path (basestring) – a path to template files without suffix to determine content type and template engine
  • values (collections.Mapping) – a dictionary of values to pass to template
  • **keywords – the same to values except these are passed by keywords
Returns:

a rendered response

Return type:

Response

Raises:
plastic.rendering.render_template(request, path, values={}, **keywords)

Helper function that renders a template of the given path with values (and keywords). It’s a shortcut of BaseApp.render_template() method.

@App.route('/<int:user_id>')
def user_profile(request, user_id):
    global users
    user = users[user_id]
    return render_template(request, 'user_profile.html',
                           user_id=user_id, user=user)

You have to pass the path without specific suffix. The last suffix will be used for determining what template engine to use. For example, if there is user_profile.html.mako file in the template_path and 'mako' is associated to Mako template engine in template_engines mapping, the following code will find user_profile.html.mako (not user_profile.html) and render it using Mako:

render_template(request, 'user_profile.html')

In other words, you have to append a suffix to determine what template engine to use into filenames of templates.

Parameters:
  • request (Request) – a request which make it to render
  • path (basestring) – a path to template files without suffix to determine template engine
  • values (collections.Mapping) – a dictionary of values to pass to template
  • **keywords – the same to values except these are passed by keywords
Returns:

a rendered result

Return type:

basestring

Raises plastic.exceptions.RenderError:
 

when there are no matched template files

See also

Method BaseApp.render_template()
Renders the response using registered template_engines.

Project Versions

Previous topic

plastic.context — Contexts

Next topic

plastic.config — Configuration mapping

This Page