Skip to content

Commit

Permalink
Handle jinja2 renamed context decorators
Browse files Browse the repository at this point in the history
Jinja2 v3.0 renamed contextfilter to pass_context. Put some code in that
handles both the v3 and v2 name at the same time, as a first step
towards supporting jinja2 v3.
  • Loading branch information
mhagander committed Nov 12, 2024
1 parent aa7967d commit b982a1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 9 additions & 4 deletions postgresqleu/confreg/jinjafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

import jinja2
import jinja2.sandbox
try:
from jinja2 import pass_context
except ImportError:
# Try Jinja2 2.x version
from jinja2 import contextfilter as pass_context
import markdown


Expand Down Expand Up @@ -259,12 +264,12 @@ def _svgparagraph():
return '<text x="{}" y="{}">{}</text>'.format(x, y, "\n".join(_svgparagraph()))


@jinja2.contextfilter
@pass_context
def filter_applymacro(context, obj, macroname):
return context.resolve(macroname)(obj)


@jinja2.contextfilter
@pass_context
def filter_lookup(context, name, default=None):
if not name:
if default is not None:
Expand All @@ -285,7 +290,7 @@ def filter_lookup(context, name, default=None):

# Unpack a social media link for the specific social media being rendered for.
# This filter is *not* enabled by default.
@jinja2.contextfilter
@pass_context
def filter_social(context, attr):
if not context.get('messaging', None):
return None
Expand All @@ -296,7 +301,7 @@ def filter_social(context, attr):
# Get social media profiles including links from a structure.
# Returns a list of (provider, handle, link) for each configured
# social media identity.
@jinja2.contextfilter
@pass_context
def filter_social_links(context, attr):
if attr:
for k, v in attr.items():
Expand Down
7 changes: 6 additions & 1 deletion tools/deploystatic/deploystatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

import jinja2
import jinja2.sandbox
try:
from jinja2 import pass_context
except ImportError:
# Try Jinja2 2.x version
from jinja2 import contextfilter as pass_context

import markdown

Expand Down Expand Up @@ -75,7 +80,7 @@ def filter_slugify(value):

# Create social links. In the main site this is dynamic, this limited
# implementation supports just twitter and mastodon.
@jinja2.contextfilter
@pass_context
def filter_social_links(context, attr):
if attr:
for k, v in attr.items():
Expand Down

0 comments on commit b982a1d

Please sign in to comment.