2 | {% if flatblock.header %}
3 |
{{ flatblock.header }}
4 | {% endif %}
5 |
{{ flatblock.content|safe }}
6 |
7 |
--------------------------------------------------------------------------------
/flatblocks/templatetags/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jazzband/django-flatblocks/8ecea624a3e38364b773eecb386ea7a92b103249/flatblocks/templatetags/__init__.py
--------------------------------------------------------------------------------
/flatblocks/templatetags/flatblocks.py:
--------------------------------------------------------------------------------
1 | """
2 | This module offers one templatetag called "flatblock" which allows you to
3 | easily embed small text-snippets (like for example the help section of a page)
4 | into a template.
5 |
6 | It requires one argument, and accepts several extra keywords:
7 |
8 | {% flatblock {slug} [evaluated={bool}] [using={template}] %}
9 |
10 | slug::
11 | The slug/key of the text (for example 'contact_help').
12 |
13 | evaluated::
14 | If set to True, the content and header of the FlatBlock will be
15 | rendered as a Template before being passed to the template.
16 |
17 | This allows you to embed template tags into your FlatBlocks.
18 |
19 | The original values for the content and header will be saved to
20 | raw_content and raw_header respectively.
21 |
22 | using::
23 | The template to render the FlatBlock with. If not supplied, will
24 | default to "flatblocks/flatblock.html".
25 |
26 | If set to False, will not be used, and the output of the tag will be
27 | the ``content`` property of the FlatBlock.
28 |
29 | Example::
30 |
31 | {% load flatblock_tags %}
32 |
33 | ...
34 |
35 | {% flatblock 'contact_help' %}
36 | {% flatblock name_in_variable %}
37 |
38 | The 'flatblock' template tag acts like an inclusiontag and operates on the
39 | ``flatblock/flatblock.html`` template file, which gets (besides the global
40 | context) also the ``flatblock`` variable passed.
41 |
42 | Compared to the original implementation this includes not only the block's
43 | content but the whole object inclusing title, slug and id. This way you
44 | can easily for example offer administrative operations (like editing)
45 | within that template.
46 |
47 | """
48 | from __future__ import absolute_import
49 |
50 | import logging
51 |
52 | from django import VERSION, template
53 | from django.template.loader import render_to_string
54 | from flatblocks import settings
55 |
56 | if VERSION >= (1, 7):
57 | from django.apps import apps
58 | get_model = apps.get_model
59 | else:
60 | from django.db import models
61 | get_model = models.get_model
62 |
63 |
64 | register = template.Library()
65 | logger = logging.getLogger(__name__)
66 |
67 | FlatBlock = get_model('flatblocks', 'flatblock')
68 |
69 |
70 | @register.simple_tag(takes_context=True)
71 | def flatblock(context, slug, evaluated=False,
72 | using='flatblocks/flatblock.html'):
73 |
74 | if not settings.AUTOCREATE_STATIC_BLOCKS:
75 | try:
76 | flatblock = FlatBlock.objects.get(slug=slug)
77 | except FlatBlock.DoesNotExist:
78 | return ''
79 | else:
80 | flatblock, _ = FlatBlock.objects.get_or_create(slug=slug,
81 | defaults={'content': slug}
82 | )
83 |
84 | if evaluated:
85 | flatblock.raw_content = flatblock.content
86 | flatblock.raw_header = flatblock.header
87 | flatblock.content = template.Template(flatblock.content).render(context)
88 | flatblock.header = template.Template(flatblock.header).render(context)
89 |
90 | if using:
91 | ctx = context.flatten()
92 | ctx['flatblock'] = flatblock
93 | result = render_to_string(using, ctx)
94 | else:
95 | result = flatblock.content
96 |
97 | return result
98 |
99 |
100 | @register.simple_tag(takes_context=True)
101 | def plain_flatblock(context, slug, evaluated=False):
102 | return flatblock(context, slug, evaluated=evaluated, using=None)
103 |
--------------------------------------------------------------------------------
/flatblocks/urls.py:
--------------------------------------------------------------------------------
1 | from django.contrib.admin.views.decorators import staff_member_required
2 | from django.urls import re_path
3 | from flatblocks.views import edit
4 |
5 | urlpatterns = [
6 | re_path(
7 | r"^edit/(?P