-
Notifications
You must be signed in to change notification settings - Fork 18
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
Conversation
There was a problem hiding this 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}) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
50e6184
to
87d100a
Compare
Fixed the linting issues 🤞🏻 |
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.
* 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.
This change does two things to permit custom rendering of footnote references in the
RichTextBlockWithFootnotes
block:replace_footnote_tags()
method to a separaterender_footnote_tag()
method on theRichTextBlockWithFootnotes
block class. This provides an easy extension point for subclasses ofRichTextBlockWithFootnotes
to customize rendering that doesn't require duplication of a lot of other code.render_footnote_tag
renders using a Django template, optionally set with theWAGTAIL_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 thereplace_footnote_tags()
with a single line f-string change. Instead, users will just have to provide a template.