8000 Added docs for new template loader api. · ddriddle/django@8ae04e7 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8ae04e7

Browse files
Added docs for new template loader api.
Refs django#15053.
1 parent 65a7a0d commit 8ae04e7

File tree

3 files changed

+141
-29
lines changed

3 files changed

+141
-29
lines changed

docs/internals/deprecation.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ details on these changes.
4444
* ``django.template.loaders.base.Loader.supports_recursion()``
4545
* ``django.template.loaders.cached.Loader.supports_recursion()``
4646

47-
* The ``load_template and ``load_template_sources`` template loader methods
48-
will be removed.
47+
* The ``load_template()`` and ``load_template_sources()`` template loader
48+
methods will be removed.
4949

5050
* The ``template_dirs`` argument for template loaders will be removed:
5151

@@ -55,7 +55,8 @@ details on these changes.
5555
* ``django.template.loaders.cached.Loader.get_template_sources()``
5656
* ``django.template.loaders.filesystem.Loader.get_template_sources()``
5757

58-
* The ``django.template.loaders.base.Loader.__call__`` method will be removed.
58+
* The ``django.template.loaders.base.Loader.__call__()`` method will be
59+
removed.
5960

6061
.. _deprecation-removed-in-2.0:
6162

docs/ref/templates/api.txt

Lines changed: 113 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -939,55 +939,142 @@ option. It uses each loader until a loader finds a match.
939939

940940
.. _custom-template-loaders:
941941

942+
.. currentmodule:: django.template.loaders.base
943+
942944
Custom loaders
943-
--------------
945+
==============
944946

945-
Custom ``Loader`` classes should inherit from
946-
``django.template.loaders.base.Loader`` and override the
947-
``load_template_source()`` method, which takes a ``template_name`` argument,
948-
loads the template from disk (or elsewhere), and returns a tuple:
949-
``(template_string, template_origin)``.
947+
It's possible to load templates from additional sources using custom template
948+
loaders. Custom ``Loader`` classes should inherit from
949+
``django.template.loaders.base.Loader`` and define the ``get_contents()`` and
950+
``get_template_sources()`` methods.
950951

951952
.. versionchanged:: 1.8
952953

953954
``django.template.loaders.base.Loader`` used to be defined at
954955
``django.template.loader.BaseLoader``.
955956

956-
The ``load_template()`` method of the ``Loader`` class retrieves the template
957-
string by calling ``load_template_source()``, instantiates a ``Template`` from
958-
the template source, and returns a tuple: ``(template, template_origin)``.
957+
.. versionchanged:: 1.9
959958

960-
.. currentmodule:: django.template
959+
In previous versions of Django, custom loaders defined a single method:
960+
``load_template_source()``.
961+
962+
Loader methods
963+
--------------
964+
965+
.. class:: Loader
966+
967+
Loads templates from a given source, such as the filesystem or a database.
968+
969+
.. method:: get_template_sources(template_name)
970+
971+
A method that takes a ``template_name`` and yields
972+
:class:`~django.template.base.Origin` instances for each possible
973+
source.
974+
975+
For example, the filesystem loader may receive ``'index.html'`` as a
976+
``template_name`` argument. This method would yield origins for the
977+
full path of ``index.html`` as it appears in each template directory
978+
the loader looks at.
979+
980+
The method doesn't need to verify that the template exists at a given
981+
path, but it should ensure the path is valid. For instance, the
982+
filesystem loader makes sure the path lies under a valid template
983+
directory.
984+
985+
.. method:: get_contents(origin)
986+
987+
Returns the contents for a template given a
988+
:class:`~django.template.base.Origin` instance.
989+
990+
This is where a filesystem loader would read contents from the
991+
filesystem, or a database loader would read from the database. If a
992+
matching template doesn't exist, this should raise a
993+
:exc:`~django.template.TemplateDoesNotExist` error.
994+
995+
.. method:: get_template(template_name, skip=None)
996+
997+
Returns a ``Template`` object for a given ``template_name`` by looping
998+
through results from :meth:`get_template_sources` and calling
999+
:meth:`get_contents`. This returns the first matching template. If no
1000+
template is found, :exc:`~django.template.TemplateDoesNotExist` is
1001+
raised.
1002+
1003+
The optional ``skip`` argument is a list of origins to ignore when
1004+
extending templates. This allow templates to extend other templates of
1005+
the same name. It also used to avoid recursion errors.
1006+
1007+
In general, it is enough to define :meth:`get_template_sources` and
1008+
:meth:`get_contents` for custom template loaders. ``get_template()``
1009+
will usually not need to be overridden.
1010+
1011+
.. method:: load_template_source(template_name, template_dirs=None)
1012+
1013+
Returns a tuple of (``template_string``, ``template_origin``), where
1014+
``template_string`` is a string containing the template contents,
1015+
and ``template_origin`` is a string identifying the template source.
1016+
A filesystem-based loader may return the full path to the file as the
1017+
``template_origin``, for example.
1018+
1019+
``template_dirs`` is an optional argument used to control which
1020+
directories the loader will search.
1021+
1022+
This method is called automatically by :meth:`load_template` and should
1023+
be overridden when writing custom template loaders.
1024+
1025+
.. deprecated:: 1.9
1026+
1027+
Custom loaders should use :meth:`get_template` and
1028+
:meth:`get_contents` instead.
1029+
1030+
.. method:: load_template(template_name, template_dirs=None)
1031+
1032+
Returns a tuple of (``template``, ``template_origin``), where ``template``
1033+
is a ``Template`` object and ``template_origin`` is a string identifying
1034+
the template source. A filesystem-based loader may return the full
1035+
path to the file as the ``template_origin``, for example.
1036+
1037+
.. deprecated:: 1.9
1038+
1039+
Custom loaders should use :meth:`get_template` and
1040+
:meth:`get_contents` instead.
1041+
1042+
.. admonition:: Building your own
1043+
1044+
For examples, `read the source code for Django's built-in loaders`_.
1045+
1046+
.. _read the source code for Django's built-in loaders: https://github.com/django/django/tree/master/django/template/loaders
1047+
1048+
.. currentmodule:: django.template.base
9611049

