Creating custom Kirbytags to display Site ($site) values

Hi there.

I’m totally new to Kirby and so far I think it’s great. I’ve managed to solve pretty much all my issues so far with trial and error plus the resources I can find online.

One thing is alluding me.

How can I display a value I have declared in my site blueprint via a custom kirbytag?

I have created my plugin following the instructions in the Kirby documentation:

Kirby::plugin('marblecake/extra-ktags', [
    'tags' => [
        'companyno' => [
            'html' => function($tag) {
                return $site->conumber()->kirbytext();
            }
        ]
    ]
]);

Then, in my page editor in the Kirby Panel I have put in:

(companyno: html)

But this simply displays the text string “(companyno: html)” on the page. If I change return $site->conumber()->kirbytext(); to a simple text string it works fine. How do I access the value I have declared for conumber?

Also, would it be possible to declare a Kirbytag using this kind of structure that would access various fields I have declared for the site blueprint?

(companydata: companyno)
(companydata: companyaddress1)
(companydata: companytel)
etc.

Sorry if this seems really basic but I’m trying to get my head around it all fits together!

Many thanks

Owen

I didn’t test it, but you can use it like this:

Usage:

(companydata: fieldname)

Code:

Kirby::plugin('marblecake/extra-ktags', [
    'tags' => [
        'companydata' => [
            'html' => function($field) {
                return $site->{$field}()->kirbytext();
            }
        ]
    ]
]);

For ex:

To get conumber from $site

(companydata: conumber)

You can also return a snippet and do your logic in there if you want greater control over the output.

A real world example of this would be my HTML5 Video tag.

This looks like the perfect solution, except for the fact that it throws an error:

Method name must be a string on line 6:
return $site->{$field}()->kirbytext();

I tried substituting in conumber() for {$field}() but then I get the same result as before (it simply displays the string “(companydata: conumber)”).

Any ideas?

Should be $field->value We usually use the $tag variable here, makes it easier.

  'html' => function($tag) {
                return $site->{$tag->value}()->kirbytext();
            }

Thanks for that. At the moment I’m looking for a simple solution to pull in common text field values and display them (allowing the user to change them globally by simply altering one field in the Panel). However, your HTML5 Video tag is certainly something that will be useful to me soon, so thanks for sharing!

Thanks for that. I’ve changed as suggested but still no dice. For clarification, in the site.yml I have this:

fields:
  conumber:
    label: Company Reg No
    type: number

Then, in my plugin file I have this:

Kirby::plugin('marblecake/extra-ktags', [
    'tags' => [
        'companydata' => [
            'html' => function($tag) {
                return $site->{$tag->value}()->kirbytext();
            }
        ]
    ]
]);

And, finally, in my page content in Kirby Panel I have written:

(companydata: conumber)

All of this simply outputs the string “(companydata: conumber)” to the page.

I very much appreciate your help on this!

I had an error I already corrected above, no () after value. And $site is not known either, so we need

Kirby::plugin('marblecake/extra-ktags', [
    'tags' => [
        'companydata' => [
            'html' => function($tag) {
                return site()->{$tag->value}()->kirbytext();
            }
        ]
    ]
]);
1 Like

Perfect! I noticed you’d corrected the extra parentheses but it still didn’t work.

The use of site() instead of $site totally fixed it. Many thanks for your help!