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

Conversation

georgespalding
Copy link

@georgespalding georgespalding commented Mar 25, 2019

This is useful for reuse when a set of shared templates are maintained in another directory
by using {% include 'lib/header.j2' %} style includes.

Closes #75

…arch for jinja templates

This is useful for reuse when a set of shared templates are maintained in another directory
by  using {% include 'lib/header.j2' %} style includes.
@georgespalding georgespalding changed the title #75 Add -i/--includes option extending template search path Add -i/--includes option extending template search path Mar 25, 2019
@mattrobenolt
Copy link
Owner

Hey, this is great! Lemme take a closer look at this.

@mvgijssel
Copy link

I also have a use for this! Any reason why this is not getting merged? 🤔

@georgespalding
Copy link
Author

TBH I forgot about this. Should I merge it?

jinja2cli/cli.py Outdated
@@ -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

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

See also mattrobenolt#76 (review)
> 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
> ```
@georgespalding
Copy link
Author

Reopen after implementing feedback from @mykhailo-inv-disco

…ludes flag; some autopeping from tools; add .python-version (pyenv) to .gitignore
@IrSent
Copy link

IrSent commented Sep 11, 2021

@georgespalding Please take a look at this PR: videoplaza#3 . I've added some tests with and without --includes flag. I've got too many templates 🤕 )

fix(tests): add tests that call jinja2-cli cmd with and without --inc…
@georgespalding
Copy link
Author

@mattrobenolt Anything else you'd like fixed before merging?

@IrSent
Copy link

IrSent commented Sep 23, 2021

btw, if you need --includes flag set to templates root but cannot wait for the new version of the jinja2-cli package to be released, as a temporary workaround, you can copy a template into the template root just before rendering it (also don't forget to remove it afterwards), and now you can have predictable imports inside templates.

@boxxxie
Copy link

boxxxie commented Apr 17, 2023

thanks for the workaround.

@thrix
Copy link

thrix commented Apr 29, 2024

Would be really great to get this merged :( This version is packaged in most of the distros ....

@mcrozes
Copy link

mcrozes commented Apr 30, 2024

+1, I just ran into this issue and currently exploring available options.

@mattrobenolt , I noticed the latest release was done over 2 years ago. Is there anything we can do here?

@mattrobenolt
Copy link
Owner

@mcrozes I'm working on a v1 to tag and clean up the codebase. I hope to get that out in the next few weeks, and I'll get this pulled in. I wanted to solve a few other outstanding issues and drop python2 support as well.

This all needs a bit of a modern refresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to specify multiple include directories, not only the one containing the template
8 participants