Associate RichTextBlock text content to a snippet model.
Project description
wagtail-draftail-snippet
Wagtail has support for adding numerous types of links to RichText
content, but there is not a way to tie a link to an arbitrary snippet
model currently. wagtail-draftail-snippet
provides a way to create either an a href
element for a specific snippet
model based on a template that can be provided, or a completely free-form piece of HTML for a snippet.
The project provides the following draftail (Wagtail Richtext editor) features:
snippet-link
: Allows to create a link using the snippet chooser modal. The link is rendered using the template{app_name}/{model_name}_snippet_link.html
.snippet-embed
: Allows to embed a block using the snippet chooser modal. The block is embedded using the template{app_name}/{model_name}_snippet_embed.html
.
Unsure whether you want to use a snippet link or embed? Embeds have complete flexibility, but 1) break the flow of text in the Draftail richtext editor and 2) can't be used to link arbitrary text. If you want to render a link to a snippet model inside of body copy, then you probably want to use the snippet link. For anything else, the embed can be used.
Install
pip install wagtail-draftail-snippet
- Add
wagtail_draftail_snippet
toINSTALLED_APPS
in Django settings - Add
"snippet-link"
and"snippet-embed"
to thefeatures
keyword list argument when instantiating aRichTextBlock
, e.g.paragraph = RichTextBlock(features=["bold", "italic", "h1", "h2", "h3", "snippet-link", "snippet-embed"])
- Create a frontend template to determine how the snippet model will be rendered. Frontend templates are required for a snippet to be selected and are discovered when they match a path like
{app_name}/{model_name}_snippet_link.html
and{app_name}/{model_name}_snippet_embed.html
. For example, if you have anAffiliate
snippet model inaffiliates/models.py
, then a file inaffiliates/templates/affiliates/affiliate_snippet_link.html
andaffiliates/templates/affiliates/affiliate_snippet_embed.html
would be required.
Example use-case
Wagtail is used for a content site that will display articles that have affiliate links embedded inside the content. Affiliate links have a snippet data model to store information with a URL, start, and end dates; the urls need to be rendered in such a way that JavaScript can attach an event listener to their clicks for analytics.
When the content gets rendered, it uses the specific affiliate model to get the URL stored in the snippet model. If the affiliate's URL ever changes, the snippet can be changed in the Wagtail admin, and the all of the content will use the correct link when rendered.
An example frontend template in affiliates/templates/affiliates/affiliate_snippet_link.html
could be the following.
<a href="{{ object.url }}" data-vars-action="content-cta" data-vars-label="{{ object.slug }}" rel="sponsored">
Requirements
The package requires Wagtail 2.5 or above.
Build the library
poetry build
Run tests
poetry install
poetry run pytest
Contributors
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for wagtail-draftail-snippet-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 392f3a142dfdbc7d574cd290a7f913c014181b4058b3418ba95ed5416c4f73ab |
|
MD5 | 4a64084a639031c1ffd7a8c7e2fadd5a |
|
BLAKE2b-256 | ac16c5af8db87442cce8e3eab07e89b3c6d434e27b93677f9527a42c7b2f0e9f |