<p>When you use the Twig curly brace <code>{}</code>, key/value pair syntax like you've got <code>imagerConfig</code> set to, you're using the Twig <a href="https://twig.sensiolabs.org/doc/2.x/templates.html#literals" rel="nofollow noreferrer">hash</a> literal.</p>

<p>In Twig, if you want to add to an array or hash after it has already been created, you can use the <a href="https://twig.sensiolabs.org/doc/2.x/filters/merge.html#merge" rel="nofollow noreferrer">merge</a> filter.</p>

<p>Something like this should do what you're looking for:</p>

<pre><code>{% set imagerConfig = { jpegQuality: 80, pngCompressionLevel: 4 } %}

{% set width = { width: 1000 } %}

{% set imagerConfig = imagerConfig|merge(width) %}

{% set largeLogo = craft.imager.transformImage((logo.getUrl('LargeLogo')), imagerConfig) %}
</code></pre>

CC BY-SA 3.0
2017-07-11T00:16:46.597Z
2017-07-11T00:16:46.597Z
Brad Bell
<p>I'm using the <a href="https://github.com/aelvan/Imager-Craft" rel="nofollow noreferrer">Imager</a> plugin and Craft for image compression. </p>

<p>Currently, I have something like this: </p>

<pre><code>{% set imagerConfig = { jpegQuality: 80, pngCompressionLevel: 4 } %} 
</code></pre>

<p>In another part of my code, I want to do something like: </p>

<pre><code> {% set largeLogo = craft.imager.transformImage((logo.getUrl('LargeLogo')), imagerConfig) %}
</code></pre>

<p>Where I want to do something like this to <code>imagerConfig</code> above: </p>

<pre><code>{% set width = {width: 1000} %} or {%set widthRatio = {width: 1000, ratio: 4/2} %}
imagerConfig.Add(width) //what's the proper syntax for adding an item or even another object(like widthRatio above) to imagerConfig here? 
</code></pre>

<templating><plugin-imager><twigextension>
How do I add a new item or object to an existing object?
# How do I add a new item or object to an existing object?

(Asked by: username_0 on 2017-07-10)

I'm using the Imager plugin and Craft for image compression. 
Currently, I have something like this: 
{% set imagerConfig = { jpegQuality: 80, pngCompressionLevel: 4 } %} 

In another part of my code, I want to do something like: 
 {% set largeLogo = craft.imager.transformImage((logo.getUrl('LargeLogo')), imagerConfig) %}

Where I want to do something like this to imagerConfig above: 
{% set width = {width: 1000} %} or {%set widthRatio = {width: 1000, ratio: 4/2} %}
imagerConfig.Add(width) //what's the proper syntax for adding an item or even another object(like widthRatio above) to imagerConfig here?

**Tags:** templating, plugin-imager, twigextension


---

## 1 answers

### Answer by username_1 on 2017-07-11. Score: 1 | Accepted answer

When you use the Twig curly brace {}, key/value pair syntax like you've got imagerConfig set to, you're using the Twig hash literal.
In Twig, if you want to add to an array or hash after it has already been created, you can use the merge filter.
Something like this should do what you're looking for:
{% set imagerConfig = { jpegQuality: 80, pngCompressionLevel: 4 } %}

{% set width = { width: 1000 } %}

{% set imagerConfig = imagerConfig|merge(width) %}

{% set largeLogo = craft.imager.transformImage((logo.getUrl('LargeLogo')), imagerConfig) %}


---

