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", "renderer/html.html"]) page = Page.objects.create(template_key="standard") HTML.objects.create(parent=page, ordering=10, region="main", html="<b>Hello</b>") regions = Regions.from_item(page, renderer=renderer) self.assertEqual(regions.render("main", Context()), "<b>Hello</b>\n") regions = Regions.from_item(page, renderer=renderer) self.assertEqual(regions.render("main", Context({"outer": "Test"})), "<b>Hello</b>Test\n") regions = Regions.from_item(page, renderer=renderer, timeout=3) self.assertEqual(regions.render("main", Context({"outer": "Test2"})), "<b>Hello</b>Test2\n") regions = Regions.from_item(page, renderer=renderer, timeout=3) # Output stays the same. self.assertEqual(regions.render("main", Context({"outer": "Test3"})), "<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>', )
def test_plugin_template_instance(self): renderer = TemplatePluginRenderer() renderer.register_template_renderer(HTML, Template("{{ plugin.html|safe }}")) page = Page.objects.create(template_key="standard") HTML.objects.create(parent=page, ordering=10, region="main", html="<b>Hello</b>") regions = Regions.from_item(page, renderer=renderer) self.assertEqual(regions.render("main", Context()), "<b>Hello</b>") self.assertEqual(regions.render("main", None), "<b>Hello</b>")
renderer = TemplatePluginRenderer() renderer.register_string_renderer( RichText, plugins.render_richtext, ) renderer.register_string_renderer( Image, lambda plugin: format_html( '<figure><img src="{}" alt=""/><figcaption>{}</figcaption></figure>', plugin.image.url, plugin.caption, ), ) renderer.register_template_renderer( Snippet, lambda plugin: plugin.template_name, lambda plugin, context: {'additional': 'context'}, ) renderer.register_string_renderer( External, plugins.render_external, ) renderer.register_string_renderer( HTML, plugins.render_html, ) def page_detail(request, path=None): page = get_object_or_404( Page.objects.active(),
from .models import HTML, External, Image, Page, RichText, Snippet renderer = TemplatePluginRenderer() renderer.register_string_renderer(RichText, richtext.render_richtext) renderer.register_string_renderer( Image, lambda plugin: format_html( '<figure><img src="{}" alt=""/><figcaption>{}</figcaption></figure>', plugin.image.url, plugin.caption, ), ) renderer.register_template_renderer( Snippet, lambda plugin: plugin.template_name, lambda plugin, context: {"additional": "context"}, ) renderer.register_string_renderer(External, external.render_external) renderer.register_string_renderer(HTML, html.render_html) def page_detail(request, path=None): page = get_object_or_404(Page.objects.active(), path=("/%s/" % path) if path else "/") page.activate_language(request) if page.redirect_to_url or page.redirect_to_page: return redirect(page.redirect_to_url or page.redirect_to_page) return render( request,
from django.utils.html import mark_safe from feincms3.renderer import TemplatePluginRenderer from app.pages import models renderer = TemplatePluginRenderer() renderer.register_string_renderer( models.RichText, lambda plugin: mark_safe(plugin.text), ) renderer.register_template_renderer( models.Image, 'plugins/image.html', )
from django.utils.html import mark_safe from feincms3.renderer import TemplatePluginRenderer from app.pages import models renderer = TemplatePluginRenderer() renderer.register_string_renderer( models.RichText, lambda plugin: mark_safe(plugin.text), ) renderer.register_template_renderer( models.Image, 'plugins/image.html', )
from django.shortcuts import get_object_or_404, render from feincms3 import plugins from feincms3.renderer import TemplatePluginRenderer from .models import Article, HTML, Download renderer = TemplatePluginRenderer() renderer.register_string_renderer(HTML, plugins.html.render_html) renderer.register_template_renderer(Download, "plugins/download.html") def article_detail(request, pk): article = get_object_or_404(Article, pk=pk) return render( request, "article.html", { "article": article, "regions": renderer.regions(article) }, )
from django.shortcuts import get_object_or_404, redirect, render from feincms3.plugins import external, html, richtext from feincms3.regions import Regions from feincms3.renderer import TemplatePluginRenderer from .models import HTML, External, Image, Page, RichText, Snippet renderer = TemplatePluginRenderer() renderer.register_string_renderer(RichText, richtext.render_richtext) renderer.register_template_renderer(Image, "renderer/image.html") Snippet.register_with(renderer) renderer.register_string_renderer(External, external.render_external) renderer.register_string_renderer(HTML, html.render_html) def page_detail(request, path=None): page = get_object_or_404(Page.objects.active(), path=("/%s/" % path) if path else "/") page.activate_language(request) if page.get_redirect_url(): return redirect(page.get_redirect_url()) return render( request, page.template.template_name, { "page": page, "regions":