I get an error using image()

Hey guys,

yesterday is started playing around with Kirby and did the tutorial on YouTube by Bastian Allgeier and everything worked fine. Today I wanted to start building my own site and my first step caused an error.

I want to put a logo inside the html <a> tag. But this image() shortcode seems to be wrong, because I get an error page when it is inside. I don’t want to insert a normal <img> tag. Is this the wrong line of code?

<a class="logo" href="<?= $site->url() ?>"><?= image('media/logo.svg')->url() ?></a>

This is in the header.php snippet.

what does the error page say? (do you have debug mode turned on?)

Error thrown with message “Call to a member function url() on null”

Error message of the Kirby Debugger.

The image helper cannot be used like this. I don’t think it’s a good idea to put files into the media folder, or is that a folder in content your are using?

In any case, never call a method like url() without making sure the object, in this case the image, exists.

Sorry, I have nearly no idea what I am doing :sweat_smile: Just copying lines of code and mostly it works. Okay, then I put the logo graphic in the content folder and link it with standard <html>

Edit: Okay, bad idea. Of course now it doesn’t work on subpages. How should I insert a image in the header snippet?

You can put the logo into an /assets/images/ folder.

Then you can grab the file using the asset() helper.

<?php
$logo = asset('/assets/images/logo.png');
if ($logo->exists()) {
  echo $logo->url();
}

Or using your code from above:

<?php $logo = asset('/assets/images/logo.png'); ?>
<?php if ($logo->exists()) : ?>
<a class="logo" href="<?= $site->url() ?>"><?= $logo ?></a>
<?php endif ?>

Yes! Now it is working – thank you very much! :slightly_smiling_face: