Answers.Body: <p>No you can't with any build in Craft functions. I've added <a href="http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7203963-eager-loading-of-field-relations-in-templates" rel="nofollow">a feature request for this on Crafts feedback site</a>. </p>
  
  <p>What you can do for now is to use Mike Peppers excellent Plugin "Fetch" which pre-fetch relations for you. </p>
  
  <p>Ser here <a href="https://github.com/mdcpepper/fetch" rel="nofollow">https://github.com/mdcpepper/fetch</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-18T05:08:46.460Z
Answers.LastActivityDate: 2015-04-18T10:42:57.443Z
Answers.LastEditDate: 2015-04-18T10:42:57.443Z
Answers.UserDisplayName: naboovalley
Body: <p>I have a Matrix field with a single block type and within that, a number of fields that I need to pull out:</p>
  
  <pre><code>{% for roomContent in hotelEntry.roomsAvailable %}
      {% if roomContent.code == room.roomTypeCode %}
          &lt;p&gt;room code = {{ roomContent.code }}&lt;br&gt;
              description = {{ roomContent.description }}&lt;br&gt;
              {% for image in roomContent.images %}
                  &lt;img src="{{ image.url }}" alt="{{ roomContent.description }}" height="50"&gt;
              {% endfor %}
          &lt;/p&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>I am doing this on a listing page where there are around 60 entries, so this is resulting in a lot of calls to the DB.</p>
  
  <p>Is there a way I can eager load all of the data for all of the entries outside of this loop so I'm not recursively calling for data?</p>
  
Tags: <matrix>
Title: Can I eager load data within a template?
ThreadText: # Can I eager load data within a template?
  
  (Asked by: username_0 on 2015-04-17)
  
  I have a Matrix field with a single block type and within that, a number of fields that I need to pull out:
  
  ```
  {% for roomContent in hotelEntry.roomsAvailable %}
      {% if roomContent.code == room.roomTypeCode %}
          <p>room code = {{ roomContent.code }}<br>
              description = {{ roomContent.description }}<br>
              {% for image in roomContent.images %}
                  <img src="{{ image.url }}" alt="{{ roomContent.description }}" height="50">
              {% endfor %}
          </p>
      {% endif %}
  {% endfor %}
  
  ```
  
  I am doing this on a listing page where there are around 60 entries, so this is resulting in a lot of calls to the DB.
  
  Is there a way I can eager load all of the data for all of the entries outside of this loop so I'm not recursively calling for data?
  
  **Tags:** matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-18. Score: 1 | Accepted answer
  
  No you can't with any build in Craft functions. I've added [a feature request for this on Crafts feedback site](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7203963-eager-loading-of-field-relations-in-templates).
  
  What you can do for now is to use Mike Peppers excellent Plugin "Fetch" which pre-fetch relations for you.
  
  Ser here <https://github.com/mdcpepper/fetch>
  
  
  ---
  
  