<p>The statement is only executed once. </p>

<p>You are essentially creating an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">elementCriteriaModel</a>, returning the first element using the <code>.first()</code> parameter (which in this case is an <a href="http://buildwithcraft.com/docs/templating/entrymodel" rel="nofollow">Entry Model</a>), and storing it in a variable; which you then can use to access your field values. </p>

<p>If you want to further increase performance you can wrap the whole block in a <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow">cache</a> tag, which will speed up subsequent requests.</p>

CC BY-SA 3.0
2015-02-01T07:25:49.210Z
Thanks! I'm guessing this also applies if said variables are actually called further down in other "included" templates?
Paul
CC BY-SA 3.0
2015-02-01T08:58:29.593Z
Yes and no — it applies anywhere that the variable is still in scope. Variables defined in a template do not automatically carry over to included templates. You would have to explicitly include the template using the `with` clause (i.e. `{% include 'template.html' with {'var': var} %}`).
Douglas McDonald
CC BY-SA 3.0
2015-02-01T09:56:58.323Z
Interesting consideration, I'll look into that. Thanks.
Paul
CC BY-SA 3.0
2015-01-31T14:32:47.333Z
2015-01-31T15:12:21.203Z
2015-01-31T15:12:21.203Z
Douglas McDonald
<p>Craft 101: I have entries with many different fields, so I'm doing something like this:</p>

<pre><code>{% set var = craft.entries.section('foo').slug('bar').first() %}

{% if var %}
  {{ var.field1 }}
  {{ var.field2 }}
  {{ var.field3 }}
  etc...
{% endif %}
</code></pre>

<p>Does Craft fetch the entry only once or every time the variable is called? I'm guessing only once but I'm not too sure, and some entries have 50+ fields.</p>

<database><elementcriteriamodel><performance>
Many entry fields, only one db request?
# Many entry fields, only one db request?

(Asked by: username_0 on 2015-01-31)

Craft 101: I have entries with many different fields, so I'm doing something like this:
{% set var = craft.entries.section('foo').slug('bar').first() %}

{% if var %}
  {{ var.field1 }}
  {{ var.field2 }}
  {{ var.field3 }}
  etc...
{% endif %}

Does Craft fetch the entry only once or every time the variable is called? I'm guessing only once but I'm not too sure, and some entries have 50+ fields.

**Tags:** database, elementcriteriamodel, performance

---

## 1 answers

### Answer by username_1 on 2015-01-31. Score: 4 | Accepted answer

The statement is only executed once.

You are essentially creating an [elementCriteriaModel](http://buildwithcraft.com/docs/templating/elementcriteriamodel), returning the first element using the `.first()` parameter (which in this case is an [Entry Model](http://buildwithcraft.com/docs/templating/entrymodel)), and storing it in a variable; which you then can use to access your field values.

If you want to further increase performance you can wrap the whole block in a [cache](http://buildwithcraft.com/docs/templating/cache) tag, which will speed up subsequent requests.

> **Comments:**
> **username_0**: Thanks! I'm guessing this also applies if said variables are actually called further down in other "included" templates?  
> **username_1**: Yes and no — it applies anywhere that the variable is still in scope. Variables defined in a template do not automatically carry over to included templates. You would have to explicitly include the template using the `with` clause (i.e. `{% include 'template.html' with {'var': var} %}`).  
> **username_0**: Interesting consideration, I'll look into that. Thanks.  

---

