Skip to content

Commit

Permalink
Add the ability to import filters
Browse files Browse the repository at this point in the history
Fixes #85
  • Loading branch information
mattrobenolt committed Apr 24, 2020
1 parent de5e8bf commit b0e13cd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _parse_env(data):
}


def render(template_path, data, extensions, strict=False):
def render(template_path, data, extensions, filters, strict=False):
from jinja2 import Environment, FileSystemLoader, StrictUndefined

env = Environment(
Expand All @@ -227,6 +227,13 @@ def render(template_path, data, extensions, strict=False):
env.globals["environ"] = lambda key: force_text(os.environ.get(key))
env.globals["get_context"] = lambda: data

if filters:
from jinja2.utils import import_string

for filter in set(filters):
filter = import_string(filter)
env.filters[filter.__name__] = filter

return env.get_template(os.path.basename(template_path)).render(data)


Expand Down Expand Up @@ -312,7 +319,7 @@ def cli(opts, args):

out = codecs.getwriter("utf8")(out)

out.write(render(template_path, data, extensions, opts.strict))
out.write(render(template_path, data, extensions, opts.filters, opts.strict))
out.flush()
return 0

Expand Down Expand Up @@ -379,6 +386,14 @@ def main():
action="append",
default=["do", "with_", "autoescape", "loopcontrols"],
)
parser.add_option(
"-f",
"--filter",
help="extra jinja2 filters to load",
dest="filters",
action="append",
default=[],
)
parser.add_option(
"-D",
help="Define template variable in the form of key=value",
Expand Down

0 comments on commit b0e13cd

Please sign in to comment.