Skip to content

Kirby 3.6.6

Fields

Content within text files can be structured with fields. You can add as many fields to a content file as your content model requires. Each text file can contain its very own set of fields. Together with the flexible folder structure you get a NoSQL-like data store with literally unlimited possibilities.

Adding fields manually

If you prefer to write your content files in a text editor, you can separate one field from the next with four dashes on a separate line:

Title: An interesting article
----
Date: 2024-04-25
----
Text:

Really interesting content here...

Each field begins with the fieldname and a colon. The content of a field can be a single or multiple lines of text. You can store plain text, HTML, JSON, YAML or anything else which can be written in plain text in a field.

Fields can be added or removed at any time. All fields are instantly available in Kirby's templates.

When you add fields manually, there is no way to validate what you enter into each field.

Fields in the Panel

Before fields are available to be filled by users in the Panel, they need to be defined for each content model. These field definitions are done in blueprints and give you full control over what sort of content each field can contain.

The Panel comes with many pre-defined field types that help with structuring your content. A full list of available field types and their options can be accessed in the Reference:

There are several plugins with custom field types available.

Naming fields

In general, you are free to name your fields like you want to, but there are two limitations:

  1. You can only use alphanumeric characters and underscores in field names.
  2. If you use field names that are also used by native Kirby methods, your field names will conflict with Kirby's existing methods, e.g. if you give your field the name "image", it will conflict with Kirby's $page->image() method. If you still want to use this name for the field, you will have to call these fields differently in your templates. For details please refer to the template docs.

Accessing fields in templates

Field content is accessible in your templates via the field's name:

<?= $page->title()->html() ?>
<?= $page->text()->kirbytext() ?>
<?= $page->date()->toDate('Y-m-d') ?>

The methods that are chained to the name of the field are field methods that allow you to modify the field content in multiple ways. You can find a full list of available field methods in the Reference.

Further reading

Also make sure to check out our Cookbook recipe on custom content fields.