Have you ever called a snippet within the snippet itself?

Hi!

I’m pretty sure I’m not the first doing something like this.
Yesterday I created a snippet called a.php containing an <a> tag, a letter ‘a’ and a call for the ‘a’ snippet.

// site/snippets/a.php

<a>
  a <?php snippet('a') ?>
</a>

Then I wanted to spice it up a little, so I created another snippet called b.php and I let A call B and B call A.

// site/snippets/a.php

<a>
  a <?php snippet('b') ?>
</a>


// site/snippets/b.php

<b>
  b <?php snippet('a') ?>
</b>

Results

In the first case (A only) the result would be a very long list of <a> a </a>.

In the second case (A and B) the structure gets more interesting, something like this:

<a>
  <b></b>
</a>

<b>
  <a>
    <b></b>
  </a>

  <b>
    <a>
      <b></b>
    </a>

    <b>
      ...
    </b>
  </b>
</b>

Conclusions:

First of all, I honestly expected my browser to crash, and it didn’t.
Second thing, I can’t explain why the number of DOM elements generated this way is always different, and the difference could is quite huge (from 5000 to more than 25000).

Has anyone ever done similar experiments and does anybody have an answer to the different number of elements generated?

php nesting functions limit or max execution time? try activating xdebug to to get a proper error?

just out of curiosity: what are you trying to achieve with the recursive snippets?

If you use recursive structures, you always need an exit point, e.g. a for loop with a condition etc. otherwise it will just stop at some point with a max. function nesting level error.

The treemenu snippet is an example of a recursive snippet: https://getkirby.com/docs/cookbook/templating/menus#tree-menu but it stops if there are no more children.