Spec-Zone.ru › Wagtail

Изменение представления форматированного текста

HTML-представление изображения в форматированном тексте может быть настраиваемо — например, для отображения подписей или пользовательских полей.

Для этого необходимо наследоваться от Format (см. Форматы изображений в редакторе форматированного текста), и переопределить его image_to_html метод.

Затем вы можете зарегистрировать форматы вашего подкласса с помощью register_image_format как обычно.

# image_formats.py
from wagtail.images.formats import Format, register_image_format


class SubclassedImageFormat(Format):

    def image_to_html(self, image, alt_text, extra_attributes=None):

        custom_html = # the custom HTML representation of your image here
                        # in Format, the image's rendition.img_tag(extra_attributes) is used to generate the HTML
                        # representation

        return custom_html


register_image_format(
    SubclassedImageFormat('subclassed_format', 'Subclassed Format', classnames, filter_spec)
)

Например, предположим, что вы хотите, чтобы текст alt отображался как подпись к изображению:

# image_formats.py
from django.utils.html import format_html
from wagtail.images.formats import Format, register_image_format


class CaptionedImageFormat(Format):

    def image_to_html(self, image, alt_text, extra_attributes=None):

        default_html = super().image_to_html(image, alt_text, extra_attributes)

        return format_html("{}<figcaption>{}</figcaption>", default_html, alt_text)


register_image_format(
    CaptionedImageFormat('captioned_fullwidth', 'Full width captioned', 'bodytext-image', 'width-750')
)

Примечание

Любые настраиваемые HTML-функции изображений не будут отображаться в редакторе Draftail, только на опубликованной странице.

© 2014-present Torchbox Ltd and individual contributors.
All rights are reserved.
Licensed under the BSD License.
https://docs.wagtail.org/en/stable/advanced_topics/images/changing_rich_text_representation.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API