Answers.Body: <p>So the only database queries in your example will be generated by:</p>
  
  <pre><code>{% set navigation = craft.entries.section('pages').all() %}
  </code></pre>
  
  <p>So creating an additional object doesn't serve the stated purpose of saving db queries. You can just access the <code>navigation</code> array directly.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-21T13:44:03.780Z
Answers.Text: Thanks for the reply. I don't think I can access an individual page by it's id from the navigation array though. (Or at least I don't know how to.) How would I grab the contact page's url for example from the navigation array directly? (can I do navigation.find('...wahtever')) The goal of the object is to prevent an additional call to craft.entries.section('pages').id('5').url, which would be another db query.
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-21T14:11:04.813Z
Answers.Text: Well, you can just iterate through the array of results... but if you need to look them up by name or such, then sure, makes sense!
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-21T19:58:30.107Z
Answers.Text: Thanks, out or curiosity, if you wanted to grab the contact page's url, and you had already iterated over your nav with a for loop, what would you do? I wonder if I am being to obsessive, but I feel like the data for the contact page is "right there," just not in a way I can conveniently access.
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-21T02:49:20.920Z
Answers.LastActivityDate: 2018-08-21T02:49:20.920Z
Answers.UserDisplayName: andrew.welch
Body: <p>I have the goal of querying the database once for page info, and then using it to display information on select pages site wide.</p>
  
  <p>For example, I want to link the the contact page with using a url slug like "contact" (which might change)</p>
  
  <p>My thinking is to query for pages, then loop over them in twig and create an object which I can access as needed.</p>
  
  <p>Here is some example code, my question is - is the best Craft like way to do this? Am I missing something that should make this easier?</p>
  
  <pre><code>{#
   # Query for all the Pages
   #}
  {% set navigation = craft.entries.section('pages').all() %}
  
  {#
   # Create an object to quickly reference pages without making additional DB queries #}
   #}
  {% set pageData = [] %}
  
  {% for item in navigation %}
      {#
       # create a dynamic property if one doesn't already exist
       #}
      {% if not attribute(pageData, 'pageId' ~ item.id) is defined %}
          {% set pageData = pageData|merge({ ('pageId' ~ item.id) : {
          'id':item.id,
          'slug':item.slug,
          'url':item.url,
          'title':item.title} }) %}
      {% endif %}
  
  {% endfor %}
  
  {#
   # Use the pageData object to reference pages by id as needed
   #}
  &lt;p&gt;&lt;a href="{{pageData.pageId5.url}}" class="button small alert"&gt;Request a Bid&lt;/a&gt;&lt;/p&gt;
  </code></pre>
  
Tags: <templating><navigation><performance>
Title: Access pages data with fewer queries by creating an object in Twig
ThreadText: # Access pages data with fewer queries by creating an object in Twig
  
  (Asked by: username_0 on 2018-08-21)
  
  I have the goal of querying the database once for page info, and then using it to display information on select pages site wide.
  
  For example, I want to link the the contact page with using a url slug like "contact" (which might change)
  
  My thinking is to query for pages, then loop over them in twig and create an object which I can access as needed.
  
  Here is some example code, my question is - is the best Craft like way to do this? Am I missing something that should make this easier?
  
  ```
  {#
   # Query for all the Pages
   #}
  {% set navigation = craft.entries.section('pages').all() %}
  
  {#
   # Create an object to quickly reference pages without making additional DB queries #}
   #}
  {% set pageData = [] %}
  
  {% for item in navigation %}
      {#
       # create a dynamic property if one doesn't already exist
       #}
      {% if not attribute(pageData, 'pageId' ~ item.id) is defined %}
          {% set pageData = pageData|merge({ ('pageId' ~ item.id) : {
          'id':item.id,
          'slug':item.slug,
          'url':item.url,
          'title':item.title} }) %}
      {% endif %}
  
  {% endfor %}
  
  {#
   # Use the pageData object to reference pages by id as needed
   #}
  <p><a href="{{pageData.pageId5.url}}" class="button small alert">Request a Bid</a></p>
  
  ```
  
  **Tags:** templating, navigation, performance
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-21. Score: 1
  
  So the only database queries in your example will be generated by:
  
  ```
  {% set navigation = craft.entries.section('pages').all() %}
  
  ```
  
  So creating an additional object doesn't serve the stated purpose of saving db queries. You can just access the `navigation` array directly.
  
  > **Comments:**
  > **username_0**: Thanks for the reply. I don't think I can access an individual page by it's id from the navigation array though. (Or at least I don't know how to.) How would I grab the contact page's url for example from the navigation array directly? (can I do navigation.find('...wahtever')) The goal of the object is to prevent an additional call to craft.entries.section('pages').id('5').url, which would be another db query.  
  > **username_1**: Well, you can just iterate through the array of results... but if you need to look them up by name or such, then sure, makes sense!  
  > **username_0**: Thanks, out or curiosity, if you wanted to grab the contact page's url, and you had already iterated over your nav with a for loop, what would you do? I wonder if I am being to obsessive, but I feel like the data for the contact page is "right there," just not in a way I can conveniently access.  
  
  ---
  
  