Skip to content

Document usage of $data variable in Tpl or use UUID #7576

@cstmth

Description

@cstmth

Description

To my understanding, templates and snippets are always provided with a $data variable. I could not find any documentation regarding that.

I just wanted to use an optional $data variable myself as a snippet attribute and found out that if I do not pass anything for it, I get the value of the ominous $data variable.

@fabianmichael told me the variable is being defined here: https://github.com/getkirby/kirby/blob/main/src/Filesystem/F.php#L423

Expected behavior

Fabian also suggested to

  • either note this behaviour somewhere in the documentation
  • or instead use a UUID, possibly somewhat like this:
function tpl($data) {
  $varName = '__' . bin2hex(random_bytes(16));
  $$varName = $data;
  unset($data);
  extract($$varName);
  // include template file
}

To reproduce

  1. Use fresh Kirby Plainkit
  2. Dump $data in template/snippet

Your setup

Kirby Version
5.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions