Use a controller variable in a custom kirbytag

Hi there,

I have written a custom kirbytag that needs to access a variable that is set up in a controller. However, when I try to use this variable in my kirbytag snippet plugin, it lets me know

Undefined variable: foo

How can I use such a variable in the kirbytag snippet plugin?

Thanks!

1 Like

Hm, the Kirbytag inside your plugin doesn’t know anything about your controller or the variables inside it.

Do you want to get the data from a particular controller?

Yes, exactly. I have a controller home.php and would like certain kirbytags that appear on text blocks of my home page to work with data from this controller at the time of their parsing.

I’d try and require the controller in the kirbytag code:

$controller = kirby()->controller('controllername');

You might have to pass the objects you need in the controller as array of arguments.

How do I do this? I would need the page object, but how can I even access this from within a kirbytag?

$tag->parent()

Gives you the page context for the page where the kirbytag is used.

Ok and how do I pass the objects as an array? If I try this, then I get an null error for the page object within the controller code:

$controller = kirby()->controller('home', [$tag->parent()]);

Also, if I could easily access the page object from the kirbytag, is there not maybe an easier way to store the data that I need within the page object somehow and then access it from the kirbytag like that? I mean, I don’t have to use a controller, if there are easier ways.

$controller = kirby()->controller('home', ['page' => $tag->parent()]);

I don’t know what you are up to, you asked for the controller…

So if you tell me/us, what you want to achieve, maybe we can find a better solution.

I have a page with a blocks field. Before I do any rendering to the page, I need to loop over all the blocks to retrieve certain information about it and collect it into an array. I do this in the controller (as it is the most appropriate place to do so).

Then, I start rendering all those blocks, including some blocks that include my kirbytag that accesses information from the array that I collected before.

I got it to work now with the kirby()->controller('home', ['page' => $tag->parent()]); way, you suggested above. However, if there is an easier (and maybe also cheaper) way to get the same thing done, I would be interested.

Thanks!

What I don’t understand is why you need these Kirbytags in the first place?

What do you mean? What I need them for specifically?
Those are Kirbytags that link you to an image within the text (something like Fig. 1: A Mountain). And I need the controller variable to first run over all the images within my block to actually figure out the number for each image. Once I have done this in the controller, I can then target these images via a slug field I set up for each of them. So I use my kirbytag like this

(fig: a-mountain)

And it then gets the correct number automatically. As I said, very open for suggestions to solve this somehow more easily.