Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get wagtail object to render django template variables #1187

Open
xmedr opened this issue Dec 11, 2024 · 0 comments
Open

Get wagtail object to render django template variables #1187

xmedr opened this issue Dec 11, 2024 · 0 comments

Comments

@xmedr
Copy link
Collaborator

xmedr commented Dec 11, 2024

When working on #1177, we made a wagtail-editable model EventNotice that contains a set of messages that would appear on every event detail page. We also wanted to have the message include details specific to the event being rendered. To that end, we started registering a new rich text editor feature within the hooks:

@hooks.register("register_rich_text_features")
def register_ecomment_feature(features):
    """Register the ecomment link type"""
    feature_name = "ecomment_link"
    type_ = "ecomment_link"

    control = {
        "type": type_,
        "label": "E",
        "description": "Ecomment Link",
        "element": "a",
    }

    features.register_editor_plugin(
        "draftail", feature_name, draftail_features.BlockFeature(control)
    )

    from_format_str = (
        "a[href={{event.ecomment_url}}][target=_blank]"
        + "[aria-label='Go to public comment - link opens in a new tab']"
    )
    features.register_converter_rule(
        "contentstate",
        feature_name,
        {
            "from_database_format": {from_format_str: BlockElementHandler(type_)},
            "to_database_format": {
                "block_map": {
                    type_: {
                        "element": "a",
                        "props": {
                            "href": "{{event.ecomment_url}}",
                            "target": "_blank",
                            "aria-label": "Go to public comment - link opens in a new tab",
                        },
                    }
                }
            },
        },
    )

Unfortunately, simply saving the django template variable to the model does not allow it to render correctly when taken back out. Let's do some further research on this. Maybe there's a way we can have from_database_format render a template instead of a straight up string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New issues
Development

No branches or pull requests

1 participant