Renderers

Custom Templates

Most people end up needing to design templates different from the ones built in at some point. Because of this Yota is setup for specifying a search path for custom templates. By default Yota will only look at its own template directory. It is typical to add a search path that points somewhere within your project. Yota will take the first template it finds that matches, so a simple way to ensure your custom templates are prioritized is to insert your path first in the list. A typical example might look something like this:

import os
from yota.renderers import JinjaRenderer

JinjaRenderer.search_path.insert(0, os.path.dirname(os.path.realpath(__file__)) +
                            "/assets/yota/templates/")

Switching Template Sets

Yota provides potential for multiple default template sets. The default template set is designed for use with Bootstrap 2.3.2, but a Bootstrap 3.0 implementation can used by modifying the JinjaRenderer attribute templ_type to ‘bs3’. More can be read at renderers.JinjaRenderer.templ_type.

Rendering Engines

By default Jinja2 is the renderer for Yota, however support for other renderes is possible by setting the Form._renderer to a different class that implements the proper interface. Currently the default and only option is renderers.JinjaRenderer, however other implementations should be easy to write. The default Nodes Node.template property lacks a file extension and expects the renderer to auto-append this before calling the template, thus allowing the Node to work accross different renderers.

Renderers are invoked when a render method of a Form is executed. currently these include Form.render() and Form.validate_render(). renderers were designed mainly to allow the interchange of template engines and context gathering semantics.

Renderer Interface

As of now only one method must be implemented by a Renderer: the render method. It accepts two parameters, a list of Nodes to be rendererd in order and a dictionary that contains the global context to include in every template context. Looking at the source for JinjaRenderer will provide some guidence on how you might write your own Renderer.

Switching Renderers

A standard pattern would be to set the Form class object Form._renderer attribute allowing the attribute change to be effectively global. This would normally be done in whatever setup function your web framework provides.

JinjaRenderer API

class yota.renderers.JinjaRenderer[source]
env[source]

Simple lazy loader for the Jinja2 enviroment

render(nodes, g_context)[source]

Loop over each Node passed in by nodes and render it into a big blob of a string. Passes g_context to each nodes Node.get_context().

search_path = []

The list of paths that Jinja will look for templates in. It scans sequentially, so inserting custom template paths at the beginning is an easy way to override default templates without touching Yota. The default path is appended to the end of this list the first time render is called.

suffix = '.html'

The default template suffix

templ_type = 'bs2'

Allows you to switch the default template set being used. Default templates for JinjaRenderer are stored in /templates/jinja/{templ_type}/. Below code being run before your render method will change templates to Bootstrap 3.0. This is usually run when setting up web framework configs to take effect globally. See the flask example for more information.

import os
from yota.renderers import JinjaRenderer

JinjaRenderer.templ_type = "bs3"

Note

In order to display errors correctly the Form.type_class_map must be overriden so alert-error can be changed to alert-danger for Bootstrap 3.

Project Versions

Table Of Contents

Previous topic

Validators and Checks

Next topic

AJAX Validation

This Page