Render a particular block from a template to a string.
Project description
Django Render Block
Render the content of a specific block tag from a Django template. Works for arbitrary template inheritance, even if a block is defined in the child template but not in the parent. Generally it works like render_to_string from Django, but allows you to specify a block to render.
Features
Render a specific block from a template
Fully supports the Django templating engine
Partially supports the Jinja2 engine: it does not currently process the extends tag.
Requirements
Django Render Block supports Django 4.2, 5.0, and 5.1 on Python 3.8, 3.9, 3.10, 3.11 and 3.12 (see the Django documentation for which versions of Python are supported by particular Django versions).
Examples
In test1.html:
{% block block1 %}block1 from test1{% endblock %}
{% block block2 %}block2 from test1{% endblock %}
In test2.html:
{% extends 'test1.html' %}
{% block block1 %}block1 from test2{% endblock %}
And from the Python shell:
>>> from render_block import render_block_to_string
>>> print(render_block_to_string('test2.html', 'block1'))
'block1 from test2'
>>> print(render_block_to_string('test2.html', 'block2'))
'block2 from test1'
It can also accept a context as a dict (just like render_to_string), in test3.html:
{% block block3 %}Render this {{ variable }}!{% endblock %}
And from Python:
>>> print(render_block_to_string('test3.html', 'block3', {'variable': 'test'}))
'Render this test!'
API Reference
The API is simple and attempts to mirror the built-in render_to_string API.
render_block_to_string(template_name, block_name, context=None, request=None)
- template_name
The name of the template to load and render. If it’s a list of template names, Django uses select_template() instead of get_template() to find the template.
- block_name
The name of the block to render from the above template.
- context
A dict to be used as the template’s context for rendering. A Context object can be provided for Django templates.
context is optional. If not provided, an empty context will be used.
- request
The request object used to render the template.
request is optional and works only for Django templates. If both context and request are provided, a RequestContext will be used instead of a Context.
Exceptions
Like render_to_string this will raise the following exceptions:
- TemplateDoesNotExists
Raised if the template(s) specified by template_name cannot be loaded.
- TemplateSyntaxError
Raised if the loaded template contains invalid syntax.
There are also two additional errors that can be raised:
- BlockNotFound
Raised if the block given by block_name does not exist in the template.
- UnsupportedEngine
Raised if a template backend besides the Django backend is used.
Contributing
If you find a bug or have an idea for an improvement to Django Render Block, please file an issue or provide a pull request! Check the list of issues for ideas of what to work on.
Attribution
This is based on a few sources:
Originally Django Snippet 769
Updated version Django Snippet 942
A version of the snippets was ported as Django-Block-Render
Additionally inspired by part of django-templated-email
Also based on a StackOverflow answer 2687173
Changelog
0.10 (July 15, 2024)
No changes from 0.10b1.
0.10b1 (July 1, 2024)
Bugfixes
Fixes exception propagation when rendering templates. Contributed by @yaakovLowenstein. (#52)
Fix rendering blocks over multiple extended templates. (#56)
Maintenance
0.9.2 (October 18, 2022)
Maintenance
0.9.1 (December 15, 2021)
Maintenance
0.9 (December 14, 2021)
Maintenance
0.8.1 (October 15, 2020)
Bugfixes
Fixes a regression in v0.8 where a Context could not be re-used. Contributed by @evanbrumley. (#25)
0.8 (October 6, 2020)
Bugfixes
render_block_to_string now forwards the Context passed as context parameter. Contributed by @bblanchon. (#21)
Maintenance
Drop support for Python 3.5, support Python 3.9. (#22)
0.7 (July 13, 2020)
Maintenance
0.6 (May 8, 2019)
Improvements
Maintenance
0.5 (September 1, 2016)
Bugfixes
Fixes a major issue with inheriting templates and rendering a block found in the parent template, but overwriting part of it in the child template. (#8)
0.4 (August 4, 2016)
Improvements
Maintenance
0.3.1 (June 1, 2016)
Maintenance
Refactoring to make more generic (for potentially supporting multiple templating engines).
0.3 (May 27, 2016)
Largely rewritten.
Support Django 1.8 and 1.9:
Guards against different template backends.
Uses internal APIs for each node.
Removed context_instance parameter.
Support for calling {{ block.super }}.
0.2.2 (January 10, 2011)
Updated per comment 3466 on Django Snippet 942 by eugenyboger to fix an issue with nested extends. The specific bug was not reproducible, but the additional code shouldn’t hurt.
0.2.1 (August 27, 2010)
Updated per comment 3237 on Django Snippet 942 by chadselph to remove a pointless render. The specific bug was not reproducible, but the removed code was extraneous.
0.2 (August 4, 2008)
Updated version from Django Snippet 942 by zbyte64.
Improves include:
Simpler/better handling of “extends” block tag
Searches If/Else blocks
Less code
Allow list of templates to be passed which is closer to the behavior of render_to_response
0.1 (May 22, 2008)
Initial version from Django Snippet 769 by sciyoshi.
Support Django 0.96.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_render_block-0.10.tar.gz
.
File metadata
- Download URL: django_render_block-0.10.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23fbc9cfed17aac47e936e65f0c29ae186765a41234cdc8dbf88d4a9377fdd76 |
|
MD5 | ad3418449ead374e1c4f0cd589ee61e5 |
|
BLAKE2b-256 | 5cc14422e64e9cf25dc5025625605706de5fe44fcef87253fe0067f526bd0d7b |
File details
Details for the file django_render_block-0.10-py3-none-any.whl
.
File metadata
- Download URL: django_render_block-0.10-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4deeec70a360aa90385443aeae1213ffb2acbfe2b5fb09d4afe47e75a29240c3 |
|
MD5 | 537a5f79db89d823e00490579f969662 |
|
BLAKE2b-256 | 9361f4c9599d1a3f3aa88692c97f7ad22040373405fe58c5bf191b52ff637d8c |