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 the ability to import filters #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ language: python
dist: xenial
python:
- '2.7'
- '3.4'
- '3.5'
- '3.6'
- '3.7'
- '3.8'

install: pip install -e .[yaml,toml,tests,xml]

Expand Down
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
4 changes: 2 additions & 2 deletions tests/test_jinja2cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_relative_path():
path = "./files/template.j2"

title = b"\xc3\xb8".decode("utf8")
output = cli.render(path, {"title": title}, [])
output = cli.render(path, {"title": title}, [], [])
assert output == title
assert type(output) == cli.text_type

Expand All @@ -20,6 +20,6 @@ def test_absolute_path():
path = os.path.join(absolute_base_path, "files", "template.j2")

title = b"\xc3\xb8".decode("utf8")
output = cli.render(path, {"title": title}, [])
output = cli.render(path, {"title": title}, [], [])
assert output == title
assert type(output) == cli.text_type