# Forward a php variable to custom kirby tag

**URL:** <https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412>\
**Category:** Questions\
**Tags:** v2\
**Created:** [February 24, 2016, 1:06pm UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412 "2016-02-24T13:06:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [February 24, 2016, 1:06pm UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412/1 "2016-02-24T13:06:21Z")

</div>

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/advanced/kirbytext)  
[https://getkirby.com/docs/templates/controllers](https://getkirby.com/docs/templates/controllers)  
[https://github.com/getkirby/kirby/blob/master/core/page.php](https://github.com/getkirby/kirby/blob/master/core/page.php)

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [February 24, 2016, 1:17pm UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412/2 "2016-02-24T13:17:57Z")

</div>

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?

> <https://github.com/getkirby/kirby/blob/master/core/kirbytag.php#L25>

  

> <https://github.com/getkirby/kirby/blob/master/core/kirbytext.php#L31>

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 24, 2016, 3:13pm UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412/3 "2016-02-24T15:13:23Z")

</div>

Something like this should work:

**Field**

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

```

**Template**

```php
<?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. 😉

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [February 25, 2016, 11:06am UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412/4 "2016-02-25T11:06:41Z")

</div>

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

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.

> <https://github.com/getkirby/toolkit/blob/master/lib/tpl.php>

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [February 25, 2016, 12:10pm UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412/5 "2016-02-25T12:10:58Z")

</div>

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

```
(form: newsletter-signup)

```

[http://www.getkirby-plugins.com/uniform](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.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:00pm UTC](https://forum.getkirby.com/t/forward-a-php-variable-to-custom-kirby-tag/3412/6 "2025-02-06T17:00:07Z")

</div>

Archived on February 6, 2025, 5:00pm UTC


