Answers.Body: <p>You can't reference a variable (<code>photo</code>) created inside the partial loop outside the partial (and loop) – it doesn't exist yet – so what you're trying to achieve isn't possible.</p>
  
  <p>What you could do, is to abstract the important bits, namely the field and transform handles:</p>
  
  <pre><code>{% set vars = {
      photos: projectEntries,
      photoField: 'projectPhotos_photo_image',
      photoTransform: 'projectThumbnail'
  } %}
  {% include "_partials/photo-tile.html" with vars %}
  </code></pre>
  
  <p>And inside your partial:</p>
  
  <pre><code>{% for photo in photos %}
     &lt;img src="{{ photo[photoField].first().getUrl(photoTransform) }}" /&gt;
  {% endfor %}
  </code></pre>
  
  <p>If that doesn't cut it, you'll probably want to use a <a href="http://twig.sensiolabs.org/doc/2.x/tags/macro.html" rel="nofollow noreferrer">macro</a>:</p>
  
  <pre><code>{# Your macro file, e.g. /templates/_macros/photoTile.html #}
  {% macro src(photo) %}
      {{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}
  {% endmacro %}
  
  {# Inside your partial #}
  {% from '_macros/photoTile' import src %}
  {% for photo in photos %}
      &lt;img src="{{- src(photo) -}}" /&gt;
  {% endfor %}
  </code></pre>
  
  <p>(If you're wondering about the <code>{{- -}}</code> syntax in the above example, the dashes are just to remove whitespace from the rendered src string – you also wrap the code inside the macro in <code>{% spaceless %}{% endspaceless %}</code> tags.)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-23T20:07:57.593Z
Answers.Text: Thanks for the response. I tried your first idea and it failed. I think that the macro would work but this is a lot of hassle in an attempt to make a componentized, partial-based system. I'm going to experiment with https://vuejs.org
Answers.UserDisplayName: portnoy-the-elder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-21T14:28:13.410Z
Answers.LastActivityDate: 2017-01-21T14:28:13.410Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>How do I pass variables to a loop within an include?</p>
  
  <p>This is my partial:</p>
  
  <pre><code>{% for photo in photos %}
      &lt;img src="{{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}"
  {% endfor %}
  </code></pre>
  
  <p>I'm calling it from a template with this:</p>
  
  <pre><code>{% set projectEntries = craft.entries({
      section: 'project'
  }) %}
  {% set vars = {
      photos: projectEntries
      } 
  %}
  {% include "_partials/photo-tile.html" with vars %}
  </code></pre>
  
  <p>All well and good </p>
  
  <p><em>but</em></p>
  
  <p>What if I want to abstract this from my partial:</p>
  
  <pre><code>src="{{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}"
  </code></pre>
  
  <p>In other words, I'd like to pass the src, <code>{{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}</code> , into the partial but I don't know how!</p>
  
  <p>If I try to do it from the template like this (through <code>photoSrc</code>), it fails, saying the 'photo' is undefined:</p>
  
  <pre><code>{% set vars = {
      section: "category",
      category: category,
      pageOneTitle:  categoryTitle,
      photos: projectEntries,
      photoSrc: photo.projectPhotos_photo_image.first().getUrl('projectThumbnail')
      } 
  %}
  </code></pre>
  
  <p>Any tips?</p>
  
Tags: <templating><include>
Title: How do I pass variables to loops within includes?
ThreadText: # How do I pass variables to loops within includes?
  
  (Asked by: username_0 on 2017-01-21)
  
  How do I pass variables to a loop within an include?
  
  This is my partial:
  
  ```
  {% for photo in photos %}
      <img src="{{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}"
  {% endfor %}
  
  ```
  
  I'm calling it from a template with this:
  
  ```
  {% set projectEntries = craft.entries({
      section: 'project'
  }) %}
  {% set vars = {
      photos: projectEntries
      } 
  %}
  {% include "_partials/photo-tile.html" with vars %}
  
  ```
  
  All well and good
  
  *but*
  
  What if I want to abstract this from my partial:
  
  ```
  src="{{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}"
  
  ```
  
  In other words, I'd like to pass the src, `{{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}` , into the partial but I don't know how!
  
  If I try to do it from the template like this (through `photoSrc`), it fails, saying the 'photo' is undefined:
  
  ```
  {% set vars = {
      section: "category",
      category: category,
      pageOneTitle:  categoryTitle,
      photos: projectEntries,
      photoSrc: photo.projectPhotos_photo_image.first().getUrl('projectThumbnail')
      } 
  %}
  
  ```
  
  Any tips?
  
  **Tags:** templating, include
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-01-21. Score: 1
  
  You can't reference a variable (`photo`) created inside the partial loop outside the partial (and loop) – it doesn't exist yet – so what you're trying to achieve isn't possible.
  
  What you could do, is to abstract the important bits, namely the field and transform handles:
  
  ```
  {% set vars = {
      photos: projectEntries,
      photoField: 'projectPhotos_photo_image',
      photoTransform: 'projectThumbnail'
  } %}
  {% include "_partials/photo-tile.html" with vars %}
  
  ```
  
  And inside your partial:
  
  ```
  {% for photo in photos %}
     <img src="{{ photo[photoField].first().getUrl(photoTransform) }}" />
  {% endfor %}
  
  ```
  
  If that doesn't cut it, you'll probably want to use a [macro](http://twig.sensiolabs.org/doc/2.x/tags/macro.html):
  
  ```
  {# Your macro file, e.g. /templates/_macros/photoTile.html #}
  {% macro src(photo) %}
      {{ photo.projectPhotos_photo_image.first().getUrl('projectThumbnail') }}
  {% endmacro %}
  
  {# Inside your partial #}
  {% from '_macros/photoTile' import src %}
  {% for photo in photos %}
      <img src="{{- src(photo) -}}" />
  {% endfor %}
  
  ```
  
  (If you're wondering about the `{{- -}}` syntax in the above example, the dashes are just to remove whitespace from the rendered src string – you also wrap the code inside the macro in `{% spaceless %}{% endspaceless %}` tags.)
  
  > **Comments:**
  > **username_0**: Thanks for the response. I tried your first idea and it failed. I think that the macro would work but this is a lot of hassle in an attempt to make a componentized, partial-based system. I'm going to experiment with https://vuejs.org  
  
  ---
  
  