A different approach to the Kirby content() method?

If you work with Kirby you are familiar with the concept of creating fields in a page blueprint and accessing it in your page template. Usually you end up with something like this:

<?= $page->myField() ?>

As long as your field name does not match a reserved word you’re fine. But as soon as your field name has the same name as an existing page method you need to make use of the content() method (and everything is fine again).

<?= $page->content()->myField() ?>

Since I use custom page methods on a regular basis, I prefer to use the content() method for every page field. No matter if there’s a naming conflict or not. In this way it is easier to me to differentiate between page methods and fields.

<?= $page->url() ?>
<?= $page->customPageMethod() ?>
<?= $page->content()->myField() ?>

Unfortunately it requires more typing and (in my opinion) it's a bit harder to read as well.

Introducing a $content variable

Lately I had the idea to create a custom $content variable and use it across all page templates. The right place for additional variables for your page template is a controller. Every page template can have its own controller. But there is also a general site controller which is useful if multiple templates share the same logic.

/site/controllers/site.php

<?php

return function ($page) {
  return [
    'content' => $page->content(),
  ];
};

With this few lines it is now possible to use a $content variable as a replacement of $page->content() across all page template. The page template example from above can be be changed to the following. It might look unusual at first but I think i’ll give it a try.

<?= $page->url() ?>
<?= $page->customPageMethod() ?>
<?= $content->myField() ?>

Please be aware that, as soon as you create a custom controller for a single page type, you need to define the $content variable again. Since it’s just a single line of code, I think it’s not a big deal. If you have more variables you want to share, check out this tutorial about how to create a shared controller setup by Manu Moreale. By the way, with Kirby 5 the shared controller setup will no longer be necessary because the general site controller will automatically be merged.

One more thing …

As Florens mentioned on Mastodon, this approach only works while $page represents the current page, because the variables defined within the controller are only available in the corresponding page template. That’s a valid point you have to keep in mind.

But in combination with custom view modes it still might be a way to go because if you use these view modes consequently it will probably reduce the amount of situations where $page is not the current page. I will try both in combination and might come back with a report of my findings.

Sources


CMS Kirby PHP #Template

What do you think?

Let's discuss on Mastodon

More …

Latest

Drowning in tabs?

Featured

Use Kirby content representations for custom view modes

pageview counter pixel