Custom kirbytag issue

I’m trying to create a custom kirbytag. It’s an alternative image tag, with two parameters: the image and a css class.

Here is the code of the php tag file:

    kirbytext::$tags['img'] = array(
     'attr' => array(
        'text'
      ), 
     'html' => function($tag) 
      {
        
        $url      = $tag->page()->url() . '/' . $tag->attr('img');
        $class    = $tag->attr('class');
        
        return "<img src='" . $url . "'" .
                  " class='" . $class . "'>";
      }
    );

And here is how I use the tag inside the text field:

(img: amora.jpg class:img-fluid)

Unfortunatly the result when rendered is this:

<img src="http://localhost/mysite/operation-mindcrime/amora.jpg class:img-fluid" class="">

As you may see, the class name is merged with the file name… :frowning:

I had this issue a couple of days ago, certain Kirby tags were outputting HTML in pre tags.

I’ve still no idea what’s causing it as I’m using an almost identical tag elsewhere on the site and that’s working fine…

I’ll take another look over the weekend to try and figure it out. If I find the problem I’ll let you know.

The class attribute is missing, you are using a text attribute but no class.

'attr' => array(
        'text',
        'class'
      ),

In the future, please wrap code blocks within three backticks on a separate line before and after the code to make stuff more readable. Thank you.

As usual, thank you @texnixe.