I have 2 stencil templates, which pretty much the same except the variable name is different:
template #1
{% for p in anArray %}
{% if p.property1 = "abc" %}
// some logic
{% endif %}
{% endfor %}
template #2
{% for a in aDifferentNameArray %}
{% if a.property1 = "abc" %}
// same logic as template 1
{% endif %}
{% endfor %}
I think it will be cleaner if I can refactor this into a template and have template #1 and #2 call this new template
{% if ??.property1 = "abc" %}
// same logic as template 1
{% endif %}
But problem is in template #1, the variable is p
where as in template #2, the variable is a
.
So what can i do to call the new template with template #1 & #2 with different variable name?
This is exactly the use case for the
include
tag. Using this tag, you can include the contents of another template, and also pass it a context of your choice.Move
some logic
to a separate template. Let's call that filesome logic.stencil
. In there, wherever you have usedp.propertyX
, you should change it to justpropertyX
, since you are giving itp
/a
as the context.some logic.stencil:
In template #1, do:
In template #2, do: