diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 0d131d952..ea15c41f8 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -36,6 +36,7 @@ Changelog * FormBuilder class now uses bound methods for field generation, adding custom fields is now easier and documented (LB (Ben) Johnston) * Added `WAGTAILADMIN_NOTIFICATION_INCLUDE_SUPERUSERS` setting to determine whether superusers are included in moderation email notifications (Bruno Alla) * Added a basic Dockerfile to the project template (Tom Dyson) + * StreamField blocks now allow custom `get_template` methods for overriding templates in instances (Christopher Bledsoe) * Fix: Do not remove stopwords when generating slugs from non-ASCII titles, to avoid issues with incorrect word boundaries (Sævar Öfjörð Magnússon) * Fix: The PostgreSQL search backend now preserves ordering of the `QuerySet` when searching with `order_by_relevance=False` (Bertrand Bordage) * Fix: Using `modeladmin_register` as a decorator no longer replaces the decorated class with `None` (Tim Heap) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 08b88ebc0..0d47a51ca 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -267,6 +267,7 @@ Contributors * Philipp Bosch * misraX * Bruno Alla +* Christopher Bledsoe (The Motley Fool) Translators =========== diff --git a/docs/releases/2.0.rst b/docs/releases/2.0.rst index 59248926c..a71585dc4 100644 --- a/docs/releases/2.0.rst +++ b/docs/releases/2.0.rst @@ -53,6 +53,7 @@ Other features * FormBuilder class now uses bound methods for field generation, adding custom fields is now easier and documented (LB (Ben Johnston)) * Added ``WAGTAILADMIN_NOTIFICATION_INCLUDE_SUPERUSERS`` setting to determine whether superusers are included in moderation email notifications (Bruno Alla) * Added a basic Dockerfile to the project template (Tom Dyson) + * StreamField blocks now allow custom ``get_template`` methods for overriding templates in instances (Christopher Bledsoe) Bug fixes diff --git a/wagtail/core/blocks/base.py b/wagtail/core/blocks/base.py index f40e95b4e..2e872c617 100644 --- a/wagtail/core/blocks/base.py +++ b/wagtail/core/blocks/base.py @@ -221,13 +221,20 @@ class Block(metaclass=BaseBlock): }) return context + def get_template(self, context=None): + """ + Return the template to use for rendering the block if specified on meta class. + This extraction was added to make dynamic templates possible if you override this method + """ + return getattr(self.meta, 'template', None) + def render(self, value, context=None): """ Return a text rendering of 'value', suitable for display on templates. By default, this will use a template (with the passed context, supplemented by the result of get_context) if a 'template' property is specified on the block, and fall back on render_basic otherwise. """ - template = getattr(self.meta, 'template', None) + template = self.get_template(context=context) if not template: return self.render_basic(value, context=context) diff --git a/wagtail/core/tests/test_blocks.py b/wagtail/core/tests/test_blocks.py index 858f7d22e..3f1721e32 100644 --- a/wagtail/core/tests/test_blocks.py +++ b/wagtail/core/tests/test_blocks.py @@ -3143,3 +3143,19 @@ class TestIncludeBlockTag(TestCase): 'language': 'fr', }) self.assertIn('

bonjour

', result) + + +class BlockUsingGetTemplateMethod(blocks.Block): + + my_new_template = "my_super_awesome_dynamic_template.html" + + def get_template(self): + return self.my_new_template + + +class TestOverriddenGetTemplateBlockTag(TestCase): + def test_template_is_overriden_by_get_template(self): + + block = BlockUsingGetTemplateMethod(template='tests/blocks/this_shouldnt_be_used.html') + template = block.get_template() + self.assertEquals(template, block.my_new_template)