1.21. templates

This module provides base classes for the Jinja2 environments used throughout the application.

1.21.1. Classes

class FindFileSystemLoader[source]

Bases: jinja2.loaders.BaseLoader

A BaseLoader which loads templates by name from the file system. Templates are searched for using the data_file() function.

get_source(environment, template)[source]

Get the template source, filename and reload helper for a template. It’s passed the environment and template name and has to return a tuple in the form (source, filename, uptodate) or raise a TemplateNotFound error if it can’t locate the template.

The source part of the returned tuple must be the source of the template as unicode string or a ASCII bytestring. The filename should be the name of the file on the filesystem if it was loaded from there, otherwise None. The filename is used by python for the tracebacks if no loader extension is used.

The last item in the tuple is the uptodate function. If auto reloading is enabled it’s always called to check if the template changed. No arguments are passed so the function must store the old state somewhere (for example in a closure). If it returns False the template will be reloaded.

class TemplateEnvironmentBase(loader=None, global_vars=None)[source]

Bases: jinja2.environment.Environment

A configured Jinja2 Environment with additional filters and default settings.

__init__(loader=None, global_vars=None)[source]
Parameters:
  • loader (jinja2.BaseLoader) – The loader to supply to the environment.
  • global_vars (dict) – Additional global variables for the environment.
from_file(path, **kwargs)[source]

A convenience method to load template data from a specified file, passing it to from_string().

Warning

Because this method ultimately passes the template data to the from_string() method, the data will not be automatically escaped based on the file extension as it would be when using get_template().

Parameters:
  • path (str) – The path from which to load the template data.
  • kwargs – Additional keyword arguments to pass to from_string().
join_path(template, parent)[source]

Over ride the default jinja2.Environment.join_path() method to explicitly specifying relative paths by prefixing the path with either “./” or “../”.

Parameters:
  • template (str) – The path of the requested template file.
  • parent (str) – The path of the template file which requested the load.
Returns:

The new path to the template.

Return type:

str

standard_variables[source]

Additional standard variables that can optionally be used in templates.

class MessageTemplateEnvironment(*args, **kwargs)[source]

Bases: king_phisher.templates.TemplateEnvironmentBase

A configured Jinja2 environment for formatting messages.

MODE_ANALYZE = 1[source]
MODE_PREVIEW = 0[source]
MODE_SEND = 2[source]
attachment_images = None[source]

A dictionary collecting the images that are going to be embedded and sent inline in the message.

set_mode(mode)[source]

Set the operation mode for the environment. Valid values are the MODE_* constants.

Parameters:mode (int) – The operation mode.