| 1096 | |
| 1097 | The ``render_to_string`` shortcut |
| 1098 | =================================== |
| 1099 | |
| 1100 | .. function:: loader.render_to_string(template_name, context=None, context_instance=None) |
| 1101 | |
| 1102 | To cut down on the repetitive nature of loading and rendering |
| 1103 | templates, Django provides a shortcut function which largely |
| 1104 | automates the process: ``render_to_string()`` in |
| 1105 | :mod:`django.template.loader`, which loads a template, renders it and |
| 1106 | returns the resulting string:: |
| 1107 | |
| 1108 | from django.template.loader import render_to_string |
| 1109 | rendered = render_to_string('my_template.html', {'foo': 'bar'}) |
| 1110 | |
| 1111 | The ``render_to_string`` shortcut takes one required argument -- |
| 1112 | ``template_name``, which should be the name of the template to load |
| 1113 | and render (or a list of template names, in which case Django will use |
| 1114 | the first template in the list that exists) -- and two optional arguments: |
| 1115 | |
| 1116 | ``context`` |
| 1117 | A dictionary to be used as variables and values for the |
| 1118 | template's context. This should be passed as the second |
| 1119 | positional argument. |
| 1120 | |
| 1121 | .. versionchanged:: 1.8 |
| 1122 | |
| 1123 | The ``context`` argument used to be called ``dictionary``. That name |
| 1124 | is deprecated in Django 1.8 and will be removed in Django 2.0. |
| 1125 | |
| 1126 | ``context_instance`` |
| 1127 | An instance of :class:`~django.template.Context` or a subclass (e.g., an |
| 1128 | instance of :class:`~django.template.RequestContext`) to use as the |
| 1129 | template's context. This can also be passed as the third positional argument. |
| 1130 | |
| 1131 | .. deprecated:: 1.8 |
| 1132 | |
| 1133 | The ``context_instance`` argument is deprecated. Simply use ``context``. |
| 1134 | |
| 1135 | See also the :func:`~django.shortcuts.render_to_response()` shortcut, which |
| 1136 | calls ``render_to_string`` and feeds the result into an :class:`~django.http.HttpResponse` |
| 1137 | suitable for returning directly from a view. |