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

Add support for custom filters #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix for python3
  • Loading branch information
WolfgangBeiDerBohne committed Feb 11, 2021
commit 9762ea16c42ede94a90d262e8bf3a3cdd042c1bd
13 changes: 4 additions & 9 deletions j2render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# encoding: utf-8
""" Render a Jinja2 template. """

from __future__ import print_function, unicode_literals

import argparse
import logging
import importlib
Expand Down Expand Up @@ -130,7 +128,7 @@ def make_parser():

def main(args=None):
args = make_parser().parse_args(args)

template_str = args.template.read()
setup_logging(quiet=args.quiet, verbose=args.verbose)
logger.debug("args: {0}".format(repr(args)))

Expand Down Expand Up @@ -169,16 +167,13 @@ def main(args=None):
for filename in args.filters:
path = filename.split('/')
if '.py' in path[-1][-3:]:
modpath=path[:-1]
modpath = path[:-1]
modpath.append(path[-1][:-3])
importpath= '.'.join(modpath)
importpath = '.'.join(modpath)
customfilter = importlib.import_module(importpath)
for fname, ffunct in inspect.getmembers(customfilter, inspect.isfunction):
environment.filters[fname] = ffunct
try:
template_str = args.template.read().decode(encoding)
except AttributeError:
template_str = args.template.read()

template = environment.from_string(template_str)
print(template.render(**context))

Expand Down