I'd like to create DRY code by passing template literals into a twig {% includes 'X' %}
as variables so they can be used in the includes code (examples below).
I have seen that you can pass a variable into a includes, like the below:
{% include 'template.html' with {'foo': 'bar'} %}
But since my code is going to be using the same includes template multiple times on the same page I need a bit more customization here.
Ideal what I want to do is set the viable when calling the includes
and have that echoed into the template, example:
in page file:
{% set contact_img = 'path to image location' %}
{% include "/IMPORT/Specific/full_page_img_bg" with contact_img %}
in template
{% set transformed_{{ VARIABLE NAME PASSED HERE }} = craft.imager.transformImage({{ VARIABLE NAME PASSED HERE }}_for_transform, [
{ width: 2000, jpegQuality: 89 },
{ width: 1300, jpegQuality: 84 },
{ width: 750, jpegQuality: 82 },
{ width: 500, jpegQuality: 80 },
], { allowUpscale: false, mode: 'fit'}) %}
<img sizes="
(max-width: 500px) 500px,
(max-width: 750px) 750px,
(max-width: 1280px) 1300px,
2000px"
srcset="{{ craft.imager.srcset(transformed_{{ VARIABLE NAME PASSED HERE }}) }}"
alt="{{ {{ VARIABLE NAME PASSED HERE }}.title }}"
class="full-background-image"/>
<div class="full-gradient"></div>
Basically its like how in JS you can set a functions argument and that argument is outputted to the function, but you can call that function multiple times with different arguments i.e.
function callName(name) {
console.log(name)
}
callName('DEV-BOY')
#DEV-BOY
callName('BOY-BE_DEV')
#BOY-VE_DEV
Feel free to ask any question, I'm sure there must be a way to do this, thanks in advance - W