Skip to content

Commit 1801830

Browse files
committed
Add a long docstring to render_section_plugins
1 parent 1e92523 commit 1801830

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

feincms3/renderer.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,46 @@ def render_regions(self, *, regions, contents, context):
320320
# Sections support
321321

322322
def render_section_plugins(self, section, plugins, context):
323+
"""
324+
Helper for implementing section rendering with section supporting
325+
optional nesting etc.
326+
327+
You need:
328+
329+
- A close section plugin
330+
- One or more open section plugins
331+
332+
Here's some example code to hopefully get you started:
333+
334+
.. code-block:: python
335+
336+
from django.utils.html import mark_safe
337+
from feincms3.renderer import CLOSE_SECTION, render_in_context
338+
339+
class SectionRenderer(RegionRenderer):
340+
def handle_section(self, plugins, context):
341+
section = plugins.popleft()
342+
content = self.render_section_plugins(section, plugins, context)
343+
yield render_in_context(
344+
context,
345+
# That's just an example:
346+
f"sections/{section.__class__.__name__.lower()}.html",
347+
{"section": section, "content": "".join(content)},
348+
)
349+
350+
renderer = SectionRenderer()
351+
renderer.register(models.CloseSection, "", subregion=CLOSE_SECTION)
352+
renderer.register(models.Accordion, "", subregion="section")
353+
354+
This code automatically determines the template name from the class
355+
name, making it easier to reuse for different section types.
356+
357+
Subregions are automatically closed when subregions change. Sections
358+
only end when encountering an explicit ``CLOSE_SECTION`` subregion or
359+
when there are no more plugins in the current region at all. Sections
360+
can contain other sections and subregions making them quite powerful
361+
when for organizing and grouping content.
362+
"""
323363
out = []
324364
while plugins:
325365
subregion = self.subregion(plugins[0])

0 commit comments

Comments
 (0)