-
Notifications
You must be signed in to change notification settings - Fork 641
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 syntax: {% include 'template' with {var: 2, var2: 'b'} %} #277
Conversation
00c43d8
to
9b5f03e
Compare
Exactly what I was looking for. Does this include the |
This doesn't include the
|
Sounds very similar to using a macro if you don't need access to the environment. |
@mnbayazit Added |
@amyboyd Sweet! Thank you. Hopefully they merge this soon. |
Yeah, what is the difference between this and just having a file be a macro that you import and call? |
I think the syntax is a bit shorter, and it lets you control whether or not the included template has access to the parent scope (a macro doesn't let you decide). To be honest, the main reason for doing this was to make my fork more like Twig because I've been converting a large number of Twig files from Twig/PHP to Nunjucks/Node. If you want to stay in line with what Jinja has, I'm happy to just keep this in my fork. |
Sticking to JINJA syntax you can rely on Jinja syntax highlighting / autocompletion in IDEA (and other IDEs where you can apply custom parser to folders). I think that is very big benefit. |
You can control what the imported macros have control to see in jinja, we just don't support it yet :http://jinja.pocoo.org/docs/dev/templates/#import-context-behavior I would rather go down that path since we do claim to follow jinja, and not twig. Sorry. The behavior is just too close. |
I would be interested if you could make this into an extension (you would have to name it something other than include). The idea for extensions was to let you customize behavior like this, and it's a great use case. If you can do that I will feature it in the docs. |
I started writing this as a custom extension, but it seemed a little redundant since this behaviour is an include, just with an additional option. That being said, I understand what you're saying about wanting to follow jinja. Perhaps I can offer a suggestion that offers this functionality in a way that does not go against jinja convention/syntax: A normal include (as is):
An include with context (a la jinja):
An include with custom context (proposed syntax):
This syntax has the added benefit of reading nicely. If no custom context is specified, the global context is implied. Otherwise a custom context can explicitly be specified. If you're open to this, I would be happy to take this on. I look forward to hearing your feedback. |
Too bad this didn't get accepted 😞 |
@jlongster I think most are aware the solution you show above is a possible workaround. But it does add another global variable and I have to use a second Using If we add the with parameter, nunjucks is still compatible with jinia, so why not this useful feature in core? Keep in mind Jinja started to implement the Template System from Django and was adding some new features too. ;) |
This is useful for re-using bits of HTML that have small changes, for example a search box used on multiple pages with different placeholder text:
{% include 'search-box.html.twig' with {placeholder: 'Search users'} %}
{% include 'search-box.html.twig' with {placeholder: 'Search companies'} %}
This isn't a feature of jinja, but is a feature of the similar Twig framework.