Answers.Body: <p>I've gotten a bit further into this that is <em>slightly</em> beyond the scope of my original question, but I think it's worth bringing up for the benefit of others (like me).</p>
  
  <p>To answer my original question, this is how I was able to make it work:</p>
  
  <pre><code>{% set i = 0 %}
      {{ user.foo[i].bar }}
      ...
  {% set i = i + 1 %}
  </code></pre>
  
  <p>However, if your templates start to become more complex, and/or need more or different types of looping, you might want to consider looking at a <a href="https://craftcms.com/classreference/services/TemplatesService#hook-detail" rel="nofollow noreferrer">template hook</a></p>
  
  <p>This works pretty slick. Let's say I am getting data from an api or some other service (using a plugin). This is how you would use a hook.</p>
  
  <p><strong>template.twig</strong></p>
  
  <pre><code>...
  {% hook 'myAwesomeHook' %}
  </code></pre>
  
  <p>Since it is recommended to place the hook logic in the <code>init</code> method of your plugin, this is what that would look like:</p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>...
  public function init() {
      craft-&gt;template()-&gt;hook('myAwesomeHook', function(&amp;$context) {
         foreach($context['user'] as $user) {
           return $user; // or do whatever you need.
         }
      }
  }
  </code></pre>
  
  <p>Hope this helps someone else!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-13T18:18:02.083Z
Answers.LastActivityDate: 2017-01-13T18:18:02.083Z
Answers.UserDisplayName: Damon
Answers.Body: <p>I think you need to try and access the array that <code>user.foo</code> would have.
  So your code would be something like:</p>
  
  <pre><code> {% for user in plugin.data.users %}
    ...  
    {{ attribute(user.foo, loop.index0)|raw|nl2br }}
    ...
   {% endfor %}
  </code></pre>
  
  <p>I haven't tested this though, just going by what I'm sure I remember doing once...</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-10T00:54:06.007Z
Answers.Text: Thanks @Alec, I couldn't get it figured out using the `attribute` tag. I updated my question with how I got it to work.
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-09T12:57:38.650Z
Answers.LastActivityDate: 2017-01-09T12:57:38.650Z
Answers.UserDisplayName: Alec Ritson
Body: <p>I am using <a href="https://stackoverflow.com/questions/18900035/how-to-concatenate-two-twig-variables-in-a-twig-loop">this SO post</a> to try to concatenate two variables in twig.</p>
  
  <p>I am very close, but can't seem to get over the hump of rendering the correct syntax.</p>
  
  <p>This is what I am currently using:</p>
  
  <pre><code> {% for user in plugin.data.users %}
       ...  
       {{ attribute(user, 'foo' ~ loop.index0)|raw|nl2br.bar }}
       ...
   {% endfor %}
  </code></pre>
  
  <p>This is what I am currently getting back:</p>
  
  <pre><code>Key "foo0"... for array with keys "...does not exist."
  </code></pre>
  
  <p>What I am trying to create is <code>foo[0]</code>, <code>foo[1]</code> etc.</p>
  
  <p>Well, I couldn't figure it out so this is how I did it. If there is a better way, I'd be happy to update the post.</p>
  
  <pre><code>{% set i = 0 %}
      {{ user.foo[i].bar }}
      ...
  {% set i = i + 1 %}
  </code></pre>
  
Tags: <templating><forloop>
Title: How can I concatenate 2 variables to access array values?
ThreadText: # How can I concatenate 2 variables to access array values?
  
  (Asked by: username_0 on 2017-01-09)
  
  I am using [this SO post](https://stackoverflow.com/questions/18900035/how-to-concatenate-two-twig-variables-in-a-twig-loop) to try to concatenate two variables in twig.
  
  I am very close, but can't seem to get over the hump of rendering the correct syntax.
  
  This is what I am currently using:
  
  ```
   {% for user in plugin.data.users %}
       ...  
       {{ attribute(user, 'foo' ~ loop.index0)|raw|nl2br.bar }}
       ...
   {% endfor %}
  
  ```
  
  This is what I am currently getting back:
  
  ```
  Key "foo0"... for array with keys "...does not exist."
  
  ```
  
  What I am trying to create is `foo[0]`, `foo[1]` etc.
  
  Well, I couldn't figure it out so this is how I did it. If there is a better way, I'd be happy to update the post.
  
  ```
  {% set i = 0 %}
      {{ user.foo[i].bar }}
      ...
  {% set i = i + 1 %}
  
  ```
  
  **Tags:** templating, forloop
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-01-13. Score: 1 | Accepted answer
  
  I've gotten a bit further into this that is *slightly* beyond the scope of my original question, but I think it's worth bringing up for the benefit of others (like me).
  
  To answer my original question, this is how I was able to make it work:
  
  ```
  {% set i = 0 %}
      {{ user.foo[i].bar }}
      ...
  {% set i = i + 1 %}
  
  ```
  
  However, if your templates start to become more complex, and/or need more or different types of looping, you might want to consider looking at a [template hook](https://craftcms.com/classreference/services/TemplatesService#hook-detail)
  
  This works pretty slick. Let's say I am getting data from an api or some other service (using a plugin). This is how you would use a hook.
  
  **template.twig**
  
  ```
  ...
  {% hook 'myAwesomeHook' %}
  
  ```
  
  Since it is recommended to place the hook logic in the `init` method of your plugin, this is what that would look like:
  
  **MyPlugin.php**
  
  ```
  ...
  public function init() {
      craft->template()->hook('myAwesomeHook', function(&$context) {
         foreach($context['user'] as $user) {
           return $user; // or do whatever you need.
         }
      }
  }
  
  ```
  
  Hope this helps someone else!
  
  
  ---
  
  ### Answer by username_1 on 2017-01-09. Score: 1
  
  I think you need to try and access the array that `user.foo` would have.
  So your code would be something like:
  
  ```
   {% for user in plugin.data.users %}
    ...  
    {{ attribute(user.foo, loop.index0)|raw|nl2br }}
    ...
   {% endfor %}
  
  ```
  
  I haven't tested this though, just going by what I'm sure I remember doing once...
  
  > **Comments:**
  > **username_0**: Thanks @username_1, I couldn't get it figured out using the `attribute` tag. I updated my question with how I got it to work.  
  
  ---
  
  