Define multiple templates for a predefined block wagtail CRX

Question:

I was moving a site over to wagtail and decided to use the codered extensions. The library comes with a image-gallery content-block. I want to use this but define a few templates you can choose from in the admin UI.

You usually define a template in the meta section, but I noticed a dropdown in the admin UI for a template.
How do I add a template to that dropdown? Link to the content block I want to change

I am interested in adding an HTML template and not inheriting from the content-block to change behaviour. (Unless inheriting is the only way to add a template to the dropdown.)

Asked By: GreatGaja

||

Answers:

You could paramatise the path to the template you want to use then use an include in your block template to point to the chosen one.

For example, if you had a card block with selection for vertical or horizontal format. In your card block class you might have an property named template that uses a choice block, something like

class AlignmentChoiceBlock(ChoiceBlock):
    choices=[
        ('blocks/flex/vertical_card.html', 'Vertical'), 
        ('blocks/flex/horizontal_card.html', 'Horizontal') 
    ]

Then in your block template, it just consists of:

<div class="some-block-container">
    {% include value.template %}
</div>

Well, this works for Wagtail at least, not sure about codered.

Answered By: Richard Allen

The answer from Richard Allen works wagtail and is perfect for your own blocks etc. Wagtail blocks define a separate field that is used for their included components, for this you need another approach.

First you need to add the CRX_FRONTEND_TEMPLATE_BLOCKS to your django settings mysite/settings/base.py.

Then create a folder for your block templates in mysite/website/templates and create a custom template. Then add this path as a entry to the CRX_FRONTEND_TEMPLATE_BLOCKS. Entry key should be the block in lowercase. For a starter you could copy a template/html file from the codered package, found in coderedcms/blocks/

Now the template should be available from the template dropdown under the advanced menu of a crx block.

This info came from a gh issue of crx. This is e pretty recent addition and the dev mentioned that they are looking to make this easier. So this might change in the future, this worked for me on 26/01/2023.

Answered By: GreatGaja
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.