How to write a custom Kirbytag?

Hello,

I want to learn how to write my own kirby-tag.

I have created the following file /site/plugins/test/index.php with this content:

<?php
Kirby::plugin('test', [
    'tags' => [
        'wikipedia' => [
            'html' => function($tag) {
                return '<a href="http://wikipedia.org">Wikipedia</a>';
            }
        ]
    ]
]);
?>

Now, however, this page is no longer loaded 500 error. What am I doing wrong ?

greetings perry

Your plugin wrapper has the wrong syntax, it must be

Kirby::plugin('yourname/yourplugin', [
    'tags' => [
        'wikipedia' => [
            'html' => function($tag) {
                return '<a href="http://wikipedia.org">Wikipedia</a>';
            }
        ]
    ]
]);

https://getkirby.com/docs/guide/plugins/plugin-basics#kirby-plugin-one-ring-to-rule-them-all

thanks @texnixe

With what word can I then run the kirby-tag? test or wikipedia both are not working.

(wikipedia:)

You can find a few more custom Kirbytag examples here: https://github.com/getkirby/getkirby.com/blob/master/site/plugins/maki/tags.php

(Note that not all of them are necessarily easy to understand because they build upon additional classes etc., but you should nevertheless get a general idea of what is possible with custom tags).

Thank you @texnixe for your help, I slowly understand the Kirby tags basics.
This tag I use to insert "cleaner"a table.

<?php
Kirby::plugin('strukturart/table', [
'tags' => [
    'table' => [
      'attr' =>[
        'table_name'
      ],
        'html' => function($tag) {
          $field = $tag->table_name;
          
          $html = '<table class="kirbytag-table">';

			foreach(page()->$field()->toStructure() as $table) 
			{
				$html .= '<tr>';
				$html .= '<td>';
				$html .= $table->col01()->html();
				$html .= '</td>';
				$html .= '<td>';
				$html .= $table->col02()->html();
				$html .= '</td>';
				$html .= '<td>';
				$html .= $table->col03()->html();
				$html .= '</td>';
				$html .= '</tr>';
			}

			$html .= '</table>';

return $html;

        }
    ]
]
]);


?>

Did you read Kirby cookbook: Columns in Kirbytext?

It may be a solution for you.

That’s a completely different story. Columns is not the same as table layout and won’t work at all for tables.

I still have a question, how can I use the first attribute without naming it?

(table: table_name: table01)

vs

(table: table01)

I think this line: $field = $tag->table_name; must be different.

$tag->value returns what comes after the tag name.

In case of (table: table01) it would return table01.

1 Like

Is there a kirby-tag-collection somewhere ? maybe somebody needs that tag.

I’m not aware of a kirbytags collection. You could post it as a separate post in the "Kirby 3 Solutions"category.