@@ -939,55 +939,142 @@ option. It uses each loader until a loader finds a match.
939
939
940
940
.. _custom-template-loaders:
941
941
942
+ .. currentmodule:: django.template.loaders.base
943
+
942
944
Custom loaders
943
- --------------
945
+ ==============
944
946
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.
950
951
951
952
.. versionchanged:: 1.8
952
953
953
954
``django.template.loaders.base.Loader`` used to be defined at
954
955
``django.template.loader.BaseLoader``.
955
956
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
959
958
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
+
F438
div>
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
961
1049
962
1050
Template origin
963
1051
===============
964
1052
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
969
1057
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``.
971
1062
972
- Templates created from a template loader will use the
973
- ``django.template.loader.LoaderOrigin`` class.
1063
+ .. class:: Origin
974
1064
975
1065
.. attribute:: name
976
1066
977
1067
The path to the template as returned by the template loader.
978
1068
For loaders that read from the file system, this is the full
979
1069
path to the template.
980
1070
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
982
1075
983
1076
The relative path to the template as passed into the
984
1077
template loader.
985
1078
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``.
0 commit comments