Exactly, this code declare the $items variable, it can then be used, e.g. in a loop, or to further modify it:
foreach ($items as $item) {
echo $item->title();
}
This is them same as directly using
foreach ($pages->listed() as $item) {
echo $item->title();
}
Assigning a value to a variable first makes sense if you are going to use it in multiple places, so you don’t have to keep repeating yourself. It also keeps your code clean and short if you define your variables in a controller and then use them in templates and snippets.
Also, the longer your definition, it makes sense to use a variable instead of putting this into a foreach loop:
No, a variable you declare in a controller is only valid for templates that use this controller (projects.php controller is use for projects.php template, remember, Kirby works with matching filenames).
No, these are both foreach loops that loop through something “iterable” (an array, a collection in this case), where the first part is the iterable (no matter if in form of a variable or not) and the thing after the as is the (sort of placeholder) variable each element in that array/collection is assigned to.
some variable names are already set by kirby, so you most likely can figure out by name, or by taking a look in the reference, about the content of the variable, other than that, it always depends on you and your data…
<?php
// $pages usually multiple
// $page->children() -- as children suggests it's a collection
// $file -- usually one result (so no foreach)
// $files -- usually a collection of files..
/* the amount of results in a foreach doesn't really matter as much...
since it'll work with 1 or 100 results...
*/
With your blog example you most likely need to add pagination as well, so as you posted it might still need adjustments. There’s a tutorial for that here too:
For beginners it’s a foreach is easy to understand, i guess it’s much more difficult to get the idea of array, objects, yaml and whatsoever. Like when your content is not in a format you wish to use…
That is exactly my problem! Same with Javascript and in a way with other naming conventions. Something like “items as item” is just too universal, too neutral for me. I immediately ask myself " which items?" Same with “name” or “text”. Whose name? And a text field in a blueprint that is like
type: text
label: Text
text: text
makes my brain crash.
To me those namings sounds like a system did that, not a human, not me. I know it is… childish and I try to overcome that, but it is hard
So for now I try to help myself with a little bit of specificity in that names. “Blogarticle”, not just “article”. “Gotcha-items” not jut plain “items” and so on. And I know, that I could re-use “article” if it is not so specific, but for now, I don’t care so much about “dry”. I probably have a template to many in my folder, but that way I can quickly know what is for what. Also I very often want to make pages or any other kind of element a little different from each other, mostly because of content reasons. So a blog-listing page will probably be a little different from a listing-page of some other kind.
Strangely I love the idea of snippets and use that as “partials” in the way I build my html, using the “kits” thing in CodeKit.