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 syntax: {% include 'template' with {var: 2, var2: 'b'} %} #277

Closed
wants to merge 3 commits into from

Conversation

amyboyd
Copy link
Contributor

@amyboyd amyboyd commented Sep 5, 2014

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.

@mnpenner
Copy link

mnpenner commented Sep 7, 2014

Exactly what I was looking for. Does this include the only option from Twig?

@amyboyd
Copy link
Contributor Author

amyboyd commented Sep 7, 2014

This doesn't include the only option, but I can add that in 5 minutes
tomorrow morning. :)
On 7 Sep 2014 23:52, "Mark Bayazit" notifications@github.com wrote:

Exactly what I was looking for. Does this include the only option from
Twig http://twig.sensiolabs.org/doc/tags/include.html?


Reply to this email directly or view it on GitHub
#277 (comment).

@jbergstroem
Copy link
Contributor

Sounds very similar to using a macro if you don't need access to the environment.

@amyboyd
Copy link
Contributor Author

amyboyd commented Sep 9, 2014

@mnbayazit Added only keyword in f48c999

@mnpenner
Copy link

mnpenner commented Sep 9, 2014

@amyboyd Sweet! Thank you. Hopefully they merge this soon.

@jlongster
Copy link
Contributor

Yeah, what is the difference between this and just having a file be a macro that you import and call?

@amyboyd
Copy link
Contributor Author

amyboyd commented Sep 23, 2014

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.

@ivan-kleshnin
Copy link
Contributor

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.

@jlongster
Copy link
Contributor

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.

@jlongster jlongster closed this Oct 21, 2014
@jlongster
Copy link
Contributor

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.

@asabhaney
Copy link

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):

{% include 'comment.html' %}

An include with context (a la jinja):

{% include 'comment.html' with context %}

An include with custom context (proposed syntax):

{% set comment = { author: 'John Doe', body: 'Hello' } %}
{% include 'comment.html' with context comment %}

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.

@Starefossen
Copy link

Too bad this didn't get accepted 😞

@ghost
Copy link

ghost commented Sep 11, 2016

@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 set to reset it after include. That's really not nice code anymore.

Using embed is properly the best workaround, but the include with passing a context is still much easier for many use cases.

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. ;)

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.

7 participants