Php if for beginner

Hello, i’m setting up my custom starterkit for kirby, i added several panel field to manage metadats, all works fine, but do you have any idea how i could enhance the following code:

<?php if($page->isHomepage()): ?>  
  <title><?php echo $site->title()->html() ?> | <?php echo $page->title()->html() ?></title>
<?php else: ?>
    <?php if($page->metatitle() != ''): ?>
      <title><?php echo $page->metatitle()->html() ?> | <?php echo $site->title()->html() ?></title>
    <?php else: ?>
      <title><?php echo $page->title()->html() ?> | <?php echo $site->title()->html() ?></title>
   <?php endif ?>
<?php endif ?>
    
<?php if($page->description() != ''): ?>
  <meta name="description" content="<?php echo html($page->description()) ?>" />
<?php else: ?>
  <meta name="description" content="<?php echo html($site->description()) ?>" />
<?php endif ?>
    
<?php if ($page != $article) :?>
    <?php if($page->keywords() != ''): ?>
      <meta name="keywords" content="<?php echo html($page->keywords()) ?>" />
    <?php else: ?>
      <meta name="keywords" content="<?php echo html($page->tags()) ?>" />
    <?php endif ?>
    
<?php else: ?>
    
    <?php if($page->keywords() != ''): ?>
      <meta name="keywords" content="<?php echo html($page->keywords()) ?>" />
    <?php else: ?>
      <meta name="keywords" content="<?php echo html($site->keywords()) ?>" />
    <?php endif ?>
    
<?php endif ?>
    
<?php if($page->author() != ''): ?>
    <meta name="author" content="<?php echo html($page->author()) ?>" />
<?php else: ?>
    <meta name="author" content="<?php echo html($site->author()) ?>" />
<?php endif ?>
 <meta name="publisher" content="<?php echo html($site->author()) ?>" />
 <meta name="copyright" content="<?php echo html($site->author()) ?>" />

Using the Kirby e() helper, you could shorten your code, e.g.

<?php if($page->description() != ''): ?>
  <meta name="description" content="<?php echo html($page->description()) ?>" />
<?php else: ?>
  <meta name="description" content="<?php echo html($site->description()) ?>" />
<?php endif ?>

would become

<meta name="description" content="<?php e($page->description()->isNotEmpty(), $page->description(), $site->description()) ?>" />
1 Like

Hey, thanks a lot, that is exactly what i was looking for!
Another issue, i said that everything works fine, but thats not totally the case, in this bit of code, how can i display tags if no keywords in the case of an article, but display site keywords if no page keywords for all other pages?

Also, how can i display a value from a select panel field? I really dig into the cheatsheet but can’t find anything…

Thanks in advance!

Unfortunately, you don’'t say what exactly is going wrong, but this should give you what you want:

<?php if ($page->intendedTemplate() ==  'article') :?>
   <meta name="keywords" content="<?php e($page->keywords()->isNotEmpty(), $page->keywords()->html(), $page->tags()->html()) ?>" />
<?php else: ?>
  <meta name="keywords" content="<?php e($page->keywords()->isNotEmpty(), $page->keywords()->html(), $site->keywords()->html()) ?>" />
<?php endif ?>

I don’t know if your articles are called article.txt or something else, since your $article in the code above is not defined, so you need to adapt this if necessary.

I’m getting the rest of the page blank now

You got a typo there, an extra h character in the PHP opening tag, should be

<?php if($page->testimonialLink() != ''): ?>
<h5 class="testimonial__client-site">
      <a href="<?php echo $page->testimonialLink() ?>" class="testimonial__client-link"><?php echo $page->testimonialSite() ?></a>
</h5>
        
<?php else: ?>
    <h5 class="testimonial__client-site"><?php echo $page->testimonialSite() ?></h5>
<?php endif ?>
1 Like