Outputting specific page URL

Hi,

I’ve a template that lives inside the templates folder. The name of this template is product.php.

This code <a class="button-two" href="/kontakt"><?php echo $page->mehr();?> </a> will output the text “Mehr dazu” and will take the visitor to the kontakt page. However, I’m not sure if this is the correct Kirby way to make the button link to the kontakt page and what do I need to add to make it conform to the Kirby standard. Your feedback is much appreciated.

There’s nothing wrong in using plain old html. The only problem could be that the address of the contact page might change in the future, i.e if an editor decides “I like /kontakte” better and changes the slug via panel.

Choose page in panel

The most robust solution would probably be to have this content be customizable via panel, with a pages field:

blueprint:

fields:
  contact_link:
    label: Contact page
    type: pages
    multiple: false

template:

<?php if($contact = $page->contact_link()->toPage()): ?>
    <a class="button-two" href="<?= $contact->url() ?>">
        <?php echo $page->mehr();?>
    </a> 
<?php endif ?>

The panel would then save the contact page UUID in the content file, which never changes, and even if the contact page slug changes it would continue working.


Search for page

A bit less involved would be searching for the contact page (for example by template, and expecting the page to be always on the top level):

template:

<?php if($contact = $pages->findBy('template', 'contact')): ?>
    <a class="button-two" href="<?= $contact->url() ?>">
        <?php echo $page->mehr();?>
    </a> 
<?php endif ?>

Fetch page by slug

if you’re sure the slug of the contact page never changes, you could leave it as html, but it might still be a good idea to fetch the page (via the page() helper), in case you deploy the site in different subdirectories (imagine wip.learn79.com/clientname/sitename).

template:

<?php if($contact = page('kontakt')): ?>
    <a class="button-two" href="<?= $contact->url() ?>">
        <?php echo $page->mehr();?>
    </a> 
<?php endif ?>

Fetch by manually set UUID

You could also imagine to manually set an UUID in the contact page content file:

Uuid: contact

---- 

Title: Kontakt

----

Text: more stuff, etc

This way you could fetch the page via it’s UUID, which at that point never changes:

<?php if($contact = page('page://contact')): ?>
    <a class="button-two" href="<?= $contact->url() ?>">
        <?php echo $page->mehr();?>
    </a> 
<?php endif ?>

Many thanks for this detailed answer.