can I use include tag with context?
Include tag passes along context by default. Jinja supports include without context to prevent that; we should support that too, but currently we don't.
you mean mabe keywords withonly in swig, but I mean like this:
menu.partial.html
<nav>
<a class="{% if name == "home" %}active{% endif %}"></a>
<a class="{% if name == "profile" %}active{% endif %}"></a>
</nav>
In home.html:
{% include menu.partial.html with {name:"home"} %}
In profile.html:
{% include menu.partial.html with {name:"profile"} %}
Can I do this like that because I want change swig to nunjucks with no pains for other team...
No, nunjucks does not have that feature. IMO such use cases are better handled using macros in nunjucks.
Not sure if this is the same as my issue #539.
It may seem to me that "passing the context" kind of covers passing variables to an included template, and I see @whosesmile's example above, which is the same as my question.
@carljm: Sometimes it may work with a macro, while other times I'd argue it's cleaner to keep separate template files.
Example: I built a modular page builder recently, where each module was a Twig template. Within each template, I had an object with default options, which then was merged with any options being passed in.
It might be personal preference, but I found that having a folder structure with single module files instead of one massive macros file was simple and nice.
{% set defaultOptions = {
heading: '',
text: '',
} %}
{% set options = defaultOptions | merge(options) %}
<div class="section-block">
{% if options.heading | length %}
<h2 class="section__heading">{{ options.heading }}</h2>
<!-- /.section__heading -->
{% endif %}
{{ options.text }}
</div>
<!-- /.section-block -->
vs. macro with loads of these.
@sjelfull You're right that #539 is a clearer feature request for this feature -- this issue was an unclear request for documentation that may have been hijacked as a feature request. I'm going to close this ticket as unclear and re-open #539 to track the feature request.
Should this issue be re-opened? This feature was added in December 2014 in this PR: https://github.com/mozilla/nunjucks/pull/319 but it still appears to be undocumented.
Good point; I'll reopen and review the proposed documentation PR above.
Most helpful comment
Good point; I'll reopen and review the proposed documentation PR above.