right now i am checking if a custom field (eg year) is set and if so, echoing it together with a short title. Is there a shorter way for this, maybe by the use of arrays?
We have a phrase here in the UK… “Swings and Roundabouts”… if you re-write this into an array or similar, your still going to end up with the same amount of code somewhere else, probably in a controller or something… in other words, a swing is the same effort as a roundabout, it just looks different
You could move this information into a structure field, and then loop through it but just call the fields title and description rather then specifics of year, client, and status. That way when you loop through it, you don’t have to care how many there are, or what they are called, or if any don’t have a value.
Personally I would just make them all one line each and use the shorthand for echo…
That I think would work, but I always feel it is bad form to echo HTML tags, concatenated with code. It is less readable. (and your link to the docs seems to be wrong).
Thank you all for the quick responses.
Eventhough texnixes solution is shorter, especially when it comes to more custom fields to echo, it is only usable when you want to display $field->key() .
I’m testing kirby3 atm. Does it have other possibilities?
Yes, sure. But if you want to loop through a set of given field that all use the same logic like above, using an array of the fields you need still produces the least overhead (compared to your original code).
Only makes sense if you want to output all fields using the same field method and the same html wrappers, of course, which is usually not the case.
I still don’t get what you want to know here. Kirby 3 has a lot of new possibilities…
Thanks texnixe!
I understand the idea of it, but I can’t figure out how to get the value of ‘label’ from the {$field} array. $page->blueprint()->{$field}()->label()
it returns “Call to a member function label() on null”.
Thanks for your patience
Your syntax is not correct. For example, if you want to get the label of a field called text, you use the field() method and pass the name of the field as a parameter: