| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: django-render-block |
| 3 |
Version: 0.8.1 |
| 4 |
Summary: Render a particular block from a template to a string. |
| 5 |
Home-page: https://github.com/clokep/django-render-block |
| 6 |
Author: Patrick Cloke |
| 7 |
Author-email: [email protected] |
| 8 |
License: UNKNOWN |
| 9 |
Download-URL: https://github.com/clokep/django-render-block |
| 10 |
Description: Django Render Block |
| 11 |
################### |
| 12 |
|
| 13 |
.. image:: https://travis-ci.org/clokep/django-render-block.svg?branch=master |
| 14 |
:target: https://travis-ci.org/clokep/django-render-block |
| 15 |
|
| 16 |
Render the content of a specific block tag from a Django template. Works for |
| 17 |
arbitrary template inheritance, even if a block is defined in the child template |
| 18 |
but not in the parent. Generally it works like ``render_to_string`` from Django, |
| 19 |
but allows you to specify a block to render. |
| 20 |
|
| 21 |
Features |
| 22 |
======== |
| 23 |
|
| 24 |
* Render a specific block from a template |
| 25 |
* Fully supports the Django templating engine |
| 26 |
* Partially supports the `Jinja2 <http://jinja.pocoo.org/>`__ engine: it does |
| 27 |
not currently process the ``extends`` tag. |
| 28 |
|
| 29 |
Requirements |
| 30 |
============ |
| 31 |
|
| 32 |
Django Render Block supports Django 2.2, 3.0, and 3.1 on Python 3.6, 3.7, 3.8, and 3.9. |
| 33 |
|
| 34 |
Examples |
| 35 |
======== |
| 36 |
|
| 37 |
In ``test1.html``: |
| 38 |
|
| 39 |
.. code-block:: jinja |
| 40 |
|
| 41 |
{% block block1 %}block1 from test1{% endblock %} |
| 42 |
{% block block2 %}block2 from test1{% endblock %} |
| 43 |
|
| 44 |
In ``test2.html``: |
| 45 |
|
| 46 |
.. code-block:: jinja |
| 47 |
|
| 48 |
{% extends 'test1.html' %} |
| 49 |
{% block block1 %}block1 from test2{% endblock %} |
| 50 |
|
| 51 |
And from the Python shell: |
| 52 |
|
| 53 |
.. code-block:: python |
| 54 |
|
| 55 |
>>> from render_block import render_block_to_string |
| 56 |
>>> print render_block_to_string('test2.html', 'block1') |
| 57 |
u'block1 from test2' |
| 58 |
>>> print render_block_to_string('test2.html', 'block2') |
| 59 |
u'block2 from test1' |
| 60 |
|
| 61 |
It can also accept a context as a ``dict`` (just like ``render_to_string``), in |
| 62 |
``test3.html``: |
| 63 |
|
| 64 |
.. code-block:: jinja |
| 65 |
|
| 66 |
{% block block3 %}Render this {{ variable }}!{% endblock %} |
| 67 |
|
| 68 |
And from Python: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
>>> print render_block_to_string('test3.html', 'block3', {'variable': 'test'}) |
| 73 |
u'Render this test!' |
| 74 |
|
| 75 |
API Reference |
| 76 |
============= |
| 77 |
|
| 78 |
The API is simple and attempts to mirror the built-in ``render_to_string`` API. |
| 79 |
|
| 80 |
``render_block_to_string(template_name, block_name, context=None, request=None)`` |
| 81 |
|
| 82 |
``template_name`` |
| 83 |
The name of the template to load and render. If it’s a list of template |
| 84 |
names, Django uses ``select_template()`` instead of ``get_template()`` |
| 85 |
to find the template. |
| 86 |
|
| 87 |
``block_name`` |
| 88 |
The name of the block to render from the above template. |
| 89 |
|
| 90 |
``context`` |
| 91 |
A ``dict`` to be used as the template’s context for rendering. A ``Context`` |
| 92 |
object can be provided for Django templates. |
| 93 |
|
| 94 |
``context`` is optional. If not provided, an empty context will be used. |
| 95 |
|
| 96 |
``request`` |
| 97 |
The request object used to render the template. |
| 98 |
|
| 99 |
``request`` is optional and works only for Django templates. If both context and request |
| 100 |
are provided, a ``RequestContext`` will be used instead of a ``Context``. |
| 101 |
|
| 102 |
Exceptions |
| 103 |
---------- |
| 104 |
|
| 105 |
Like ``render_to_string`` this will raise the following exceptions: |
| 106 |
|
| 107 |
``TemplateDoesNotExists`` |
| 108 |
Raised if the template(s) specified by ``template_name`` cannot be |
| 109 |
loaded. |
| 110 |
|
| 111 |
``TemplateSyntaxError`` |
| 112 |
Raised if the loaded template contains invalid syntax. |
| 113 |
|
| 114 |
There are also two additional errors that can be raised: |
| 115 |
|
| 116 |
``BlockNotFound`` |
| 117 |
Raised if the block given by ``block_name`` does not exist in the |
| 118 |
template. |
| 119 |
|
| 120 |
``UnsupportedEngine`` |
| 121 |
Raised if a template backend besides the Django backend is used. |
| 122 |
|
| 123 |
Contributing |
| 124 |
============ |
| 125 |
|
| 126 |
If you find a bug or have an idea for an improvement to Django Render Block, |
| 127 |
please |
| 128 |
`file an issue <https://github.com/clokep/django-render-block/issues/new>`_ or |
| 129 |
provide a pull request! Check the |
| 130 |
`list of issues <https://github.com/clokep/django-render-block/issues/>`_ for |
| 131 |
ideas of what to work on. |
| 132 |
|
| 133 |
Attribution |
| 134 |
=========== |
| 135 |
|
| 136 |
This is based on a few sources: |
| 137 |
|
| 138 |
* Originally `Django Snippet 769 <https://djangosnippets.org/snippets/769/>`__ |
| 139 |
* Updated version `Django Snippet 942 <https://djangosnippets.org/snippets/942/>`__ |
| 140 |
* A version of the snippets was ported as `Django-Block-Render <https://github.com/uniphil/Django-Block-Render/>`_ |
| 141 |
* Additionally inspired by part of `django-templated-email <https://github.com/BradWhittington/django-templated-email/blob/master/templated_email/utils.py>`_ |
| 142 |
* Also based on a `StackOverflow answer 2687173 <http://stackoverflow.com/questions/2687173/django-how-can-i-get-a-block-from-a-template>`_ |
| 143 |
|
| 144 |
.. :changelog: |
| 145 |
|
| 146 |
Changelog |
| 147 |
######### |
| 148 |
|
| 149 |
0.8.1 (October 15, 2020) |
| 150 |
======================== |
| 151 |
|
| 152 |
* Fixes a regression in v0.8 where a ``Context`` could not be re-used. See |
| 153 |
`#25 <https://github.com/clokep/django-render-block/pull/25>`_, contributed |
| 154 |
by @evanbrumley. |
| 155 |
|
| 156 |
0.8 (October 6, 2020) |
| 157 |
===================== |
| 158 |
|
| 159 |
* ``render_block_to_string`` now forwards the ``Context`` passed as ``context`` parameter. |
| 160 |
(`#21 <https://github.com/clokep/django-render-block/pull/21>`_, by @bblanchon) |
| 161 |
* Drop support for Python 3.5, officially support Python 3.9. |
| 162 |
|
| 163 |
0.7 (July 13, 2020) |
| 164 |
=================== |
| 165 |
|
| 166 |
* Drop support for Django < 2.2. |
| 167 |
* Officially support Django 3.0, 3.1. |
| 168 |
* Drop support for Python 2.7. |
| 169 |
* Officially support Python 3.8. |
| 170 |
|
| 171 |
0.6 (May 8, 2019) |
| 172 |
================= |
| 173 |
|
| 174 |
* Supports Django 1.11, Django 2.1, and Django 2.2. |
| 175 |
* Supports Python 2.7, 3.5, 3.6, and 3.7. |
| 176 |
* ``render_block_to_string`` now optionally accepts a ``request`` parameter. |
| 177 |
If given, a ``RequestContext`` instead of a ``Context`` is used when |
| 178 |
rendering with the Django templating engine. See |
| 179 |
`#15 <https://github.com/clokep/django-render-block/pull/15>`_, thanks to |
| 180 |
@vintage. |
| 181 |
|
| 182 |
0.5 (September 1, 2016) |
| 183 |
======================= |
| 184 |
|
| 185 |
* Fixes a major issue with inheriting templates and rendering a block found in |
| 186 |
the parent template, but overwriting part of it in the child template. |
| 187 |
(`#8 <https://github.com/clokep/django-render-block/pull/8>`_) |
| 188 |
|
| 189 |
0.4 (August 4, 2016) |
| 190 |
==================== |
| 191 |
|
| 192 |
* Initial support for using the `Jinja2 <http://jinja.pocoo.org/>`_ templating |
| 193 |
engine. See README for caveats. (`#3 <https://github.com/clokep/django-render-block/pull/3>`_) |
| 194 |
* Support Django 1.10. (`#5 <https://github.com/clokep/django-render-block/pull/5>`_) |
| 195 |
* Support Python 3. (`#6 <https://github.com/clokep/django-render-block/pull/6>`_) |
| 196 |
|
| 197 |
0.3.1 (June 1, 2016) |
| 198 |
==================== |
| 199 |
|
| 200 |
* Refactoring to make more generic (for potentially supporting multiple |
| 201 |
templating engines). |
| 202 |
|
| 203 |
0.3 (May 27, 2016) |
| 204 |
================== |
| 205 |
|
| 206 |
* Largely rewritten. |
| 207 |
* Updated to support modern Django (1.8, 1.9): |
| 208 |
|
| 209 |
* Guards against different template backends. |
| 210 |
* Uses internal APIs for each node. |
| 211 |
* Removed ``context_instance`` parameter. |
| 212 |
* Support for calling ``{{ block.super }}``. |
| 213 |
|
| 214 |
0.2.2 (January 10, 2011) |
| 215 |
======================== |
| 216 |
|
| 217 |
* Updated per |
| 218 |
`comment 3466 on Django Snippet 942 <https://djangosnippets.org/snippets/942/#c3466>`_ |
| 219 |
to fix an issue with nested extends. The specific bug was not reproducible, |
| 220 |
but the additional code shouldn't hurt. |
| 221 |
|
| 222 |
0.2.1 (August 27, 2010) |
| 223 |
======================= |
| 224 |
|
| 225 |
* Updated per |
| 226 |
`comment 3237 on Django Snippet 942 <https://djangosnippets.org/snippets/942/#c3237>`_ |
| 227 |
to remove a pointless render. The specific bug was not reproducible, but the |
| 228 |
code is extraneous. |
| 229 |
|
| 230 |
0.2 (August 4, 2008) |
| 231 |
==================== |
| 232 |
|
| 233 |
* Updated version from |
| 234 |
`Django Snippet 942 <https://djangosnippets.org/snippets/942/>`_ by zbyte64. |
| 235 |
* Improves include: |
| 236 |
|
| 237 |
1. Simpler/better handling of "extends" block tag |
| 238 |
2. Searches If/Else blocks |
| 239 |
3. Less code |
| 240 |
4. Allow list of templates to be passed which is closer to the behavior of |
| 241 |
render_to_response |
| 242 |
|
| 243 |
|
| 244 |
0.1 (May 22, 2008) |
| 245 |
================== |
| 246 |
|
| 247 |
* Initial version from |
| 248 |
`Django Snippet 769 <https://djangosnippets.org/snippets/769/>`_ by sciyoshi. |
| 249 |
* Supports Django 0.96. |
| 250 |
|
| 251 |
|
| 252 |
Keywords: django,template,block,templates,render,context |
| 253 |
Platform: UNKNOWN |
| 254 |
Classifier: Development Status :: 4 - Beta |
| 255 |
Classifier: Intended Audience :: Developers |
| 256 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
| 257 |
Classifier: Environment :: Web Environment |
| 258 |
Classifier: Topic :: Internet |
| 259 |
Classifier: Framework :: Django |
| 260 |
Classifier: Framework :: Django :: 2.2 |
| 261 |
Classifier: Framework :: Django :: 3.0 |
| 262 |
Classifier: Framework :: Django :: 3.1 |
| 263 |
Classifier: Programming Language :: Python |
| 264 |
Classifier: Programming Language :: Python :: 3.6 |
| 265 |
Classifier: Programming Language :: Python :: 3.7 |
| 266 |
Classifier: Programming Language :: Python :: 3.8 |
| 267 |
Classifier: Programming Language :: Python :: 3.9 |
| 268 |
Classifier: License :: OSI Approved :: ISC License (ISCL) |
| 269 |
Requires-Python: >=3.5 |
| 270 |
Description-Content-Type: text/x-rst |