Package turbogears :: Package view :: Module base

Module base

source code

Template processing for TurboGears view layer.

The template engines are configured and loaded here and this module provides the generic template rendering function "render", which selects the template engine to use and the appropriate output format, headers and encoding based on the given arguments and the application configuration.

Also defines the functions and variables that will be available in the template scope and provides a hook for adding additional template variables.

Classes
  cycle
Loops forever over an iterator.
  UserAgent
Representation of the user's browser.
Functions
 
render(info, template=None, format=None, headers=None, fragment=False, **options)
Renders data in the desired format.
source code
 
transform(info, template)
Create ElementTree representation of the output.
source code
 
loadBaseTemplates()
Load base templates for use by other templates.
source code
 
selector(expression)
If the expression is true, return the string 'selected'.
source code
 
checker(expression)
If the expression is true, return the string "checked".
source code
 
ipeek(iterable)
Lets you look at the first item in an iterator.
source code
 
genshi_et(element)
If this is an ElementTree element, convert it to a Genshi Markup stream.
source code
 
kid_xml(stream)
If this is a Genshi Markup stream, convert it to a Kid ElementStream.
source code
 
stdvars()
Create a Bunch of variables that should be available in all templates.
source code
 
load_engines()
Load and initialize all templating engines.
source code
Variables
  kid = None
hash(x)
  kid_i18n_filter = None
hash(x)
  genshi_i18n_filter = Translator(gettext)
  log = logging.getLogger('turbogears.view')
  baseTemplates = []
  variable_providers = []
  root_variable_providers = []
  engines = {}
  __package__ = 'turbogears.view'
Function Details

render(info, template=None, format=None, headers=None, fragment=False, **options)

source code 

Renders data in the desired format.

Parameters:
  • info (dict) - the data itself
  • template (string) - name of the template to use
  • format (string) - 'html', 'xml', 'text' or 'json'
  • headers (dict) - for response headers, primarily the content type
  • fragment (bool

    All additional keyword arguments are passed as keyword args to the render method of the template engine.

    ) - passed through to tell the template if only a fragment of a page is desired. This is a way to allow xml template engines to generate non valid html/xml because you warn them to not bother about it.

loadBaseTemplates()

source code 

Load base templates for use by other templates.

By listing templates in turbogears.view.baseTemplates, these templates will automatically be loaded so that the "import" statement in a template will work.

selector(expression)

source code 

If the expression is true, return the string 'selected'.

Useful for HTML <option>s.

checker(expression)

source code 

If the expression is true, return the string "checked".

This is useful for checkbox inputs.

ipeek(iterable)

source code 

Lets you look at the first item in an iterator.

This is a good way to verify that the iterator actually contains something. This is useful for cases where you will choose not to display a list or table if there is no data present.

genshi_et(element)

source code 

If this is an ElementTree element, convert it to a Genshi Markup stream.

If this is a list, apply this function recursively and chain everything.

stdvars()

source code 
Create a Bunch of variables that should be available in all templates.

These variables are:

checker
    the checker function
config
    the cherrypy config get function
cycle
    cycle through a set of values
errors
    validation errors
identity
    the current visitor's identity information
inputs
    input values from a form
ipeek
    the ipeek function
locale
    the default locale
quote_plus
    the urllib quote_plus function
request
    the cherrypy request
selector
    the selector function
session
    the current cherrypy.session if tools.sessions.on is set in the
    app.cfg configuration file, otherwise session will be None
tg_js
    the url path to the JavaScript libraries
tg_static
    the url path to the TurboGears static files
tg_toolbox
    the url path to the TurboGears toolbox files
tg_version
    the version number of the running TurboGears instance
url
    the turbogears.url function for creating flexible URLs
useragent
    a UserAgent object with information about the browser

Additionally, you can add a callable to turbogears.view.variable_providers
that can add more variables to this list. The callable will be called with
the vars Bunch after these standard variables have been set up.

load_engines()

source code 

Load and initialize all templating engines.

This is called during startup after the configuration has been loaded. You can call this earlier if you need the engines before startup; the engines will then be reloaded with the custom configuration later.