<p>It's not a simple question, but a fundamental one.</p>

<p>Whenever you query one of Craft's Element API services (such as <code>craft.entries</code>, <code>craft.assets</code> and <code>craft.categories</code>) or a relational field (such as an Entries or Assets field, i.e. <code>entry.myAssetsField</code>) you'll get back an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> instance.</p>

<p>The ElementCriteriaModel is basically a fancy wrapper for your query, which you can call methods on (such as <code>.find()</code>, <code>.ids()</code>, <code>.first()</code> and <code>.last()</code>), in order to actually pull elements based on your criteria (e.g. <code>.section('sectionHandle')</code>). Some methods – such as <code>.find()</code> – will return an array of elements (Craft actually calls <code>.find()</code> implicitly unless you use <a href="https://craftcms.com/docs/templating/elementcriteriamodel#fetching-the-elements" rel="nofollow">one of the other methods</a>), while others will return a single element (<code>.first()</code>, <code>.last()</code>). There are also methods that can do different things entirely, such as returning an array of matching element IDs (<code>.ids()</code>).</p>

<p>In your example, you're using <code>.find()</code> – which means Craft will return an array of elements (even if there's only one). In such cases, it usually makes more sense to use <code>.first()</code>, to make Craft return a single element and avoid the <code>for</code> loop:</p>

<pre><code>{% set entry = craft.entries.section('sectionHandle').first() %}
</code></pre>

<p>However, since the query may not always return any matching elements, it's usually a good idea to wrap the result in a conditional:</p>

<pre><code>{% set entry = craft.entries.section('sectionHandle').first() %}
{% if entry %}
    ...
{% endif %}
</code></pre>

<p>The above will of course also be true for any relational fields – consider an Assets field for a "featured image"; even if you limit the field to a single image, simply accessing the field in a template will always return an ElementCriteriaModel, unless you use <code>.first()</code>:</p>

<pre><code>{# Returns an ElementCriteriaModel #}
{% set featuredImage = entry.someAssetsField %}

{# Returns an array of elements #}
{% set featuredImage = entry.someAssetsField.find() %}

{# Returns a single Asset #}
{% set featuredImage = entry.someAssetsField.first() %}
</code></pre>

<blockquote>
  <p>Also I am curious if 'entry' in the for loop actually means anything or is just a good symantic name to use</p>
</blockquote>

<p>It's just a semantic name – the result variable for your query can be named whatever you want:</p>

<pre><code>{% set myAwesomeSingleEntry = craft.entries.section('sectionHandle').first() %}
</code></pre>

<p>However, a good thing to know about is that whenever Craft <a href="https://craftcms.com/docs/routing" rel="nofollow">matches the current URL request to an element</a> – e.g. your Single entry – it'll automatically populate your template with an <code>entry</code> variable. In other words, if the current page is the actual URL for your Single entry, you won't need to do any <code>craft.entries</code> query at all (this is true for all Entries and Categories that have URLs enabled).</p>

CC BY-SA 3.0
2016-08-03T23:05:09.500Z
2016-08-04T01:32:53.120Z
2016-08-04T01:32:53.120Z
Mats Mikkel Rummelhoff
<p>I am brand new to Craft so excuse me if the question seems a little simple. Is this the best way to return the contents of a 'singles' section. Does everything end up in a for loop even if you are only returning a single entry.</p>

<pre><code>{% for entry in craft.entries.section('sectionHandle').limit(1).find() %}
  {{ entry.title }}
{% endfor %}
</code></pre>

<p>Also I am curious if 'entry' in the for loop actually means anything or is just a good symantic name to use for example could you say</p>

<pre><code>{% for oranges in ...%}{% oranges.title %}{% endfor %}
</code></pre>

<entries>
Singles Entry return content
# Singles Entry return content

(Asked by: username_0 on 2016-08-03)

I am brand new to Craft so excuse me if the question seems a little simple. Is this the best way to return the contents of a 'singles' section. Does everything end up in a for loop even if you are only returning a single entry.
{% for entry in craft.entries.section('sectionHandle').limit(1).find() %}
  {{ entry.title }}
{% endfor %}

Also I am curious if 'entry' in the for loop actually means anything or is just a good symantic name to use for example could you say
{% for oranges in ...%}{% oranges.title %}{% endfor %}

**Tags:** entries

---

## 1 answers

### Answer by username_1 on 2016-08-03. Score: 5 | Accepted answer

It's not a simple question, but a fundamental one.
Whenever you query one of Craft's Element API services (such as craft.entries, craft.assets and craft.categories) or a relational field (such as an Entries or Assets field, i.e. entry.myAssetsField) you'll get back an ElementCriteriaModel instance.
The ElementCriteriaModel is basically a fancy wrapper for your query, which you can call methods on (such as .find(), .ids(), .first() and .last()), in order to actually pull elements based on your criteria (e.g. .section('sectionHandle')). Some methods – such as .find() – will return an array of elements (Craft actually calls .find() implicitly unless you use one of the other methods), while others will return a single element (.first(), .last()). There are also methods that can do different things entirely, such as returning an array of matching element IDs (.ids()).
In your example, you're using .find() – which means Craft will return an array of elements (even if there's only one). In such cases, it usually makes more sense to use .first(), to make Craft return a single element and avoid the for loop:
{% set entry = craft.entries.section('sectionHandle').first() %}

However, since the query may not always return any matching elements, it's usually a good idea to wrap the result in a conditional:
{% set entry = craft.entries.section('sectionHandle').first() %}
{% if entry %}
    ...
{% endif %}

The above will of course also be true for any relational fields – consider an Assets field for a "featured image"; even if you limit the field to a single image, simply accessing the field in a template will always return an ElementCriteriaModel, unless you use .first():
{# Returns an ElementCriteriaModel #}
{% set featuredImage = entry.someAssetsField %}

{# Returns an array of elements #}
{% set featuredImage = entry.someAssetsField.find() %}

{# Returns a single Asset #}
{% set featuredImage = entry.someAssetsField.first() %}


Also I am curious if 'entry' in the for loop actually means anything or is just a good symantic name to use

It's just a semantic name – the result variable for your query can be named whatever you want:
{% set myAwesomeSingleEntry = craft.entries.section('sectionHandle').first() %}

However, a good thing to know about is that whenever Craft matches the current URL request to an element – e.g. your Single entry – it'll automatically populate your template with an entry variable. In other words, if the current page is the actual URL for your Single entry, you won't need to do any craft.entries query at all (this is true for all Entries and Categories that have URLs enabled).


---