9621050
Template origin
9631051
===============
9641052

965-
When an :class:`~django.template.Engine` is initialized with ``debug=True``,
966-
its templates have an ``origin`` attribute depending on the source they are
967-
loaded from. For engines initialized by Django, ``debug`` defaults to the
968-
value of :setting:`DEBUG`.
1053+
Templates have an ``origin`` containing attributes depending on the source
1054+
they are loaded from.
1055+
1056+
.. versionchanged:: 1.9
9691057

970-
.. class:: loader.LoaderOrigin
1058+
Django used to create an origin based on
1059+
``django.template.loader.LoaderOrigin`` or
1060+
``django.template.base.StringOrigin``. These have been replaced by
1061+
``django.template.base.Origin``.
9711062

972-
Templates created from a template loader will use the
973-
``django.template.loader.LoaderOrigin`` class.
1063+
.. class:: Origin
9741064

9751065
.. attribute:: name
9761066

9771067
The path to the template as returned by the template loader.
9781068
For loaders that read from the file system, this is the full
9791069
path to the template.
9801070

981-
.. attribute:: loadname
1071+
If the template is instantiated directly rather than through a
1072+
template loader, this is a string value of ``<unknown_source>``.
1073+
1074+
.. attribute:: template_name
9821075

9831076
The relative path to the template as passed into the
9841077
template loader.
9851078

986-
.. class:: StringOrigin
987-
988-
Templates created from a ``Template`` class will use the
989-
``django.template.StringOrigin`` class.
990-
991-
.. attribute:: source
992-
993-
The string used to create the template.
1079+
If the template is instantiated directly rather than through a
1080+
template loader, this is ``None``.

docs/releases/1.9.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ Templates
223223
* :meth:`Context.update() <django.template.Context.update>` can now be used as
224224
a context manager.
225225

226+
* Django template loaders can now extend templates recursively.
227+
228+
* The debug page template postmortem now include output from each engine that
229+
is installed.
230+
226231
Requests and Responses
227232
^^^^^^^^^^^^^^^^^^^^^^
228233

@@ -343,6 +348,16 @@ Dropped support for PostgreSQL 9.0
343348
Upstream support for PostgreSQL 9.0 ended in September 2015. As a consequence,
344349
Django 1.9 sets 9.1 as the minimum PostgreSQL version it officially supports.
345350

351+
Template ``LoaderOrigin`` and ``StringOrigin`` are removed
352+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
353+
354+
In previous versions of Django, when a template engine was initialized with
355+
debug as ``True``, an instance of ``django.template.loader.LoaderOrigin`` or
356+
``django.template.base.StringOrigin`` was set as the origin attribute on the
357+
template object. These classes have been combined into
358+
:class:`~django.template.base.Origin` and is now always set regardless of the
359+
engine debug setting.
360+
346361
Miscellaneous
347362
~~~~~~~~~~~~~
348363

@@ -415,6 +430,15 @@ models is now obsolete. As soon as your code doesn't call any of the deprecated
415430
methods, you can simply remove the ``objects = GeoManager()`` lines from your
416431
models.
417432

433+
Template loader APIs have changed
434+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
435+
436+
Django template loaders have been updated to allow recursive template
437+
extending. This change necessitated a new template loader API. The old
438+
``load_template()`` and ``load_template_sources()`` methods are now deprecated.
439+
Details about the new API can be found :ref:`in the template loader
440+
documentation <custom-template-loaders>`.
441+
418442
Miscellaneous
419443
~~~~~~~~~~~~~
420444

0 commit comments

Comments
 (0)
0