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

Allow custom footnote reference rendering #70

Merged
merged 3 commits into from
Aug 13, 2024

Conversation

willbarton
Copy link
Contributor

This change does two things to permit custom rendering of footnote references in the RichTextBlockWithFootnotes block:

  1. Moves reference rendering from an inner function in the replace_footnote_tags() method to a separate render_footnote_tag() method on the RichTextBlockWithFootnotes block class. This provides an easy extension point for subclasses of RichTextBlockWithFootnotes to customize rendering that doesn't require duplication of a lot of other code.
  2. The default implementation of render_footnote_tag renders using a Django template, optionally set with the WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE setting, allowing users to override rendering by providing a different template to that setting.

This is similar in spirit to @an-ant0ni0's proposal in #27, however we've chosen to move rendering out of Python f-strings altogether and into Django (or any other configured engine's) templates.

The motivation here, is we're adopting this package to replace our hand-crafted (😱) footnotes, and our existing style doesn't include [] brackets around the reference number.

This will allow that kind of customization of reference rendering so users can render however their style specifies without having to subclass RichTextBlockWithFootnotes and include the full code of the replace_footnote_tags() with a single line f-string change. Instead, users will just have to provide a template.

Copy link
Member

@zerolab zerolab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

"wagtail_footnotes/includes/footnote_reference.html"
)
template = get_template(template_name)
return template.render({"index": index})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the template approach much better than f-strings.

At first I was in two minds about adding a setting over than just the usual pattern of overriding templates from other apps by adding the correct path in your project's templates.
In this case, adding wagtail_footnotes/includes/footnote_reference.html to one's app's templates folder, or a top-level templates folder should override it.

On the other hand, Django, Wagtail and some packages do provide a setting for specific templates too (like the password required template in Wagtail), so this makes it more expressive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I thought about that too. There's nothing with this approach that prevents just overriding wagtail_footnotes/includes/footnote_reference.html. Do you think there be a note about that option in the README?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think we should guide people towards the setting as the official way of doing it

This change does two things to permit custom rendering of footnote references in the `RichTextBlockWithFootnotes` block:

1. Moves reference rendering from an inner function in the `replace_footnote_tags()` method to a separate `render_footnote_tag()` method on the `RichTextBlockWithFootnotes` block class. This provides an easy extension point for subclasses of `RichTextBlockWithFootnotes` to customize rendering that doesn't require duplication of a lot of other code.
2. The default implementation of `render_footnote_tag` renders using a Django template, optionally set with the `WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE` setting, allowing users to override rendering by providing a different template to that setting.

This is similar in spirit to @an-ant0ni0's proposal in torchbox#27, however we've chosen to move rendering out of Python f-strings altogether and into Django (or any other configured engine's) templates.
@willbarton
Copy link
Contributor Author

Fixed the linting issues 🤞🏻

.github/workflows/ruff.yml Outdated Show resolved Hide resolved
@zerolab zerolab merged commit 15b55aa into torchbox:main Aug 13, 2024
8 checks passed
willbarton added a commit to cfpb/consumerfinance.gov that referenced this pull request Aug 14, 2024
This updates wagtail-footnotes to 0.12 and removes our custom `RichTextBlockWithFootnotes` subclass of wagtail-footnotes own `RichTextBlockWithFootnotes`, tests, and switches to using the upstream `RichTextBlockWithFootnotes`. 0.12 includes our changes in torchbox/wagtail-footnotes#70, which were based on this code being removed.

This change updates `RichTextBlockWithFootnotes` in-place in old migrations. Removing `v1.blocks.RichTextBlockWithFootnotes` requires these migrations to be updated, and because there are no schema changes, new migrations are not required.
nickmoreton pushed a commit to torchbox-forks/wagtail-footnotes that referenced this pull request Aug 20, 2024
* Allow custom footnote reference rendering

This change does two things to permit custom rendering of footnote references in the `RichTextBlockWithFootnotes` block:

1. Moves reference rendering from an inner function in the `replace_footnote_tags()` method to a separate `render_footnote_tag()` method on the `RichTextBlockWithFootnotes` block class. This provides an easy extension point for subclasses of `RichTextBlockWithFootnotes` to customize rendering that doesn't require duplication of a lot of other code.
2. The default implementation of `render_footnote_tag` renders using a Django template, optionally set with the `WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE` setting, allowing users to override rendering by providing a different template to that setting.

This is similar in spirit to @an-ant0ni0's proposal in torchbox#27, however we've chosen to move rendering out of Python f-strings altogether and into Django (or any other configured engine's) templates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants