# Using variables with strings in with Kirby collection

**URL:** <https://forum.getkirby.com/t/using-variables-with-strings-in-with-kirby-collection/6433>\
**Category:** Questions\
**Tags:** v2\
**Created:** [January 27, 2017, 4:25pm UTC](https://forum.getkirby.com/t/using-variables-with-strings-in-with-kirby-collection/6433 "2017-01-27T16:25:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lonelyalien](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lonelyalien](https://forum.getkirby.com/u/lonelyalien)\
**Post date:** [January 27, 2017, 4:25pm UTC](https://forum.getkirby.com/t/using-variables-with-strings-in-with-kirby-collection/6433/1 "2017-01-27T16:25:03Z")

</div>

I’m trying to dynamically generate field names for use in a plugin I’m working on for a project. Kirby returns an Undefined property error whenever I try to use my variable to create a Kirby collection.

`$template = $page->template(); $subject = page('constants')->$template.'_subject()';`

When I echo `$template.'_subject()`, it works just fine and shows eg. `plots_subject();`. When I try to use it to create a Collection ($subject), it returns following error.

`Undefined property: Page::$plot_subject()`.

For some reason, Kirby/PHP tries to cast it to a variable when it should not. Is there a way around this?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 27, 2017, 4:35pm UTC](https://forum.getkirby.com/t/using-variables-with-strings-in-with-kirby-collection/6433/2 "2017-01-27T16:35:49Z")

</div>

Try this:

```auto
$template = $page->template();
$field = $template . '_subject';
$subject = $page->$field();

```

---

<div class="post-metadata">

**Author:** ![lonelyalien](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lonelyalien](https://forum.getkirby.com/u/lonelyalien)\
**Post date:** [January 27, 2017, 4:44pm UTC](https://forum.getkirby.com/t/using-variables-with-strings-in-with-kirby-collection/6433/3 "2017-01-27T16:44:15Z")

</div>

Yes! That worked. Thanks 😀

---

<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, 4:52pm UTC](https://forum.getkirby.com/t/using-variables-with-strings-in-with-kirby-collection/6433/4 "2025-02-06T16:52:52Z")

</div>

Archived on February 6, 2025, 4:52pm UTC


