Craft Pro 3.4.20

New to craft development. I was following the documentation and calling my entries via the suggested method as outlined https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#example.

However I have found that I can access any fields via entry.<fieldName> within my twig templates without setting the entry query, so long as that template has been assigned to that section.

For example, my homepage has a template set in the cp, and I can access all its field entries without any query within the twig template, I just call entry.title like anything else and it works.

My question is this—is this an acceptable method? Are there any performance issues with this?

  • This is perfectly fine and the way it was intended :)
    – Oli
    May 28 '20 at 7:11

the simple answer is: yes it is an acceptable method!

In single sections or in a single entry of a structure or channel, this is totally fine.

You need the entries query if you want to do something like having a list of all recent blog posts on your homepage. So on your index.twig the "entry" object has got all data for the homepage, like title, url and all fields!

Example index.twig:

<h1>{{entry.title}}</h1> <---(the title of your site)

{% set blogPosts = craft.entries().section('blog') %}
{% for entry in blogPosts.all() %}
  {{ entry.title }} <---- (the title of a blogpost)
{% endfor %} 
  • Thank you! This is what I was doing, and now I know its safe. I am assuming if I want to eager load elements for these singles then I would need to do a query. May 29 '20 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.