Skip to content

Commit c7fdce0

Browse files
Merge pull request #1 from videoplaza/allow-include-dirs
Add -i --includes option extending the paths to search for jinja templates (mattrobenolt#75)
2 parents 7e59041 + 5f8ea85 commit c7fdce0

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ nosetests.xml
3535

3636
.pytest_cache
3737
dist/
38+
env/
39+
*.iml

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ Usage: jinja2 [options] <input template> <input data>
1717
Options:
1818
--version show program's version number and exit
1919
-h, --help show this help message and exit
20-
--format=FORMAT format of input variables: auto, ini, json,
20+
--format=FORMAT format of input variables: auto, env, ini, json,
2121
querystring, yaml, yml
2222
-e EXTENSIONS, --extension=EXTENSIONS
2323
extra jinja2 extensions to load
24+
-I INCLUDES, --includes=INCLUDES
25+
extra jinja2 template directory to search for
26+
(included) templates
2427
-D key=value Define template variable in the form of key=value
2528
-s SECTION, --section=SECTION
2629
Use only this section from the configuration
2730
--strict Disallow undefined variables to be used within the
2831
template
32+
-o FILE, --outfile=FILE
33+
File to use for output. Default is stdout.
2934
```
3035

3136
## Optional YAML support

jinja2cli/cli.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ def _parse_env(data):
212212
}
213213

214214

215-
def render(template_path, data, extensions, strict=False):
215+
def render(template_path, data, extensions, strict=False, includes=[]):
216216
from jinja2 import Environment, FileSystemLoader, StrictUndefined
217217

218218
env = Environment(
219-
loader=FileSystemLoader(os.path.dirname(template_path)),
219+
loader=FileSystemLoader([os.path.dirname(template_path)] + includes),
220220
extensions=extensions,
221221
keep_trailing_newline=True,
222222
)
@@ -311,7 +311,7 @@ def cli(opts, args):
311311

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

314-
out.write(render(template_path, data, extensions, opts.strict))
314+
out.write(render(template_path, data, extensions, opts.strict, includes=opts.includes))
315315
out.flush()
316316
return 0
317317

@@ -378,6 +378,13 @@ def main():
378378
action="append",
379379
default=["do", "with_", "autoescape", "loopcontrols"],
380380
)
381+
parser.add_option(
382+
"-I",
383+
"--includes",
384+
help="extra jinja2 template directory to search for (included) templates",
385+
dest="includes",
386+
action="append",
387+
)
381388
parser.add_option(
382389
"-D",
383390
help="Define template variable in the form of key=value",

0 commit comments

Comments
 (0)