def test_standalone_renderer(self): """The renderer also works when used without a wrapping template""" renderer = TemplatePluginRenderer() renderer.register_template_renderer(HTML, "renderer/html.html") page = Page.objects.create(template_key="standard") HTML.objects.create(parent=page, ordering=10, region="main", html="<b>Hello</b>") regions = renderer.regions(page) self.assertEqual(regions.render("main", Context()), "<b>Hello</b>\n") regions = renderer.regions(page) self.assertEqual(regions.render("main", Context({"outer": "Test"})), "<b>Hello</b>Test\n") regions = renderer.regions(page) self.assertEqual( regions.render("main", Context({"outer": "Test2"}), timeout=3), "<b>Hello</b>Test2\n", ) regions = renderer.regions(page) # Output stays the same. self.assertEqual( regions.render("main", Context({"outer": "Test3"}), timeout=3), "<b>Hello</b>Test2\n", ) self.assertEqual(regions.cache_key("main"), "testapp.page-%s-main" % page.pk)
def test_standalone_renderer(self): """The renderer also works when used without a wrapping template""" renderer = TemplatePluginRenderer() renderer.register_template_renderer( HTML, 'renderer/html.html', ) page = Page.objects.create(template_key='standard', ) HTML.objects.create( parent=page, ordering=10, region='main', html='<b>Hello</b>', ) regions = renderer.regions(page) self.assertEqual( regions.render('main', Context()), '<b>Hello</b>\n', ) regions = renderer.regions(page) self.assertEqual( regions.render('main', Context({'outer': 'Test'})), '<b>Hello</b>Test\n', )
def test_custom_regions(self): class CustomRegions(Regions): @cached_render # Not necessary for this test, but good style. def render(self, region, context=None): html = [] for plugin in self._contents[region]: out = self._renderer.render_plugin_in_context( plugin, context) if isinstance(plugin, HTML): html.extend(['<div class="raw">', out, "</div>"]) else: html.append(out) return mark_safe("".join(html)) renderer = TemplatePluginRenderer(regions_class=CustomRegions) # List of templates works too: renderer.register_template_renderer(HTML, ["renderer/html.html"]) renderer.register_string_renderer( RichText, lambda plugin: mark_safe(plugin.text)) page = Page.objects.create(template_key="standard") HTML.objects.create(parent=page, ordering=10, region="main", html="<b>Hello</b>") RichText.objects.create(parent=page, ordering=20, region="main", text="<strong>yes</strong>") regions = renderer.regions(page) self.assertEqual( regions.render("main"), '<div class="raw"><b>Hello</b>\n</div><strong>yes</strong>', )