Forward a php variable to custom kirby tag

I want to create a custom Kirby Tag, but that tag needs extra data from the template controller. Is it possible to ‘add’ the return values of the template controller to the page object so i can access it within the kirbytag code?

or any other way i can get a php variable from the template code into a kirby tag?

https://getkirby.com/docs/advanced/kirbytext
https://getkirby.com/docs/templates/controllers

maybe programmatically add a new ‘field’ to the page object in the template code which then will be availab to the kirbytag trough its kirbytext->field() method? can this be done?


Something like this should work:

Field

kirbytext::$tags['myfield'] = array(
  'html' => function($tag) {
    return $tag->page()->myFieldAttr;
  }
);

Template

<?php $page->myFieldAttr = 'this is a test value'; ?>
<?php echo $page->text()->kirbytext() ?>

A bit hacky, but at least it does not use global variables. :wink:

1 Like

cool. just what i needed. wonder why i did not try this before. :slightly_smiling:

another solution i thought of would be to make the kirbytag create the required html code with placeholders and parse the ->kirbytext() output before rendering adding my variable where needed. like using a template language.

the usecase for the kirbytag was to add uniform forms in the panel. like so…

(form: newsletter-signup)

http://www.getkirby-plugins.com/uniform

but uniform defines the forms in the controller so i needed to forward the php variables to the kirbytag code.
which is now possible. thanks again.