Adding a custom filter to my (Jinja2) templates

55 views
Skip to first unread message

Andrew Todd

unread,
Feb 8, 2014, 1:47:08 AM2/8/14
to nikola-...@googlegroups.com
Does anyone have any pointers or instructions for adding a custom filter [1] to my Jinja2 theme templates in Nikola?

I've done it in a Flask app but am not too sure how to go about it with a Nikola theme.

Thanks,
Andy

[1] http://jinja.pocoo.org/docs/api/#custom-filters

Chris “Kwpolska” Warrick

unread,
Feb 8, 2014, 5:30:12 AM2/8/14
to Nikola—Discuss
> --
> You received this message because you are subscribed to the Google Groups
> "nikola-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nikola-discus...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

I think the only way to do it would be very quirky, hacky and
generally unwanted: modify the Jinja2 plugin to include your filter.

However, this is a good point and it should be made easily
configurable: https://github.com/getnikola/nikola/issues/1038

I will handle this later today (or tomorrow).

--
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense

Andrew Todd

unread,
Feb 9, 2014, 1:45:26 AM2/9/14
to nikola-...@googlegroups.com

Thanks, I see there is a commit next to the github issue that you created. I'll pull this in the next day or so and give it a try.

Andy

Mitch Berkson

unread,
May 1, 2023, 4:11:14 PM5/1/23
to nikola-discuss
I was not successful in adding a custom Jinja2 filter. I defined my filter function foo in plugins/filters.py. In conf.py, I put:
from plugins.filters import foo

JINJA_FILTERS = {'foo': foo}

But when I build, foo is not found when rendering the template. Am I missing a step?

Chris Warrick

unread,
May 1, 2023, 4:16:11 PM5/1/23
to nikola-...@googlegroups.com
On Mon, 1 May 2023 at 22:11, Mitch Berkson <mitc...@gmail.com> wrote:
>
> I was not successful in adding a custom Jinja2 filter. I defined my filter function foo in plugins/filters.py. In conf.py, I put:
> from plugins.filters import foo
>
> JINJA_FILTERS = {'foo': foo}
>
> But when I build, foo is not found when rendering the template. Am I missing a step?

The variable got renamed to TEMPLATE_FILTERS.

--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
Reply all
Reply to author
Forward
0 new messages