Content Block Templates#

ContentBlockTemplate#

You can create ContentBlockTemplate objects in the admin site.

class ContentBlockTemplate#
name#

Unique name to identify the content block template with.

template_filename#

The file name of a template in the content_blocks/content_blocks template directory. You would usually create this directory in your project’s template directory as defined in your project settings. This template is used to render content blocks made with this ContentBlockTemplate.

If this is not provided content blocks made from this template cannot be rendered. This may be suitable for nested content blocks.

ContentBlockTemplateField#

ContentBlockTemplateField objects are created inline in the ContentBlockTemplate admin change page. You can drag and drop to reorder the fields in the content block editor.

Most types of ContentBlockTemplateField have the options shown here when adding them via the admin site. Some types have additional options detailed below in the relevant ContentBlockField section.

class ContentBlockTemplateField#
field_type#

Choose the field type. All types are detailed below. Can’t be changed once saved.

key#

The key is used when rendering template and is the key for the context provided by this field.

help_text#

Show some help text for this field in the content block editor. Help text is shown as information tooltips the user can hover over to read.

required#

If checked this field cannot be left blank in the content block editor.

css_class#

The css class provided is added to the field wrapper in the content block editor. You can use this alongside some js to provide additional functionality for fields in the content block editor such as wysiwyg editors.

ContentBlockField#

ContentBlockTemplateField objects create ContentBlockField objects. The details of each type is given here as well as example usage in templates.

TextField#

Intended use is for single line text content.

class TextField#
property form_field#
Return type:

django.forms.CharField

The field used in the content block editor.

property context_value#
Return type:

str

The context value in ContentBlock.context. To be used in templates.

Template Usage Example (key = "text")#
<h2>{{ content_block.text }}</h2>

ContentField#

Intended use is for multiline text content.

class ContentField#
property form_field#
Return type:

django.forms.CharField

Widget:

django.forms.TextArea

property context_value#
Return type:

str

Template Usage Example (key = "content")#
{{ content_block.content|linebreaks }}

ImageField#

A preview of the image is shown in the content block editor.

class ImageField#
property form_field#
Return type:

content_blocks.fields.SVGAndImageFieldFormField a subclass of django.forms.ImageField which also accepts svg files.

property context_value#
Return type:

django.db.models.fields.files.ImageFieldFile

Template Usage Example (key = "image")#
<img src="{{ content_block.image.url }}" />

VideoField#

A preview of the video is shown in the content block editor.

class VideoField#
property form_field#
Return type:

forms.FileField

property context_value#
Return type:

content_blocks.fields.FieldVideo a subclass of django.db.models.fields.files.FieldFile which provides the file_extension property.

Template Usage Example (key = "video")#
<video>
    <source src="{{ content_block.video.url }}"
     type="video/{{ content_block.video.file_extension }}">
</video>

FileField#

Intended for all files except image and video.

class FileField#
property form_field#
Return type:

forms.FileField

property context_value#
Return type:

django.db.models.fields.files.FieldFile

Template Usage Example (key = "file")#
<a href="{{ content_block.file.url }}">Download</a>

EmbeddedVideoField#

A preview of the embedded video is shown in the content block editor. Supports YouTube, Vimeo and possibly others.

class EmbeddedVideoField#
property form_field#
Return type:

django.forms.CharField

property context_value#
Return type:

str

Template Usage Example (key = "embedded_video")#
<iframe src="{{ content_block.embedded_video }}" frameborder="0"
 allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
 webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

NestedField#

Can be used to make things such as image galleries and menus. They function in a similar way to Django admin inlines but as the name implies they can be nested.

When adding a NestedField you choose which ContentBlockTemplate objects can be used to create nested content blocks. You can reuse existing ContentBlockTemplate objects or create new ones just for use with this NestedField. If you create new ones then you might want to hide them from use as top level content blocks by creating a ContentBlockAvailability.

class NestedField#
property form_field#
Returns:

None. Nested fields do not use a form field. They use a form which is appended to the content block form.

property context_value#
Returns:

A QuerySet of nested ContentBlock objects.

The following additional options are available for ContentBlockTemplate:

  • nested_templates Choose which ContentBlockTemplate can be used to create nested content blocks.

  • min_num The minimum number of nested content blocks that can be created. The editor will prevent nested content blocks from being deleted if there are min_num or fewer. When adding a new content block this number of nested content blocks will be created, the first chosen nested_templates is used to create these initial nested content blocks.

  • max_num The maximum number of nested content blocks that can be created. The editor will prevent nested content blocks from being created if there are max_num or more.

Template Usage Example (key = "nested_content_blocks")#
{# If the nested content block template has a template_filename #}
{# you can use the render_content_block template tag. #}

{% load content_blocks %}

{% for nested_content_block in content_block.nested_content_blocks %}
    {% render_content_block nested_content_block %}
{% endfor %}

{# Or you can reference the context of the nested content block. #}
{# In this example our nested content block has a field with a key of "text" #}

{% for nested_content_block in content_block.nested_content_blocks %}
    <h3>{{ nested_content_block.context.text }}</h3>
{% endfor %}

ModelChoiceField#

ModelChoiceField let’s us reference objects from other models in your project via the Django contenttypes framework. When created in the admin site we choose the model_choice_content_type. When used in the content block editor the choices are model_choice.content_type.objects.all().

class ModelChoiceField#
property form_field#
Return type:

forms.ModelChoiceField

property context_value#
Returns:

The chosen object of the type given by model_choice_content_type.

The following additional options are available for ContentBlockTemplate:

  • model_choice_content_type Choose the ContentType for the available choices.

Template Usage Example (key = "model_choice")#
{# Here our related object has an attribute "name". #}

<h2>{{ content_block.model_choice.name }}</h2>

{# We can make related objects blocks when used in a NestedField. #}

{% for nested_content_block in content_block.nested_content_blocks %}
    <h3>{{ nested_content_block.model_choice.name }}</h3>
{% endfor %}

{# When used with template tags we can make awesome things happen. #}
{# Here we have a template tag that takes our object as an argument. #}

{% load awesome_tags %}
{% do_something_awesome content_block.model_choice %}

ChoiceField#

We can set a list of choices to choose from in the content block editor. Can be useful for providing style options via css classes.

class ChoiceField#
property form_field#
Return type:

forms.CharField

property context_value#
Return type:

str

The following additional options are available for ContentBlockTemplate:

  • choices Set the available choices.

Template Usage Example (key = "choice")#
<h2 class="{{ content_block.choice }}">Stylish by choice!</h2>

CheckboxField#

We can also add a checkbox for providing further customisation options.

class CheckboxField#
property form_field#
Return type:

forms.BooleanField

property context_value#
Return type:

bool

Template Usage Example (key = "checkbox")#
{% if content_block.checkbox %}
    <h3>Something</h3>
{% else %}
    <h2>Something else!</h2>
{% endif %}