title: Elixir templates - looping through a list with iterator value
body: <p>Coming from other programming languages, many like me will be in for a surprise. I have a simple problem. I have a list - say, users. I want to iterate through the users and display some information. Pretty simple until I got stumped with this: </p>
  
  <p>Using an eex template, I was trying to do this:</p>
  
  <pre><code>&lt;%= for i &lt;- 0..length(@users) do %&gt;
    &lt;% user = Enum.at(@users) %&gt;
  
    &lt;!-- every third user, I need to display on a new "row" --&gt;
    &lt;%= if rem(i,3) == 0 do %&gt;
      &lt;div class="row"&gt;
    &lt;% end %&gt;
  
    &lt;!-- display user information - like user name --&gt;
    &lt;%= user.name %&gt;
  
    &lt;!-- then close out the "row" tag if we are due for starting another on the next iteration --&gt;
  
     &lt;%= if rem(i+1,3) == 0 do %&gt;
      &lt;div class="row"&gt;
    &lt;% end %&gt; 
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>Firstly, the user value is turning out to be nil. Not sure why. Secondly, I don't have an explanation for this.</p>
  
  <p>What is the difference between "user = Enum.at(@users, i)" and &lt;%= for user &lt;- @users %>? In the latter case, the user is not nil.</p>
  
  <p>Finally, what is the best way to keep track of the times through the list and take a slightly different action (like starting the user on a new row as in the example above)?</p>
  
  <p>To keep things simple, I have also ignored the fact that we may not have a final closing "/div" tag for the row (which depends on the number of users in the list). I've ignored putting any logic around that just to focus on the issue at hand.</p>