Merge pull request #1224 from pallets/loader-docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
update package and filesystem loader docs
authored by David Lord · committed by GitHub · parents 2ad1677, 2408a55 · Browse files · 2 files·+42 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−27 removed
- other:+42 added−27 removed
Provenance
- Commit
ca8b0b0- Tree
78f4115- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2ad1677- Parent 2
2408a55
2 files changed+42 added−27 removed
modified:docs/api.rst+25 added-15 removed
23 unmodified lines24 initialization and use that to load templates. In some cases however, it's25 useful to have multiple environments side by side, if different configurations26 are in use.27 28-The simplest way to configure Jinja to load templates for your application29-looks roughly like this::28+The simplest way to configure Jinja to load templates for your29+application is to use :class:`~loaders.PackageLoader`.30 31+.. code-block:: python32+33 from jinja2 import Environment, PackageLoader, select_autoescape34 env = Environment(33- loader=PackageLoader('yourapplication', 'templates'),34- autoescape=select_autoescape(['html', 'xml'])35+ loader=PackageLoader("yourapp"),36+ autoescape=select_autoescape()37 )38 37-This will create a template environment with the default settings and a38-loader that looks up the templates in the `templates` folder inside the39-`yourapplication` python package. Different loaders are available40-and you can also write your own if you want to load templates from a41-database or other resources. This also enables autoescaping for HTML and42-XML files.39+This will create a template environment with a loader that looks up40+templates in the ``templates`` folder inside the ``yourapp`` Python41+package (or next to the ``yourapp.py`` Python module). It also enables42+autoescaping for HTML files. This loader only requires that ``yourapp``43+is importable, it figures out the absolute path to the folder for you.44 44-To load a template from this environment you just have to call the45-:meth:`get_template` method which then returns the loaded :class:`Template`::45+Different loaders are available to load templates in other ways or from46+other locations. They're listed in the `Loaders`_ section below. You can47+also write your own if you want to load templates from a source that's48+more specialized to your project.49 47- template = env.get_template('mytemplate.html')50+To load a template from this environment, call the :meth:`get_template`51+method, which returns the loaded :class:`Template`.52 49-To render it with some variables, just call the :meth:`render` method::53+.. code-block:: python54 51- print(template.render(the='variables', go='here'))55+ template = env.get_template("mytemplate.html")56 57+To render it with some variables, call the :meth:`render` method.58+59+.. code-block:: python60+61+ print(template.render(the="variables", go="here"))62+63 Using a template loader rather than passing strings to :class:`Template`64 or :meth:`Environment.from_string` has multiple advantages. Besides being65 a lot easier to use it also enables template inheritance.66 815 unmodified linesmodified:src/jinja2/loaders.py+17 added-12 removed
135 unmodified lines136 )137 138 139 class FileSystemLoader(BaseLoader):140- """Loads templates from the file system. This loader can find templates141- in folders on the file system and is the preferred way to load them.140+ """Load templates from a directory in the file system.141 143- The loader takes the path to the templates as string, or if multiple144- locations are wanted a list of them which is then looked up in the145- given order::142+ The path can be relative or absolute. Relative paths are relative to143+ the current working directory.144 147- >>> loader = FileSystemLoader('/path/to/templates')148- >>> loader = FileSystemLoader(['/path/to/templates', '/other/path'])145+ .. code-block:: python146 150- Per default the template encoding is ``'utf-8'`` which can be changed151- by setting the `encoding` parameter to something else.147+ loader = FileSystemLoader("templates")148 153- To follow symbolic links, set the *followlinks* parameter to ``True``::149+ A list of paths can be given. The directories will be searched in150+ order, stopping at the first matching template.151 155- >>> loader = FileSystemLoader('/path/to/templates', followlinks=True)152+ .. code-block:: python153 154+ loader = FileSystemLoader(["/override/templates", "/default/templates"])155+156+ :param searchpath: A path, or list of paths, to the directory that157+ contains the templates.158+ :param encoding: Use this encoding to read the text from template159+ files.160+ :param followlinks: Follow symbolic links in the path.161+162 .. versionchanged:: 2.8158- The ``followlinks`` parameter was added.163+ Added the ``followlinks`` parameter.164 """165 166 def __init__(self, searchpath, encoding="utf-8", followlinks=False):167 if not isinstance(searchpath, abc.Iterable) or isinstance(searchpath, str):399 unmodified lines