Answers.Body: <p>To convert the results of an Element Query into a custom key/value array you can use the <a href="https://craftcms.com/docs/3.x/dev/filters.html#index" rel="nofollow noreferrer">index</a> filter to specify what you want to use as the keys. So for example if you want the category slugs to be the keys and the category titles to be the values you'd do it like this:</p>
  <pre><code>{% set indexedCats = craft.categories.all()|index('slug') %}
  {{ macro.select('name', indexedCats }}
  
  =================
  
  output:
  
  &lt;select name=&quot;name&quot;&gt;
      &lt;option value=&quot;cat-slug-1&quot;&gt;Cat Title 1&lt;/option&gt;
      &lt;option value=&quot;cat-slug-2&quot;&gt;Cat Title 2&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  <p>Swap in 'title' if you want the key and value to both be the same.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-16T02:43:50.833Z
Answers.Text: Awesome! Never knew about `|index()` - thanks!
Answers.UserDisplayName: supazu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-15T16:45:08.477Z
Answers.LastActivityDate: 2021-02-15T16:45:08.477Z
Answers.UserDisplayName: James Smith
Body: <p>I have a macro for form fields, including select:</p>
  <pre><code>{% macro select(name, options) %}
    &lt;select name=&quot;{{ name }}&quot;&gt;
      {% for key, value in options %}
        &lt;option value=&quot;{{ key }}&quot;&gt;{{ value }}&lt;/option&gt;
      {% endfor %}
    &lt;/select&gt;
  {% endmacro %}
  </code></pre>
  <p>Which I call with:
  <code>{{ macro.select('name', {'key': 'value'}) }}</code></p>
  <p>This works well when I define the key and value, however, I want to populate it from my categories.</p>
  <p><code>{% set categoriesQuery = craft.categories().group('categories').all() %}</code></p>
  <p>How can I make <code>{'key': 'value'}</code> loop over results from <code>categoriesQuery</code>? Hope that makes sense!</p>
  
Tags: <templating>
Title: Loop over query results in a macro
ThreadText: # Loop over query results in a macro
  
  (Asked by: username_0 on 2021-02-14)
  
  I have a macro for form fields, including select:
  
  ```
  {% macro select(name, options) %}
    <select name="{{ name }}">
      {% for key, value in options %}
        <option value="{{ key }}">{{ value }}</option>
      {% endfor %}
    </select>
  {% endmacro %}
  
  ```
  
  Which I call with:
  `{{ macro.select('name', {'key': 'value'}) }}`
  
  This works well when I define the key and value, however, I want to populate it from my categories.
  
  `{% set categoriesQuery = craft.categories().group('categories').all() %}`
  
  How can I make `{'key': 'value'}` loop over results from `categoriesQuery`? Hope that makes sense!
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-02-15. Score: 3 | Accepted answer
  
  To convert the results of an Element Query into a custom key/value array you can use the [index](https://craftcms.com/docs/3.x/dev/filters.html#index) filter to specify what you want to use as the keys. So for example if you want the category slugs to be the keys and the category titles to be the values you'd do it like this:
  
  ```
  {% set indexedCats = craft.categories.all()|index('slug') %}
  {{ macro.select('name', indexedCats }}
  
  =================
  
  output:
  
  <select name="name">
      <option value="cat-slug-1">Cat Title 1</option>
      <option value="cat-slug-2">Cat Title 2</option>
  </select>
  
  ```
  
  Swap in 'title' if you want the key and value to both be the same.
  
  > **Comments:**
  > **username_0**: Awesome! Never knew about `|index()` - thanks!  
  
  ---
  
  