Writing conditional head items

Okay, so I’m going it alone in some places and exploring how to logically deal with head items.
Some blueprints have the summary field which is a short intro, but nearly every blueprint has the text field. I’m also trying to strip out any quote marks in text.

I know this doesn’t use any ternary logic, but what would a better way to write this look like?

<?php if ($page->isHomePage()) : ?>
	<meta name="description" content="<?= $site->seodescription() ?>">
	<?php elseif ($page->summary()->exists()) : ?>
		<meta name="description" content="<?= $page->parent()->title() ?>: <?= $page->summary()->short('100')->toString() ?>">
	<?php else : ?>
		<meta name="description" content="<?= $page->parent()->title() ?>: <?= $page->text()->short('100')->toString() ?>">
<?php endif ?>

(I actually want to use the same logic to determine the SEO image: Use the site logo if it’s the homepage, otherwise use any uploaded page image instead.)

I think this is a great opportunity for me to learn how to do things the correct way in PHP from here on out. Thanks for your time, in advance!

If you don’t want to use one of the plugins, I’d create page methods that contain the logic, so that you don’t have to deal with all the logic in the header or metatag snippets. e.g. a $page->seoDescription() method, etc. then you metatag snippet will be reduced to

<meta name="description" content="<?= $page->seoDescription() ?>">
Kirby::plugin('my/page-methods', [
  'pageMethods' => [
    'seoDescription' => function () {
      if ($this->isHomePage()) {
        return site()->seoDescription();
      } elseif ($this->summary()->isNotEmpty()) {
        return $this->parent()->title() . ': ' . $this->summary()->short('100');
      } else {
        return $this->parent()->title() . ': ' . $this->text()->short('100');
1 Like

I understand the usual requirement of moving functions out of templates into methods and controllers, I think, but in this case, I want all of the logic in the header.php snippet for easy management. Using controllers and methods and routes are all still a little in upper (unreachable) headspace for me yet.

I did take my above code and make it work properly, though:

<?php if ($page->isHomePage()) : ?>
	<meta name="description" content="<?= $site->seodescription() ?>">
<?php elseif ($page->summary()->exists()) : ?>
	<meta name="description" content="<?= ucfirst($page->parent()) ?>: <?= $page->summary()->short('100') ?>">
<?php elseif ($page->about()->exists()) : ?>
	<meta name="description" content="<?= $page->about()->short('100') ?>">
<?php elseif ($page->text()->exists()) : ?>
	<meta name="description" content="<?= ucfirst($page->parent()) ?>: <?= $page->text()->short('100') ?>">
<?php else : ?>
<?php endif ?>

<?php if ($page->isHomePage()):  ?>
	<meta property="og:image" content="/apple-touch-icon.png">
<?php elseif ($page->hasImages()) : ?>
	<meta property="og:image" content="<?= $page->image()->url() ?>">
<?php else : ?>
<?php endif ?>

Finding the ucfirst tool was helpful, and it took a bit to realize that it had to be first with the code inside a parens instead of after a long chain of ->thing() instead.
My logic is still poorly written, though it does feel like an improvement. If I progress further I will likely realize that using those methods and routes are smarter, or it will click and I’ll start realize the power of the work others have already done with plugins and tutorials. Either way, I’m getting there. :wink:

Thanks for your help, Sonja.