Custom tag add attribut

Hello,

I try to create a custom tag,
but I do not unterstand how to declare an
attribut.

(gallery: gallery_1 grid:2)

<?php

kirbytext::$tags['gallery'] = array(
  'attr' => array(
    'grid'
  ),


 'html' => function($tag) {

    $grid    = $tag->attr('grid');
    $html = '<ul class=$grid>';
    $field = $tag->attr('gallery');
    foreach($tag->page()->$field()->toStructure() as $gallery) {
      $html .= '<li>';
      $html .= '<img src=';
      $html .= $tag->page()->url();
      $html .= '/';
      $html .= $gallery->gallery_item();
      $html .= '>';
      $html .= '</li>';
    }

    $html .= '</ul>';

    return $html;
  }
);

?>

greetings perry

sorry

^._.^

<?php

kirbytext::$tags['gallery'] = array(
  'attr' => array(
    'grid'
  ),


 'html' => function($tag) {

    $grid    = $tag->attr('grid');
    $html = '<ul class='.$grid.'>';
    
    $field = $tag->attr('gallery');
    foreach($tag->page()->$field()->toStructure() as $gallery) {
      $html .= '<li>';
      $html .= '<img src=';
      $html .= $tag->page()->url();
      $html .= '/';
      $html .= $gallery->gallery_item();
      $html .= '>';
      $html .= '</li>';
    }

    $html .= '</ul>';

    return $html;
  }
);

?>

1 Like

You can use the 2nd parameter of $tag->attr() to define a fallback/default value:

$grid = $tag->attr('grid', 'fallback value');

So in your case it could be $grid = $tag->attr('grid', 'my-grid-class'); that would give the content creator the option to change the grid class in the markdown (gallery: something grid: another-grid-class).

@Pascalmh thanks for your answer.
Now it works.