I have a top-level template where I want to use a "fragment" template inside a for cycle but I'm not able to pass in the variable value:
{% for item in coll %}
{% include "fragment.html" with name="slack" item=item %}
{% endfor %}
item and name is then used in the fragment.html template:
<div>
<label>
<input
title="{{item.id}}"
id="{{name}_{{item.id}}_active"
name="{{name}}-{{item.id}}_active"
...
/>
While the name parameter is expanded properly (its value is hardcoded in the parent template), the item parameter is not (its value is passed in as is).
Do I need to use a different syntax for that or it's just not supported?
The
includetag splices in the included template. This means that any variables within scope of the parent template will be available to the included template. Thewithoperator allows you to supply default values, which are not interpreted. Sayingitem=itemis effectively sayingitem|default:"item", which is to say thatitemis redefined as"item".See https://github.com/yogthos/Selmer#including-templates