Pyjoyment.Plugin.PyRenderer - Template string renderer plugin

# Pyjoyment
app.plugin('PyRenderer')

# A template of a function
@app.template('index.html')
def index(c, name):
    return t'<p>Hello {name}!</p>'

c.render('index', name='Joe')

# An inline t-string of Python 3.14
c.render(inline=t'<p>Hello {name}!</p>')

# The same on any Python
c.render(inline=t('<p>Hello {name}!</p>', name=name))

Pyjoyment.Plugin.PyRenderer is a renderer for py templates, which are template strings, t-strings of PEP 750 or of Pyjo.String.Template.t(), rendered by Pyjo.String.Template, which escapes the values that they interpolate, unless they are markup, as the helpers of Pyjoyment.Plugin.TagHelpers return.

from Pyjo.String.Template import t

# "<p>Hello &lt;script&gt;!</p>"
name = '<script>'
c.render(inline=t('<p>Hello {name}!</p>', name=name))

# "<p><a href="/">Home</a></p>"
c.render(inline=t('<p>{link}</p>', link=c.link_to('Home', '/')))

A template string is code, which cannot be read from a file of data as a template of Pyjoyment.Plugin.EPRenderer is, so a template is a function that returns one, registered with Pyjoyment.Pyjoyment_App.template(), or the function template of Pyjoyment.Lite, by its name with the format, as index.html. The function gets the controller and the values of the stash that it names as its parameters, as the variables of an ep template, or all of them with **, and it can set the values of the stash that the renderer reads, as a layout.

# A template with a layout
@app.template('hello.html')
def hello(c, name):
    c.layout('default')
    c.title('Hello')
    return t'<p>Hello {name}!</p>'

# The layout, which gets the content and the title
@app.template('layouts/default.html')
def default(c, **stash):
    return t'<title>{c.title()}</title><body>{c.content()}</body>'

A template can also be a file in Pyjoyment.Renderer.Pyjoyment_Renderer.paths with the extension py, as templates/index.html.py, a module of Python whose function render is the function of the template. The file is compiled once and kept in the cache of the renderer, without the bytecode that an import writes next to it, and an exception in it shows the lines of the file.

# templates/hello.html.py
def render(c, name):
    return t'<p>Hello {name}!</p>'

An inline template that is a template string is rendered by it, unless another handler is given. Its values are evaluated where it is written, so it gets the values of the stash as the code around it does, c.stash('name'), and a layout does not apply to it, as to any inline template.

This is a core plugin, that means it is always enabled and its code a good example for learning to build new plugins, you’re welcome to fork it.

See Pyjoyment.Plugins for a list of plugins that are available by default.

See also

Pyjoyment, https://mojolicious.org.

Inspired by Mojolicious::Plugin::EPLRenderer of Mojolicious 9.49.

class Pyjoyment.Plugin.PyRenderer.Pyjoyment_Plugin_PyRenderer[source]

Pyjoyment.Plugin.PyRenderer inherits all methods from Pyjoyment.Plugin and implements the following new ones.

register(app: Pyjoyment_App, conf: dict[str, Any] | None = None) → None[source]
plugin.register(Pyjoyment.new())

Register renderer in Pyjoyment application.