Recently I've been exploring the world of gulp and nunjucks templating, specifically for creating emails.
One challenge I'm facing is figuring out how to call a module/partial and assign different values to its attributes each time it's processed.
While my initial thought was to use a for loop, the modules won't always be called sequentially within the template.
Within these modules, there are attributes assigned to variables that I'd like to resolve differently based on the section where the module is used.
For example, in an index file:
{% block content %}
<!-- logo start -->
{% include genericMod %}
<!-- logo end -->
<!-- some other section start -->
{% include someOtherMod %}
<!-- some other section end -->
<!-- hero start -->
{% include genericMod %}
<!-- hero end -->
{% endblock %}
In the genericMod itself:
<tr>
<td class="full-width-image" align="{{align}}" ><img src="{{src}}" alt="{{alt}}"/></td>
</tr>
I would like to introduce a functionality where I can define a "modKey" variable within each attribute of the module. For example:
{{modKey.align}}
{{modKey.src}}
{{modKey.alt}}
Then, somehow assign that key to the module each time it's called:
<!-- logo start —>
{% include genericMod "modKey": "logo" %}
<!-- logo end -->
So that when using JSON data, the respective values would be rendered for each attribute variable:
"logo": {
"alt": "some logo alt text",
"href": "http://www.someurl.com",
"align": "left"
},
"hero": {
"alt": "some hero alt text",
"href": "http://www.someotherurl.com",
"align": "centre"
}
This is just a concept, but I'm wondering if there's a way to actually achieve something similar?