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 -i/--includes option extending template search path #76

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ nosetests.xml

.pytest_cache
dist/
env/
*.iml
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ Usage: jinja2 [options] <input template> <input data>
Options:
--version show program's version number and exit
-h, --help show this help message and exit
--format=FORMAT format of input variables: auto, ini, json,
--format=FORMAT format of input variables: auto, env, ini, json,
querystring, yaml, yml
-e EXTENSIONS, --extension=EXTENSIONS
extra jinja2 extensions to load
-I INCLUDES, --includes=INCLUDES
extra jinja2 template directory to search for
(included) templates
-D key=value Define template variable in the form of key=value
-s SECTION, --section=SECTION
Use only this section from the configuration
--strict Disallow undefined variables to be used within the
template
-o FILE, --outfile=FILE
File to use for output. Default is stdout.
```

## Optional YAML support
Expand Down
14 changes: 11 additions & 3 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ def _parse_env(data):
}


def render(template_path, data, extensions, strict=False):
def render(template_path, data, extensions, strict=False, includes=[]):
Copy link

@mykhailo-inv-disco mykhailo-inv-disco Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad idea to pass empty list as a default argument
explanation here: https://web.archive.org/web/20200221224620/http://effbot.org/zone/default-values.htm
should be something like that:

def render(template_path, data, extensions, strict=False, includes=None):
    includes = [] if includes is None else includes

from jinja2 import Environment, FileSystemLoader, StrictUndefined

env = Environment(
loader=FileSystemLoader(os.path.dirname(template_path)),
loader=FileSystemLoader([os.path.dirname(template_path)] + includes),
extensions=extensions,
keep_trailing_newline=True,
)
Expand Down Expand Up @@ -311,7 +311,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.strict, includes=opts.includes))
out.flush()
return 0

Expand Down Expand Up @@ -378,6 +378,14 @@ def main():
action="append",
default=["do", "with_", "autoescape", "loopcontrols"],
)
parser.add_option(
"-I",
"--includes",
help="extra jinja2 template directory to search for (included) templates",
dest="includes",
action="append",
default=[],
)
parser.add_option(
"-D",
help="Define template variable in the form of key=value",
Expand Down