Access previous kirbytag in a kirbytag

Hi there,

I am using Kirby to write an academic text and in this text I use kirbytags for references, something like „This is a meaningful quote“ (Mustermann, 2018, p. 30). Now, when I quote the same book the next time right after, I would like it to say – as is typical in academic texts – „this is another quote from the same book“ (Ibid., p. 48).

For this system to figure out to say “Ibid.” the second time, a kirbytag would need to be able to look up the content of the previus Kirbytag. So I want to temporarily save the most kirbytag somewhere for the next kirbytag to have access to it. Only, I don’t know how to do it. When I set up a “global” variable on the page itself, it gives me an error, that the Kirbytag does not know about that variable.

Is there any way to have a variable somewhere that keeps track of the most recent kirbytag of that type and let’s me access that once I reach the next instance of the kirbytag?

Thanks!

I think I don’t understand your use case. So while writing you would put “Mustermann” into both tags but when rendering the page you want the output of the second one to be “Ibid” instead? Then why not put that into the tag in the first place? Because as far as I know, for in-text citations to use “Ibid”, these text parts should be close to each other, so that would be another factor to keep in mind.

I put a slug like mustermann into the kirbytag that looks up a subpage for that specific book and allows some extra features, like allowing an info box giving all the book details on hover or something like that. So my kirbytags within the text will look like (see: mustermann pages: 20) and a bit later maybe (see: mustermann pages: 38ff.).

That’s why I cannot write the text like “Ibid.” directly into the Kirbytag (or without Kirbytag), because then it wouldn’t know which book to look up and I would have to keep track of it manually and change it back in case I list another book inbetween these two tags at some point.

these text parts should be close to each other

This can be ignored in my use case, I really just need to find out how to look up the previous kirbytag.

You could extract all relevant kirbytags with some regex pattern, create a collection from them and then check the one before.

It would probably be a lot easier just to give those tags a third parameter

(see: mustermann pages: 38ff. ibid: true)

Is there no easy way to set up a global variable somewhere that I can write to and read from? That way I could easily keep track of the relevant stuff. I tried to access a variable that I set up in the template code of the page itself, but that could not be read from within the kirbytag. Is there not a more global place to set up such variables and to access them from Kirbytags?

Ok, I solved it like this now. Unfortunately my knowledge of PHP is not sufficient to judge if this is a good solution, but it works without going through the pain of setting up a regex:

<?php

$previousTag = '';

Kirby::plugin('trych/custom-tags', [
  'tags' => [
    'see' => [
      'attr' => [
          'title'
      ],
      'html' => function($tag) {
        global $previousTag;

        $prevTag = $previousTag;
        $previousTag = $tag->value();

        $naming = $prevTag === $tag->value ? 'Ibid.' : tag->value;

        // ...
      }
    ]
  ]
]);