class Loader [source]
Loads templates from a given source, such as the filesystem or a database.
-
get_template_sources(template_name)[source] -
A method that takes a
template_nameand yieldsOrigininstances for each possible source.For example, the filesystem loader may receive
'index.html'as atemplate_nameargument. This method would yield origins for the full path ofindex.htmlas it appears in each template directory the loader looks at.The method doesn’t need to verify that the template exists at a given path, but it should ensure the path is valid. For instance, the filesystem loader makes sure the path lies under a valid template directory.
-
get_contents(origin) -
Returns the contents for a template given a
Origininstance.This is where a filesystem loader would read contents from the filesystem, or a database loader would read from the database. If a matching template doesn’t exist, this should raise a
TemplateDoesNotExisterror.
-
get_template(template_name, skip=None)[source] -
Returns a
Templateobject for a giventemplate_nameby looping through results fromget_template_sources()and callingget_contents(). This returns the first matching template. If no template is found,TemplateDoesNotExistis raised.The optional
skipargument is a list of origins to ignore when extending templates. This allow templates to extend other templates of the same name. It also used to avoid recursion errors.In general, it is enough to define
get_template_sources()andget_contents()for custom template loaders.get_template()will usually not need to be overridden.
-
load_template_source(template_name, template_dirs=None)[source] -
Returns a tuple of (
template_string,template_origin), wheretemplate_stringis a string containing the template contents, andtemplate_originis a string identifying the template source. A filesystem-based loader may return the full path to the file as thetemplate_origin, for example.template_dirsis an optional argument used to control which directories the loader will search.This method is called automatically by
load_template()and should be overridden when writing custom template loaders.Deprecated since version 1.9: Custom loaders should use
get_template()andget_contents()instead.
-
load_template(template_name, template_dirs=None)[source] -
Returns a tuple of (
template,template_origin), wheretemplateis aTemplateobject andtemplate_originis a string identifying the template source. A filesystem-based loader may return the full path to the file as thetemplate_origin, for example.Deprecated since version 1.9: Custom loaders should use
get_template()andget_contents()instead.
Building your own
For examples, read the source code for Django’s built-in loaders.
Please login to continue.