Are there JavaScript template engines that support something like recursion?

The most obvious example that I can think of is the output of nested comments. Say you have a comment tree and you want to display it as a nested html (say lists inside lists or divs inside divs) using your template.

The comment block / function / tag / helper / comment / something else should somehow call itself for comments.

Are there template engines that support such things inside a single template file?

I know that you can simply pre-calculate the “indent” or “depth level” of each comment and send them to the template as one flat list in the correct order, but let me say that I don’t want that. And let them say that I do not want to stitch the fragments together in the code / outside the template - I want the whole page to be contained in one template or theme file.


Refresh . I want to create a nested html. I want comments to be nested, not nested. I know how to backtrack from CSS. :) Whether it is done in a browser or on a server does not matter, because I want the template to be self-contained in a single file.

How in:

var html = render(template, {comments: aTreeOfNestedComments});

(., node.js, , "jQuery", javascript ...) , , mixins. , - ?

+3
3

, . , , .

, JavaScript, , . ; DOM (, , ), , .

JS .

[EDIT]. : , span ( ) div ( ), recursiveTemplate data-template-name="..."

. jQuery , recursiveTemplate .

+1

as @TJHeuvel said, you can use the server side of the script to create the desired output, this is the best way to do what you need. However, if you should use JavaScript, I would recommend jQuery, it will also allow you to get the desired result.

eg:

$("ul li").css("margin-left", "10px");
-1
source

All Articles