pysite.sitemgr.page

class pysite.sitemgr.page.Page(context, request)

Bases: builtins.object

Loads and renders a page.

This class facades creating a Jinja environment, loading the appropriate template for a given request and finally rendering the page.

On creation, a page instance is initialised with a context and a request object from the current request. It also initialises a sandboxed environment for Jinja. A caller may override the default Jinja environment by setting attribute jjenv.

The simplest task is to just render the page for the current request, e.g. inside a Pyramid view:

@view_config(...)
def view(context, request):
    page = Page(context, request)
    return Response(page.get_page())

The rendering environment by default is a sandbox, and the following globals are available in a template:

  • site: Contains the settings of the site. Usage: {{ site.title }}

  • page: Contains the settings of the current page. Usage: {{ page.keywords }}

  • url(): See url()

  • asset_url(): See asset_url()

  • ‘load_config(): See load_config()

  • slugparts: List of path items, e.g. if current page is foo/bar/baz

    it contains ["foo", "bar", "baz"].

It also sets these options:

  • autoescape = True
  • auto_reload = True
asset_url(path, query=None, anchor=None)

Returns URL to a static asset, e.g. CSS file.

Assets are stored in the site’s assets directory, which is published via a static route in Pyramid.

Usage:

<img src="{{ asset_url("img/grass-mud-horse2.jpg") }}">
Parameters:
  • path – Path to a static asset, relative to the assets directory.
  • query – Optional data to build a query string. Internally is passed to urllib.parse.urlencode.
  • anchor – Optional an anchor name.
Returns:

Absolute URL to static asset

Todo

Add a signal, so that plugins can hook into the creation of the static URL and trigger e.g. cimpilation of LESSCSS or sth. like this.

If hook returns its generated URL, no more hooks shall be signaled. If hook returns None, the next hook is signaled. If all hooks returned None, the default generation takes place.

blog(path, **kw)
filter_angular(s)
get_page(fn, jjglobals=None, jjcontext=None)

Returns the rendered page for current request.

This function is for convenience, as it just encapsulates calls to load() and render().

Parameters:
  • jjglobals – A dict with additional globals; passed to load().
  • jjcontext – A dict with additional settings for the rendering context; passed to render().
Returns:

Rendered page as string.

load(fn, jjglobals=None)

Loads a template.

Populates attribute jjtpl with an instance of the Jinja template.

Parameters:
  • fn – Filename of the template. If omitted, it is built according to the current request’s context.
  • jjglobals – A dict with additional globals to pass to the template. If set, attribute jjglobals is updated by these.
load_config(fn, encoding='utf-8', sortkey=None, reverse=False)

Loads a configuration file.

YAML, JSON and INI format are supported.

Usage:

{% set data = load_config("test.yaml") %}
{% for k, v in data.items() %}
    <div>Found key "{{k}}" with value "{{v}}".</div>
{% endfor %}
Parameters:
  • fn – Name of configuration file. Path may be relative or absolute within the site.
  • encoding – Optional. Character set encoding of the configuration data. Defaults to ‘utf-8’.
Returns:

Loaded data structure, mostly list or dict.

render(jjcontext=None)

Renders current template.

Stores rendered page as string in attribute page.

Parameters:jjcontext – Dict with additional settings for the render context. If set, attribute jjcontext is updated by these.
render_from_string(s, jjglobals=None, jjcontext=None)
url(apath, **kw)

Returns absolute URL to another page.

Determines path‘s target resource and then calls current request’s method resource_url() to create the URL. All optional keyword arguments are passed to resource_url().

Usage:

<p>Go to <a href="{{ url("dir_1/dir_2/other_page") }}">
other page</a></p>
Parameters:path – Relative path to another page, which is stored in site’s content directory.
Returns:Absolute URL to another page
context = None

Context of current request

jjcontext = None

Dict used for Jinja rendering context

jjenv = None

Instance of a Jinja environment

jjglobals = None

Dict of variables that will globally be available in a template

jjtpl = None

Instance of a loaded Jinja template

page = None

Rendered page as string

request = None

Current request

class pysite.sitemgr.page.Sandbox(*args, **kwargs)

Bases: jinja2.sandbox.SandboxedEnvironment

Previous topic

pysite.sitemgr

Next topic

pysite.sitemgr.models

This Page