Is that code in a template? This means: is that code written in a .php file inside of /site/templates
?
If yes, you’ll need to write it like texnixe wrote above. Kirby won’t have the chance to convert it to the an absolute URL otherwise.
I’ll try to explain the code, starting with your <a href="page-xyz">XYZ</a>
and replacing it bit by bit.
The first thing you want to do is get an absolute url. Therefore you get a handle to the page from which you can let Kirby generate the URL:
// Store the target page into the variable $p
$p = page('page-xyz');
$p
is now a either null
if the page was not found (for any reason), or an object of type Kirby\Cms\Page
, alias simply Page
. There are lots of stuff you can call on an object (in OOP jargon “instance”) of type Page. You’ll find most of them here: $page | Kirby CMS.
One of those methods is url()
which returns you the absolute URL of the given page.
This would print the absolute url of the page stored in $p
to the current HTML:
<?php echo $p->url() ?>
You can abbreviate this to (it’s equivalent to):
<?= $p->url() ?>
If you put it into the HTML we started with it would become:
<?php
// Store the target page into the variable $p
$p = page('page-xyz');
?>
<a href="<?= $p->url() ?>">XYZ</a>
The problem is, however, that $p
might be null
, like I mentioned above. That means that it does not contain a reference to the page. This could be because you might have deleted the page, for example, or changed its id
.
Calling any method on null
causes an error (a “Null Pointer Exception”). Therefore we need to make sure our variable $p
is not null
:
<?php
// Store the target page into the variable $p
$p = page('page-xyz');
// make sure $p is not null
if($p !== null) {
// print the absolute page URL
echo $p->url();
}
The result of an assignment is the assigned value: the result of the operation $p = page('page-xyz')
is actually also the page (or null
) that gets assigned to $p
. Therefore you can write both the assignment and the if clause on the same line:
<?php
// Store the target page into the variable $p and make sure it's not null
if(($p = page('page-xyz')) !== null) {
// print the absolute page URL
echo $p->url();
}
null
is a “falsy” value: if you put it into an if
clause it’s just like having false
. Therefore you don’t really need to check if $p
is exactly null
, for us it’s enough to know that it isn’t any of the falsy values (like false
, null
, 0
, ''
, and others), but a “truthy value” (everything else). Therefore it’s enough to write
<?php
// Store the target page into the variable $p and make sure it's not falsy
if($p = page('page-xyz')) {
// print the absolute page URL
echo $p->url();
}
if
statements can be written either with the standard syntax like above with curly braces or with the alternative syntax:
<?php
// Store the target page into the variable $p and make sure it's not falsy
if($p = page('page-xyz')):
// print the absolute page URL
echo $p->url();
endif;
This alternative syntax is especially handy when you combine it with HTML, because it integrates better than the curly braces:
<?php if($p = page('page-xyz')): ?>
<?= $p->url(); ?>
<?php endif; ?>
If we put your HTML back in, it becomes:
<?php if($p = page('page-xyz')): ?>
<a href="<?= $p->url() ?>">XYZ</a>
<?php endif ?>
@texnixe went one step further and also replaced the XYZ
with the actual title of the linked page.