title: Go Templates: Are Nested Ranges Possible?
body: <p>This one is seemingly simple but it's driving me insane.</p>
  
  <p>How does one go about referencing a struct element higher in the scope within a nested range in golang templates?</p>
  
  <p>Example:</p>
  
  <pre><code>type Foo struct {
    Id string
    Name string
  }
  
  type Bar struct {
    Id string
    Name string
  }
  
  var foos []Foo
  var bars []Bar
  
  // logic to populate both foos and bars
  </code></pre>
  
  <p>In the template:</p>
  
  <pre><code>{{range .foos}}
    &lt;div&gt;Foo {{.Name}}&lt;/div&gt;
    &lt;div&gt;
      {{range ..bars}}
        &lt;div&gt;Bar {{.Name}} &lt;input type="text" name="ids_{{..Id}}_{{.Id}}" /&gt;&lt;/div&gt;
      {{end}}
    &lt;/div&gt;
  {{end}}
  </code></pre>
  
  <p>Obviously ..bars and ..Id don't work, but hopefully my intent is clear.  I'd like to iterate through all combinations of Foo and Bar and generate a form element with a name build by both the Foo's Id and the Bar's Id.</p>
  
  <p>The problem is that it seems it is impossible to:</p>
  
  <ol>
  <li>Access bars from inside the scope of the foos range scope</li>
  <li>Access Foo's Id from inside the bar's range scope</li>
  </ol>
  
  <p>I have a temporary workaround to this by putting a bunch of redundant fields in both structs, but this seems very ugly to me, violates DRY, and in general feels very wrong.</p>
  
  <p>Is there any way with golang templates to do what I'd like to do?</p>