UI 组件

本文档是 Wagtail 用户界面(UI)组件的参考,这些组件用于构建 CMS 内的各项功能。这些组件的演示可通过 Styleguide 应用和模式库查看。

关于由 JavaScript 驱动的组件和 Stimulus 控制器,请按控制器或类名搜索,并按照 JavaScript 组件 中的 JavaScript 参考阅读。对 ClipboardController、DialogController、DropdownController、PreviewController、RulesController 和 TooltipController 这类组件尤其有用。

备注

我们为这些组件提供文档,但不把它们纳入弃用政策所约定的同等稳定性保证。也就是说,某个组件的 API 可能在次要版本里直接变化,而不先经历弃用流程。如果你在自己的代码里用到了这些组件,请务必准备好测试流程,以便在每次升级 Wagtail 时发现破坏性变更。

Fragment

wagtail.admin.templatetags.wagtailadmin_tags.fragment(parser, token)

Store a template fragment as a variable.

Usage:

{% fragment as header_title %}
    {% blocktrans trimmed %}Welcome to the {{ site_name }} Wagtail CMS{% endblocktrans %}
{% endfragment %}
{% include "my/custom/header.html" with title=header_title %}

Adopted from slippers' fragment template tag with a few tweaks.

To strip leading and trailing whitespace produced in the fragment, use the stripped option. This is useful if you need to check if the resulting fragment is empty (after leading and trailing spaces are removed):

{% fragment stripped as recipient %}
    {{ title }} {{ first_name }} {{ last_name }}
{% endfragment %}
{% if recipient %}
    Recipient: {{ recipient }}
{% endif %}

Note that the stripped option only strips leading and trailing spaces, unlike {% blocktrans trimmed %} that also does line-by-line stripping. This is because the fragment may contain HTML tags that are sensitive to whitespace, such as <pre> and <code>.

Dialog

class wagtail.admin.templatetags.wagtailadmin_tags.DialogNode

用于在模态对话框中显示信息的对话框。由 DialogController(w-dialog)驱动。要创建对话框,可以使用 {% dialog %} 和 {% enddialog %} 模板标签。

{% dialog icon_name="globe" title="Dialog with critical error" id="my-dialog" subtitle="This is a testing subtitle" message_status="critical" message_heading="There was an issue with the thing" message_description="This is a subtext for the message" %}
    <p>This dialog message was generated by passing message_status=critical as well as message_heading and message_description to the dialog template tag</p>
{% enddialog %}

{% dialog %} 标签的必需参数:

{% dialog %} 标签的可选参数:

  • dialog_root_selector:对话框根元素的 CSS 选择器,默认为 body。当你希望把对话框渲染到 DOM 的特定位置(例如某个 <form> 元素内)时很有用。

  • icon_name:对话框头部要显示的图标名称。

  • subtitle:对话框的副标题。

  • theme:对话框的主题,取值为空或 floating。

  • classname:用于对话框样式的 CSS 类。

对话框的内容可以是在 {% dialog %} 与 {% enddialog %} 标签之间书写的任意 Django 模板代码。

对话框还可以在顶部显示状态消息,例如错误或警告。可以用以下可选参数渲染该消息:

  • message_status:消息的状态级别,可以是 info、warning、critical、success 之一。

  • message_heading:消息的标题。

  • message_description:消息的描述。

对话框开关

wagtail.admin.templatetags.wagtailadmin_tags.dialog_toggle()

一个用于打开指定 ID 对话框的按钮组件。要创建打开对话框的按钮,可以使用 {% dialog_toggle %} 模板标签。

{% dialog_toggle classname='button' dialog_id='my-dialog' text='Dialog with error' %}

{% dialog_toggle %} 标签的参数:

  • dialog_id:要打开的对话框的 ID。

  • text:按钮上显示的文本。

  • classname(可选):用于按钮样式的 CSS 类。

Tooltip

可附加到 HTML 元素上、在悬停或获得焦点时显示附加信息的工具提示。由 TooltipController(w-tooltip)驱动。要添加工具提示,请把 w-tooltip 控制器附加到元素上,并用 data 属性指定各项属性。

<button
    type="button"
    data-controller="w-tooltip"
    data-w-tooltip-content-value="More detail here"
    data-w-tooltip-offset-value="[10, 15]"
    data-w-tooltip-placement-value="top"
>
  A button with a tooltip
</button>

如果希望工具提示显示富内容,可以在 w-tooltip 元素内使用带 data-w-tooltip-target="content" 属性的 HTML 元素作为内容目标:

<button
    type="button"
    data-controller="w-tooltip"
    data-w-tooltip-offset-value="[10, 15]"
    data-w-tooltip-placement-value="top"
>
  <template data-w-tooltip-target="content">
    More <strong>detail</strong> here
  </template>
  A button with a tooltip
</button>

w-tooltip 可用的取值属性:

  • data-w-tooltip-content-value:工具提示的内容。如果改用内容目标,则该属性可选。

  • data-w-tooltip-offset-value(可选):工具提示相对元素的偏移,以两个数字组成的数组表示([skidding, distance])。默认为 [0, 10]。

  • data-w-tooltip-placement-value(可选):工具提示相对元素的位置。可选值为 top、top-start、top-end、right、right-start、right-end、bottom、bottom-start、bottom-end、left、left-start、left-end。默认为 bottom。