Kirbytag doesn’t work on the main page

Hello there.

I’m using a slideshow script on my blog. The code is simple:

<div class="fotorama">
 <img src="image1.jpg">
 <img src="image2.jpg">
 <img src="image3.jpg">
</div>

I couldn’t use Kirby’s (image: …) tag as if it adds the <figure> tag into the code and my slideshow script doesn’t work properly. So I’ve created a kirbytag (fotorama: …) which creates a pure <img> tag w/o <figure>:

<?php
kirbytext::$tags['fotorama'] = array(
  'html' => function($tag) {
    return '<img src="' . image($tag->attr('fotorama'))->url() . '">';
  }
);

Now to call the slideshow I’m using something like this:

<div class="fotorama">
 (fotorama: image1.jpg)
 (fotorama: image2.jpg)
 (fotorama: image3.jpg)
</div>

It works great on a single blog post page, but when I open the blog with a list of entries I receive an error:

Whoops\Exception\ErrorException thrown with message "Method Kirbytext::__toString() must not throw an exception, caught Error: Call to a member function url() on null"

I guess the problem is with url() parameter — the system just couldn’t get it from the page with list of blog entries.

So the question is how I’m supposed to pass the url() on the main page?

Please help :slight_smile:

You can get the page with $tag->page():

<?php
kirbytext::$tags['fotorama'] = array(
  'html' => function($tag) {
    $image = $tag->page()->image($tag->attr('fotorama'));
    if($image):
      return '<img src="' . $image->url() . '">';
    else:
      return '';
    endif;  
  }
);

Wouldn’t it make more sense to pass all images to the tag as a list instead of using three separate tags?

(fotorama: image1.jpg, image2.jpg, image3.jpg)
2 Likes

Thanks for reply! It’s finally worked :slight_smile:

You’re right, this way is much better. Thanks for the tip!

Another advantage of that is that you can wrap the div around the images within the tag and the user does not have to deal with that in the text field.

Oh, this one even better. I didn’t even think about it ))