Where should the .all() be placed in Craft 3
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm confused, I have ended up with 2 similar queries in a template after converting them from Craft 2.
% set events = craft.entries()
.section('events')
.eventDate('>' ~ now
% for entry in events %
...
% endfor %
and
% set entries = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entries.all() %
...
% endfor %
Both work without any errors reported but which one is better or correct?
craft3 query queries
add a comment |Â
up vote
1
down vote
favorite
I'm confused, I have ended up with 2 similar queries in a template after converting them from Craft 2.
% set events = craft.entries()
.section('events')
.eventDate('>' ~ now
% for entry in events %
...
% endfor %
and
% set entries = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entries.all() %
...
% endfor %
Both work without any errors reported but which one is better or correct?
craft3 query queries
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm confused, I have ended up with 2 similar queries in a template after converting them from Craft 2.
% set events = craft.entries()
.section('events')
.eventDate('>' ~ now
% for entry in events %
...
% endfor %
and
% set entries = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entries.all() %
...
% endfor %
Both work without any errors reported but which one is better or correct?
craft3 query queries
I'm confused, I have ended up with 2 similar queries in a template after converting them from Craft 2.
% set events = craft.entries()
.section('events')
.eventDate('>' ~ now
% for entry in events %
...
% endfor %
and
% set entries = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entries.all() %
...
% endfor %
Both work without any errors reported but which one is better or correct?
craft3 query queries
asked Aug 13 at 20:41
Paul Frost
522311
522311
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Both approaches are valid. In your first example, you could have moved .all()
into the for-loop; and in the second example, you could have moved .all()
out of the for-loop, and into the % set entries = ... %
statement.
Until you call .all()
, you are working with an entry query, and after you call .all()
, youâÂÂre working with an array of the actual entries (the query results).
So which approach is better depends on what else youâÂÂre doing in your template.
For example, if you need to output the total number of entries, you could do this:
% set entryQuery = craft.entries()
.section('news')
%
% for entry in entryQuery.all() %
...
% endfor %
Total: entryQuery.count()
But it would result in two database queries getting executed â once to get the entries for the for-loop, and a second query to get the total matching entries.
If you know youâÂÂre going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:
% set entries = craft.entries()
.section('news')
.all()
%
% for entry in entries %
...
% endfor %
Total: length
Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have events
(an array of actual event entries) and entries
(an entry query).
I would say that if you are not going to call .all()
when defining your entries
variable, then you might want to rename that to either entryQuery
or query
, just so itâÂÂs easier to remember what sort of variable youâÂÂre working with later on in the template.
% set entryQuery = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entryQuery.all() %
...
% endfor %
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
add a comment |Â
up vote
2
down vote
They are both correct.
In your first example, events
is an array of entries. In your second example, entries
is an Element Query. As soon as you apply the .all()
to your Element Query, it converts that into an array of entries.
The only two things you need to know about .all()
is:
- It must exist (or use
.one()
) - It must be last
That's it!
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Both approaches are valid. In your first example, you could have moved .all()
into the for-loop; and in the second example, you could have moved .all()
out of the for-loop, and into the % set entries = ... %
statement.
Until you call .all()
, you are working with an entry query, and after you call .all()
, youâÂÂre working with an array of the actual entries (the query results).
So which approach is better depends on what else youâÂÂre doing in your template.
For example, if you need to output the total number of entries, you could do this:
% set entryQuery = craft.entries()
.section('news')
%
% for entry in entryQuery.all() %
...
% endfor %
Total: entryQuery.count()
But it would result in two database queries getting executed â once to get the entries for the for-loop, and a second query to get the total matching entries.
If you know youâÂÂre going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:
% set entries = craft.entries()
.section('news')
.all()
%
% for entry in entries %
...
% endfor %
Total: length
Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have events
(an array of actual event entries) and entries
(an entry query).
I would say that if you are not going to call .all()
when defining your entries
variable, then you might want to rename that to either entryQuery
or query
, just so itâÂÂs easier to remember what sort of variable youâÂÂre working with later on in the template.
% set entryQuery = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entryQuery.all() %
...
% endfor %
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
add a comment |Â
up vote
4
down vote
accepted
Both approaches are valid. In your first example, you could have moved .all()
into the for-loop; and in the second example, you could have moved .all()
out of the for-loop, and into the % set entries = ... %
statement.
Until you call .all()
, you are working with an entry query, and after you call .all()
, youâÂÂre working with an array of the actual entries (the query results).
So which approach is better depends on what else youâÂÂre doing in your template.
For example, if you need to output the total number of entries, you could do this:
% set entryQuery = craft.entries()
.section('news')
%
% for entry in entryQuery.all() %
...
% endfor %
Total: entryQuery.count()
But it would result in two database queries getting executed â once to get the entries for the for-loop, and a second query to get the total matching entries.
If you know youâÂÂre going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:
% set entries = craft.entries()
.section('news')
.all()
%
% for entry in entries %
...
% endfor %
Total: length
Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have events
(an array of actual event entries) and entries
(an entry query).
I would say that if you are not going to call .all()
when defining your entries
variable, then you might want to rename that to either entryQuery
or query
, just so itâÂÂs easier to remember what sort of variable youâÂÂre working with later on in the template.
% set entryQuery = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entryQuery.all() %
...
% endfor %
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Both approaches are valid. In your first example, you could have moved .all()
into the for-loop; and in the second example, you could have moved .all()
out of the for-loop, and into the % set entries = ... %
statement.
Until you call .all()
, you are working with an entry query, and after you call .all()
, youâÂÂre working with an array of the actual entries (the query results).
So which approach is better depends on what else youâÂÂre doing in your template.
For example, if you need to output the total number of entries, you could do this:
% set entryQuery = craft.entries()
.section('news')
%
% for entry in entryQuery.all() %
...
% endfor %
Total: entryQuery.count()
But it would result in two database queries getting executed â once to get the entries for the for-loop, and a second query to get the total matching entries.
If you know youâÂÂre going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:
% set entries = craft.entries()
.section('news')
.all()
%
% for entry in entries %
...
% endfor %
Total: length
Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have events
(an array of actual event entries) and entries
(an entry query).
I would say that if you are not going to call .all()
when defining your entries
variable, then you might want to rename that to either entryQuery
or query
, just so itâÂÂs easier to remember what sort of variable youâÂÂre working with later on in the template.
% set entryQuery = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entryQuery.all() %
...
% endfor %
Both approaches are valid. In your first example, you could have moved .all()
into the for-loop; and in the second example, you could have moved .all()
out of the for-loop, and into the % set entries = ... %
statement.
Until you call .all()
, you are working with an entry query, and after you call .all()
, youâÂÂre working with an array of the actual entries (the query results).
So which approach is better depends on what else youâÂÂre doing in your template.
For example, if you need to output the total number of entries, you could do this:
% set entryQuery = craft.entries()
.section('news')
%
% for entry in entryQuery.all() %
...
% endfor %
Total: entryQuery.count()
But it would result in two database queries getting executed â once to get the entries for the for-loop, and a second query to get the total matching entries.
If you know youâÂÂre going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:
% set entries = craft.entries()
.section('news')
.all()
%
% for entry in entries %
...
% endfor %
Total: length
Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have events
(an array of actual event entries) and entries
(an entry query).
I would say that if you are not going to call .all()
when defining your entries
variable, then you might want to rename that to either entryQuery
or query
, just so itâÂÂs easier to remember what sort of variable youâÂÂre working with later on in the template.
% set entryQuery = craft.entries()
.section('news')
.orderBy('postDate desc')
.limit('4')
.with(['imageGallery'])
%
% for entry in entryQuery.all() %
...
% endfor %
answered Aug 13 at 21:24
Brandon Kelly
28.5k45104
28.5k45104
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
add a comment |Â
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Damn, tied... you clearly put in a bit more effort. :)
â Lindsey Dâ¦
Aug 13 at 21:27
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
â Paul Frost
Aug 14 at 8:07
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
Yeah, weâÂÂre slowly working to add better examples to the docs. I just rewrote the Element Queries page to hopefully be a little more clear, and with more usable examples.
â Brandon Kelly
Aug 14 at 14:08
add a comment |Â
up vote
2
down vote
They are both correct.
In your first example, events
is an array of entries. In your second example, entries
is an Element Query. As soon as you apply the .all()
to your Element Query, it converts that into an array of entries.
The only two things you need to know about .all()
is:
- It must exist (or use
.one()
) - It must be last
That's it!
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
add a comment |Â
up vote
2
down vote
They are both correct.
In your first example, events
is an array of entries. In your second example, entries
is an Element Query. As soon as you apply the .all()
to your Element Query, it converts that into an array of entries.
The only two things you need to know about .all()
is:
- It must exist (or use
.one()
) - It must be last
That's it!
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
add a comment |Â
up vote
2
down vote
up vote
2
down vote
They are both correct.
In your first example, events
is an array of entries. In your second example, entries
is an Element Query. As soon as you apply the .all()
to your Element Query, it converts that into an array of entries.
The only two things you need to know about .all()
is:
- It must exist (or use
.one()
) - It must be last
That's it!
They are both correct.
In your first example, events
is an array of entries. In your second example, entries
is an Element Query. As soon as you apply the .all()
to your Element Query, it converts that into an array of entries.
The only two things you need to know about .all()
is:
- It must exist (or use
.one()
) - It must be last
That's it!
answered Aug 13 at 21:26
Lindsey Dâ¦
20k43686
20k43686
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
add a comment |Â
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
â Paul Frost
Aug 14 at 7:55
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcraftcms.stackexchange.com%2fquestions%2f27284%2fwhere-should-the-all-be-placed-in-craft-3%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password