Structured field content
Kirby's content fields can contain any plain text format. But that does not have to be one-dimensional data like a title or some text. One way to further structure plain text is called YAML - a human-readable data structure syntax.
With the help of YAML our content can be even more powerful.
Example: nice addresses
If you are building a contact page and you've got just one address, so far you could simply structure the content like this for example…
But what happens, when you got more than just one address? Maybe your company has various offices all around the world or you are just very rich and you want to show off with the addresses of your houses on the Bahamas, New York, LA and in Monaco.
One way would be to add subpages for each address and then build a foreach loop with
$page->children() to display each address. But that would be quite an oversized solution for just showing more than one address.
You could also add multiple fields like this:
But that wouldn't be very easy to read and maintain.
With YAML syntax you can add more than just one address in one single field:
Looks pretty tidy, right? To see the full potential of YAML syntax, check out the YAML site or the YAML Wikipedia entry
How to access data
In your template you can parse YAML in content fields with the
This will give you a nice associative array:
dump($array) to inspect the content of any array or object.
Working with such an array is not difficult, but we can add more Kirby flavor to it, to keep the template syntax more in line with the rest of our APIs.
toStructure method parses the YAML content and converts it into a nice Kirby-style object.
This works also great in connection with Microformats
As you can see this will give you a lot more control and structure for your content. It's not limited to addresses though:
A list of profiles
Your Team Members
…or whatever structured data you need!