简体   繁体   English

为预定义块 wagtail CRX 定义多个模板

[英]Define multiple templates for a predefined block wagtail CRX

I was moving a site over to wagtail and decided to use the codered extensions .我正在将网站移至wagtail 并决定使用编码扩展 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.我想使用它,但定义了一些您可以在管理 UI 中选择的模板。

You usually define a template in the meta section, but I noticed a dropdown in the admin UI for a template.您通常在元部分定义一个模板,但我注意到模板的管理 UI 中有一个下拉菜单。 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.我有兴趣添加一个 HTML 模板,而不是从内容块继承来改变行为。 (Unless inheriting is the only way to add a template to the dropdown.) (除非继承是将模板添加到下拉列表的唯一方法。)

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.您可以将要使用的模板的路径参数化,然后在您的块模板中使用include指向所选模板。

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 中,您可能有一个名为template的属性,它使用一个选择块,比如

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.好吧,这至少适用于 Wagtail,不确定编码。

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. Richard Allen 的答案适用于 wagtail,非常适合您自己的块等。Wagtail 块定义了一个单独的字段,用于其包含的组件,为此您需要另一种方法。

First you need to add the CRX_FRONTEND_TEMPLATE_BLOCKS to your django settings mysite/settings/base.py .首先,您需要将CRX_FRONTEND_TEMPLATE_BLOCKS添加到您的 django 设置mysite/settings/base.py中。

Then create a folder for your block templates in mysite/website/templates and create a custom template.然后在mysite/website/templates中为您的块模板创建一个文件夹并创建一个自定义模板。 Then add this path as a entry to the CRX_FRONTEND_TEMPLATE_BLOCKS.然后将此路径作为条目添加到 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/对于初学者,您可以从 codered package 复制一个模板/html 文件,该文件位于coderedcms/blocks/

Now the template should be available from the template dropdown under the advanced menu of a crx block.现在模板应该可以从 crx 块的高级菜单下的模板下拉列表中获得。

This info came from a gh issue of crx.此信息来自 crx 的gh 问题 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.所以这在未来可能会改变,这在 2023 年 1 月 26 日对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM