<p>The first example makes sense because you are passing a multidimensional  array (<code>syName</code>), looping through that array, assigns each array within to <code>alt</code> allowing you to then access the values through dot notation (<code>{{ alt.name }}</code>), because <code>alt</code> is an array.</p>

<p>In your second example you are passing through just one array, so when you loop through it, the value in <code>name</code> is assigned to <code>alt</code> as a string. (the value in the array) so you can echo it out just by doing <code>{{ alt }}</code></p>

<p>So your code could look like:</p>

<pre><code>{% macro prognose(symbol, syName) %}
   &lt;img src="test/{{ symbol }}.png"&gt;
   {% for name in syName %}
      {{ name }}
   {% endfor %}
{% endmacro %}

{{ _self.prognose(  '1' , syName.x ) }}
</code></pre>

<p>This will output <code>Schön</code></p>

<p>To make things even easier for yourself, if you are passing just one array i.e <code>syName.x</code> You don't even need the loop as you can just access the value through dot notation:</p>

<pre><code>{% set syName = {
   x: { name: "Schön"},
   y: { name: "Leicht bewölkt"},
   z: { name: "Stark Bewölkt"},
}%}

{% macro prognose(symbol, syName) %}
    &lt;img src="test/{{ symbol }}.png"&gt;
    {{ syName.name }} 
{% endmacro %}

{{ _self.prognose('1', syName.x) }}
</code></pre>

<p>This will output <code>Schön</code></p>

CC BY-SA 3.0
2015-06-16T09:23:46.240Z
Thank you. The one with the for loop works now. But you simplified code doesn't. It throws: `Impossible to access an attribute ("name") on a NULL variable ("")`
KSPR
CC BY-SA 3.0
2015-06-16T09:28:19.847Z
That would happen if the value of the array being passed to the macro is `null`, are you passing calling the macro like `{{ _self.prognose(  '1' , syName.x ) }}` and `syName.x` is not `null`?
Alec Ritson
CC BY-SA 3.0
2015-06-16T11:45:38.193Z
Yep exactly. But the value of `x` is not not `null`, its `Schön`.
KSPR
CC BY-SA 3.0
2015-06-16T11:53:13.217Z
That's bizarre, I've just updated that part of my answer to show the full code i'm using, which is working....can you confirm this is the same as yours?
Alec Ritson
CC BY-SA 3.0
2015-06-16T12:04:06.533Z
I copied your code 1:1 and it works. There must be a typo or something in my extended code. I just can't see it right now. But your code is correct.
KSPR
CC BY-SA 3.0
2015-06-16T09:03:38.023Z
2015-06-16T11:52:16.863Z
2015-06-16T11:52:16.863Z
Alec Ritson
<p>I try to pass a variable from to a macro:</p>

<pre><code>{% set syName = {
   x: { name: "Schön"},
   y: { name: "Leicht bewölkt"},
   z: { name: "Stark Bewölkt"},
    } 
%}

{% macro prognose(symbol, syName) %}
   &lt;img src="test/{{ symbol }}.png"&gt;
   {% for alt in syName %}
      {{ alt.name }}
   {% endfor %}
{% endmacro %}

{{ _self.prognose(  '1' , syName ) }}
</code></pre>

<p>So far so good. But this passes all the content from the variable into the macro. I want to trigger x, y, z individually.</p>

<p>So I tried to write it like this: <code>{{ _self.prognose(  '1' , syName.x ) }}</code></p>

<p>This gives the error: </p>

<blockquote>
  <p>Impossible to access an attribute ("name") on a string variable
  ("Schön")</p>
</blockquote>

<p>How do I have to write this?</p>

CC BY-SA 3.0
2015-06-16T17:10:07.367Z
FYI, you shouldn't use `_self` like that... Per the docs: ["When you define a macro in the template where you are going to use it, you might be tempted to call the macro directly via _self.input() instead of importing it; even if seems to work, this is just a side-effect of the current implementation and it won't work anymore in Twig 2.x."](http://twig.sensiolabs.org/doc/tags/macro.html) Even though the macro is in the same file, you still need to `import` it.
Lindsey D
CC BY-SA 3.0
2015-06-17T05:45:51.330Z
I plan on placing all macros in a separate file later on.  So I need write an import tag between the macro and the call to the macro? Something like this? `{% macro bla( greetings ) %}{{ greetings }}{% endmacro %}{% import 'bla' as this %}{{ this.bla(  'hello' ) }}`
KSPR
CC BY-SA 3.0
2015-06-17T05:48:57.223Z
Yup, exactly. So something like `{% import _self as myMacros %}`, then you could call it like `{{ myMacros.prognose('1', syName) }}`. In fact, when you move it to a separate file, you'll need that `import` anyway. :)
Lindsey D
<variable><macro>
Pass a variable array to a macro
# Pass a variable array to a macro

(Asked by: username_0 on 2015-06-16)

I try to pass a variable from to a macro:
{% set syName = {
   x: { name: "Schön"},
   y: { name: "Leicht bewölkt"},
   z: { name: "Stark Bewölkt"},
    } 
%}

{% macro prognose(symbol, syName) %}
   
   {% for alt in syName %}
      {{ alt.name }}
   {% endfor %}
{% endmacro %}

{{ _self.prognose(  '1' , syName ) }}

So far so good. But this passes all the content from the variable into the macro. I want to trigger x, y, z individually.
So I tried to write it like this: {{ _self.prognose(  '1' , syName.x ) }}
This gives the error: 

Impossible to access an attribute ("name") on a string variable
  ("Schön")

How do I have to write this?

**Tags:** variable, macro
> **Comments:**
> **username_1**: FYI, you shouldn't use `_self` like that... Per the docs: ["When you define a macro in the template where you are going to use it, you might be tempted to call the macro directly via _self.input() instead of importing it; even if seems to work, this is just a side-effect of the current implementation and it won't work anymore in Twig 2.x."](http://twig.sensiolabs.org/doc/tags/macro.html) Even though the macro is in the same file, you still need to `import` it.  
> **username_0**: I plan on placing all macros in a separate file later on.  So I need write an import tag between the macro and the call to the macro? Something like this? `{% macro bla( greetings ) %}{{ greetings }}{% endmacro %}{% import 'bla' as this %}{{ this.bla(  'hello' ) }}`  
> **username_1**: Yup, exactly. So something like `{% import _self as myMacros %}`, then you could call it like `{{ myMacros.prognose('1', syName) }}`. In fact, when you move it to a separate file, you'll need that `import` anyway. :)  

---

## 1 answers

### Answer by username_2 on 2015-06-16. Score: 5 | Accepted answer

The first example makes sense because you are passing a multidimensional  array (syName), looping through that array, assigns each array within to alt allowing you to then access the values through dot notation ({{ alt.name }}), because alt is an array.
In your second example you are passing through just one array, so when you loop through it, the value in name is assigned to alt as a string. (the value in the array) so you can echo it out just by doing {{ alt }}
So your code could look like:
{% macro prognose(symbol, syName) %}
   
   {% for name in syName %}
      {{ name }}
   {% endfor %}
{% endmacro %}

{{ _self.prognose(  '1' , syName.x ) }}

This will output Schön
To make things even easier for yourself, if you are passing just one array i.e syName.x You don't even need the loop as you can just access the value through dot notation:
{% set syName = {
   x: { name: "Schön"},
   y: { name: "Leicht bewölkt"},
   z: { name: "Stark Bewölkt"},
}%}

{% macro prognose(symbol, syName) %}
    
    {{ syName.name }} 
{% endmacro %}

{{ _self.prognose('1', syName.x) }}

This will output Schön

> **Comments:**
> **username_0**: Thank you. The one with the for loop works now. But you simplified code doesn't. It throws: `Impossible to access an attribute ("name") on a NULL variable ("")`  
> **username_2**: That would happen if the value of the array being passed to the macro is `null`, are you passing calling the macro like `{{ _self.prognose(  '1' , syName.x ) }}` and `syName.x` is not `null`?  
> **username_0**: Yep exactly. But the value of `x` is not not `null`, its `Schön`.  
> **username_2**: That's bizarre, I've just updated that part of my answer to show the full code i'm using, which is working....can you confirm this is the same as yours?  
> **username_0**: I copied your code 1:1 and it works. There must be a typo or something in my extended code. I just can't see it right now. But your code is correct.  

---

