clokep / django-render-block

Render the content of a specific block tag from a Django template.
ISC License
241 stars 23 forks source link

Forward Context instance #21

Closed bblanchon closed 4 years ago

bblanchon commented 4 years ago

This pull request makes render_block_to_string() forwards the Context instance provided by the user.

For example, this allows the user to disable autoescape, like so:

result = render_block_to_string(template, block, Context(context, autoescape=False))

Such a feature is handy when rendering a text-only template, like an email or a notification message.

clokep commented 4 years ago

Seems pretty reasonable. Thanks for including tests!

Any idea if there are changes that should be made to the Jinja code in this situation?

bblanchon commented 4 years ago

No, I don't know if there is something similar for Jinja, but since Context is in the django.template namespace, the user knows it's a feature limited to Django templates.

BTW, django-templated-email expects this behavior; they've been waiting for a fix since 2017 😮.

clokep commented 4 years ago

No, I don't know if there is something similar for Jinja, but since Context is in the django.template namespace, the user knows it's a feature limited to Django templates.

Right so theoretically you could create a django.template.Context instance and pass it into render_block_to_string which could call into the Jinja2 code. That's just silly (although I actually think Jinja2 would convert it to a dict, which probably works). Anyway, I don't think we need to do anything about it.

clokep commented 4 years ago

FYI I released 0.8 with this change in it. Thanks!