Pyjoyment.Renderer - Generate dynamic content

from Pyjoyment.Renderer import Renderer

renderer = Renderer()
renderer.classes.append('MyApp.Controller.Foo')
renderer.paths.append('/home/sri/templates')

# Or import the module, as in Google Python Style Guide

import Pyjoyment.Renderer

renderer = Pyjoyment.Renderer.new()
renderer.classes.append('MyApp.Controller.Foo')
renderer.paths.append('/home/sri/templates')

Pyjoyment.Renderer is the standard Pyjoyment renderer.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Renderer of Mojolicious 9.49.

class Pyjoyment.Renderer.Pyjoyment_Renderer[source]

Pyjoyment.Renderer inherits all methods from Pyjo.Base and implements the following new ones.

accepts(c: Pyjoyment.Controller.object, *formats: str) → list[str] | str | None[source]
all = renderer.accepts(Pyjoyment.Controller.new())
best = renderer.accepts(Pyjoyment.Controller.new(), 'html', 'json')

Select best possible representation for Pyjoyment.Controller object from format GET/POST parameter, format stash value, or Accept request header, defaults to returning the first extension if no preference could be detected.

add_handler(name: str, cb: Handler) → Self[source]
renderer = renderer.add_handler('epl', lambda renderer, c, output, options: ...)

Register a handler.

def foo(renderer, c, output, options):
    ...
    output.write('Hello World!')

renderer.add_handler('foo', foo)
add_helper(name: str, cb: Callable[[...], Any]) → Self[source]
renderer = renderer.add_helper('url_for', lambda c, *args: ...)

Register a helper.

def foo(c, *args):
    ...

renderer.add_helper('foo', foo)
add_template(name: str, cb: None = None) → Callable[[F], F][source]
add_template(name: str, cb: Callable[[...], Any]) → Self
renderer = renderer.add_template('index.html', lambda c: t('<p>Hello!</p>'))

@renderer.add_template('index.html')
def index(c): ...

Register a template of a function, which gets the controller and returns a template string, see Pyjoyment.Plugin.PyRenderer.

def foo(c, name):
    return t('<p>Hello {name}!</p>')

renderer.add_template('foo.html', foo)
cache: Pyjo_Cache[Any, Any]
cache = renderer.cache
renderer.cache = Pyjo.Cache.new()

Renderer cache, defaults to a Pyjo.Cache object.

classes
classes = renderer.classes
renderer.classes = ['__main__']

Classes to use for finding templates in DATA sections with Pyjo.Loader, first one has the highest precedence, defaults to __main__. Only files with exactly two extensions will be used, like index.html.ep. Note that for templates to be detected, these classes need to have already been loaded and added before warmup() is called, which usually happens automatically during application startup.

# Add another class with templates in DATA section
renderer.classes.append('Pyjoyment.Plugin.Fun')

# Add another class with templates in DATA section and higher precedence
renderer.classes.insert(0, 'Pyjoyment.Plugin.MoreFun')
compress
boolean = renderer.compress
renderer.compress = boolean

Try to negotiate compression for dynamically generated response content and gzip compress it automatically, defaults to true.

default_format
default = renderer.default_format
renderer.default_format = 'html'

The default format to render if format is not set in the stash, defaults to html. Note that changing the default away from html is not recommended, as it has the potential to break, for example, plugins with bundled templates.

default_handler: str | None
default = renderer.default_handler
renderer.default_handler = 'ep'

The default template handler to use for rendering in cases where auto-detection doesn’t work, like for inline templates. An inline template that is a template string of Pyjo.String.Template is rendered by the handler py of Pyjoyment.Plugin.PyRenderer.

encoding: str | None
encoding = renderer.encoding
renderer.encoding = 'koi8-r'

Will encode generated content if set, defaults to UTF-8. Note that many renderers also use this value to determine if template files should be decoded before processing.

get_data_template(options: dict[str, Any]) → str | bytes | None[source]
template = renderer.get_data_template({'template': 'foo/bar', 'format': 'html', 'handler': 'epl'})

Return a DATA section template from classes for an options dict with template, format, variant and handler values, or None if no template could be found, usually used by handlers.

get_helper(name: str) → Callable[[...], Any] | type[Pyjoyment_Renderer_Helpers] | None[source]
helper = renderer.get_helper('url_for')

Get a helper by full name, generate a helper dynamically for a prefix, or return None if no helper or prefix could be found. Generated helpers return a proxy object containing the current controller object and on which nested helpers can be called.

handlers: dict[str, Handler]
handlers = renderer.handlers
renderer.handlers = {'epl': lambda renderer, c, output, options: ...}

Registered handlers.

helpers: dict[str, Callable[[...], Any]]
helpers = renderer.helpers
renderer.helpers = {'url_for': lambda c, *args: ...}

Registered helpers.

min_compress_size
size = renderer.min_compress_size
renderer.min_compress_size = 1024

Minimum output size in bytes required for compression to be used if enabled, defaults to 860.

paths: list[str]
paths = renderer.paths
renderer.paths = ['/home/sri/templates']

Directories to look for templates in, first one has the highest precedence.

# Add another "templates" directory
renderer.paths.append('/home/sri/templates')

# Add another "templates" directory with higher precedence
renderer.paths.insert(0, '/home/sri/themes/blue/templates')
render(c: Pyjoyment.Controller.object) → tuple[str | bytes, str | None] | None[source]
output, format = renderer.render(Pyjoyment.Controller.new())

Render output through one of the renderers. See Pyjoyment.Controller.Pyjoyment_Controller.render() for a more user-friendly interface.

respond(c: Pyjoyment.Controller.object, output: str | bytes, format: str | None, status: int | str | None = None) → bool[source]
boolean = renderer.respond(Pyjoyment.Controller.new(), output, format)
boolean = renderer.respond(Pyjoyment.Controller.new(), output, format, status)

Finalize dynamically generated response content and compress it if possible.

template_for(c: Pyjoyment.Controller.object) → str | None[source]
name = renderer.template_for(Pyjoyment.Controller.new())

Return default template name for Pyjoyment.Controller object, or None if no name could be generated.

template_handler(options: dict[str, Any]) → str | None[source]
handler = renderer.template_handler({'template': 'foo/bar', 'format': 'html'})

Return handler for an options dict with template, format and variant values, or None if no handler could be found.

template_name(options: dict[str, Any]) → str | None[source]
template = renderer.template_name({'template': 'foo/bar', 'format': 'html', 'handler': 'epl'})

Return a template name for an options dict with template, format, variant and handler values, or None if no template could be found, usually used by handlers.

template_path(options: dict[str, Any]) → str | None[source]
path = renderer.template_path({'template': 'foo/bar', 'format': 'html', 'handler': 'epl'})

Return the full template path for an options dict with template, format, variant and handler values, or None if the file does not exist in paths, usually used by handlers.

templates: dict[str, Callable[[...], Any]]
templates = renderer.templates
renderer.templates = {'index.html': lambda c: t('<p>Hello!</p>')}

Registered templates of functions, by the name of the template with its format, as index.html, which the handler py of Pyjoyment.Plugin.PyRenderer renders, before the templates of paths and classes.

warmup() → None[source]
renderer.warmup()

Prepare templates from classes and templates for future use.

class Pyjoyment.Renderer.Pyjoyment_Renderer_Helpers(c: Any)[source]

The proxy object of Pyjoyment_Renderer.get_helper(), which contains the current controller object and on which nested helpers can be called.