Search code examples
phpsymfony1

Symfony: recursive call in template


I need to render a nested set tree as a li-structure with unlimited depth. While I understand how to do it in plain php (like here: PHP: How to generate a <ul><li> tree in an xml2assoc array result?), I hate echoing html tags and would like to have it done in a template. Is it possible with PHP as a templating language? Where should I define a recursive function?


Solution

  • For me, I depends on how much 'code' is needed in each iteration.

    For simple trees, I would just declare a function at the top of the view-file. (Since I think that function has only real value in that separate file).

    For trees with a bit more rendering, I would create a separate partial file. That file could be called in the view file and in the partial file itself.

    You could also create a helper file, which you use on that specific page, put than the partial would make more sense and is easier to implement (and you can use all other helper functions and symfony functions)