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_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>")
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_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_restart_subregion(self): """Restarting the subregion we're already in works correctly""" class RestartRegions(Regions): def handle_restart(self, items, context): first = True yield '<div class="stuff">' while True: yield self.renderer.render_plugin_in_context( items.popleft(), context ) # Item isn't the first and explicitly specifies # subregion="restart", restart subregion if ( not first and items and matches(items[0], subregions={None, "restart"}) ): break first = False yield "</div>" restart_renderer = TemplatePluginRenderer() restart_renderer.register_string_renderer(Text, lambda plugin: plugin.text) restart_renderer.register_string_renderer(Command, "") regions = RestartRegions( contents={ "main": [ Text(text="before"), Command(subregion="restart"), Text(text="first"), Command(subregion="restart"), Text(text="second"), Command(subregion=""), Text(text="after"), ] }, renderer=restart_renderer, ) self.assertEqual( regions.render("main"), 'before<div class="stuff">first</div><div class="stuff">second</div>after', )
from django.shortcuts import get_object_or_404, redirect, render from django.utils.html import format_html from feincms3 import plugins from feincms3.renderer import TemplatePluginRenderer from .models import HTML, External, Image, Page, RichText, Snippet 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(
subregion = "teasers" class FAQ(SimpleNamespace): subregion = "faq" class Command(SimpleNamespace): subregion = "" class File(SimpleNamespace): pass renderer = TemplatePluginRenderer() renderer.register_string_renderer(Text, lambda plugin: plugin.text) renderer.register_string_renderer(Teaser, lambda plugin: plugin.text) renderer.register_string_renderer(FAQ, lambda plugin: plugin.text) renderer.register_string_renderer(File, lambda plugin: plugin.text) renderer.register_string_renderer(Command, "") class MyRegions(Regions): def handle_teasers(self, items, context): yield '<div class="teasers">' while True: yield self.renderer.render_plugin_in_context( items.popleft(), context) if not items or not matches(items[0], plugins=(Teaser, )): break
from feincms3.renderer import TemplatePluginRenderer from juso.blog import models as blog from juso.renderer import register_renderers renderer = TemplatePluginRenderer() register_renderers(renderer, blog)
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 feincms3.renderer import TemplatePluginRenderer from juso.events import models as events from juso.renderer import register_renderers, render_image renderer = TemplatePluginRenderer() register_renderers(renderer, events) location_renderer = TemplatePluginRenderer() location_renderer.register_string_renderer(events.LocationImage, render_image)
from django.shortcuts import get_object_or_404, redirect, render from feincms3.regions import Regions from feincms3.renderer import TemplatePluginRenderer from .models import Page, Snippet renderer = TemplatePluginRenderer() renderer.register_template_renderer( Snippet, lambda plugin: plugin.template_name, lambda plugin, context: {"additional": "context"}, ) 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, page.template.template_name, { "page": page, "regions": Regions.from_item(page, renderer=renderer,
from django.template.loader import render_to_string from feincms3 import plugins from feincms3.renderer import TemplatePluginRenderer from juso.forms import models renderer = TemplatePluginRenderer() renderer.register_string_renderer( models.RichText, lambda plugin: plugins.richtext.render_richtext(plugin)) def render_form_field(field): return render_to_string(f"forms/fields/{field.input_type}.html", { "field": field, }) renderer.register_string_renderer(models.FormField, lambda plugin: render_form_field(plugin))
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":