Skip to content
Myr

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

2 files+42 added−27 removed

modified:docs/api.rst+25 added-15 removed
23 unmodified lines
24initialization and use that to load templates. In some cases however, it's25useful to have multiple environments side by side, if different configurations26are in use.2728The simplest way to configure Jinja to load templates for your application29looks roughly like this::28The simplest way to configure Jinja to load templates for your29application is to use :class:`~loaders.PackageLoader`.3031.. code-block:: python3233 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 )3837This will create a template environment with the default settings and a38loader that looks up the templates in the `templates` folder inside the39`yourapplication` python package. Different loaders are available40and you can also write your own if you want to load templates from a41database or other resources. This also enables autoescaping for HTML and42XML files.39This will create a template environment with a loader that looks up40templates in the ``templates`` folder inside the ``yourapp`` Python41package (or next to the ``yourapp.py`` Python module). It also enables42autoescaping for HTML files. This loader only requires that ``yourapp``43is importable, it figures out the absolute path to the folder for you.4444To load a template from this environment you just have to call the45:meth:`get_template` method which then returns the loaded :class:`Template`::45Different loaders are available to load templates in other ways or from46other locations. They're listed in the `Loaders`_ section below. You can47also write your own if you want to load templates from a source that's48more specialized to your project.4947 template = env.get_template('mytemplate.html')50To load a template from this environment, call the :meth:`get_template`51method, which returns the loaded :class:`Template`.5249To render it with some variables, just call the :meth:`render` method::53.. code-block:: python5451 print(template.render(the='variables', go='here'))55 template = env.get_template("mytemplate.html")5657To render it with some variables, call the :meth:`render` method.5859.. code-block:: python6061 print(template.render(the="variables", go="here"))6263Using a template loader rather than passing strings to :class:`Template`64or :meth:`Environment.from_string` has multiple advantages. Besides being65a lot easier to use it also enables template inheritance.66
815 unmodified lines
modified:src/jinja2/loaders.py+17 added-12 removed
135 unmodified lines
136 )137138139class 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.141143 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.144147 >>> loader = FileSystemLoader('/path/to/templates')148 >>> loader = FileSystemLoader(['/path/to/templates', '/other/path'])145 .. code-block:: python146150 Per default the template encoding is ``'utf-8'`` which can be changed151 by setting the `encoding` parameter to something else.147 loader = FileSystemLoader("templates")148153 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.151155 >>> loader = FileSystemLoader('/path/to/templates', followlinks=True)152 .. code-block:: python153154 loader = FileSystemLoader(["/override/templates", "/default/templates"])155156 :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.161162 .. versionchanged:: 2.8158 The ``followlinks`` parameter was added.163 Added the ``followlinks`` parameter.164 """165166 def __init__(self, searchpath, encoding="utf-8", followlinks=False):167 if not isinstance(searchpath, abc.Iterable) or isinstance(searchpath, str):
399 unmodified lines