aspen.simplates

class aspen.simplates.simplate.Simplate(request_processor, fspath, raw, fs_media_type)

A simplate is a dynamic resource with multiple syntaxes in one file.

Parameters:
  • fspath (str) – the absolute filesystem path of this simplate
  • raw (bytes) – raw content of this simplate as bytes
  • fs_media_type (str) – the content type derived from the extension in the simplate’s filesystem name, if it has one
render_for_type(media_type, context)

Render the simplate.

Parameters:
  • media_type (str) – the media type of the page to render
  • context (dict) – execution context values you wish to supply

Returns: an Output object.

parse_into_pages(decoded)

Given a bytestring that is the entire simplate, return a list of pages.

If there’s one page, it’s a template.

If there’s more than one page, the first page is always python and the last is always a template.

If there’s more than two pages, the second page is python unless it has a specline, which makes it a template.

compile_pages(pages)

Given a list of pages, replace the pages with objects.

Page 0 is the ‘run once’ page - it is executed and the resulting context stored in self.pages[0].

Page 1 is the ‘run every’ page - it is compiled for easier execution later, and stored in self.pages[1].

Subsequent pages are templates, so each one’s content type and respective renderer are stored as a tuple in self.pages[n].

compile_page(page)

Given a Page, return a (renderer, media_type) pair.

aspen.simplates.renderers.factories(configuration)

return a dict of render factory names to the factories themselves

class aspen.simplates.renderers.Renderer(factory, filepath, raw, media_type, offset)

The base class of renderers.

compile(filepath, padded)

Override.

Whatever you return from this will be set on self.compiled the first time the renderer is called. If changes_reload is True then this will be called every time the renderer is called. You can then use self.compiled in your render_content method as needed.

render_content(context)

Override. Context is a dict.

You can use these attributes:

self.raw        the raw bytes of the content page
self.compiled   the result of self.compile (generally a template in
                 compiled object form)
self.meta       the result of Factory.compile_meta
self.media_type the media type of the page
self.offset     the line number at which the page starts
class aspen.simplates.renderers.Factory(configuration)

The base class of renderer factories.

class Renderer(factory, filepath, raw, media_type, offset)

The base class of renderers.

compile(filepath, padded)

Override.

Whatever you return from this will be set on self.compiled the first time the renderer is called. If changes_reload is True then this will be called every time the renderer is called. You can then use self.compiled in your render_content method as needed.

render_content(context)

Override. Context is a dict.

You can use these attributes:

self.raw        the raw bytes of the content page
self.compiled   the result of self.compile (generally a template in
                 compiled object form)
self.meta       the result of Factory.compile_meta
self.media_type the media type of the page
self.offset     the line number at which the page starts
compile_meta(configuration)

Takes a configuration object. Override as needed.

Whatever you return from this will be set on self.meta the first time the factory is called, or every time if changes_reload is True. You can then use self.meta in your Renderer class as needed.