Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for recursive partials #26

Open
deuill opened this issue Jan 9, 2013 · 4 comments
Open

Support for recursive partials #26

deuill opened this issue Jan 9, 2013 · 4 comments

Comments

@deuill
Copy link

deuill commented Jan 9, 2013

Mustache supposedly allows for recursive partials, which are useful for rendering nested/heirarchical data like trees and lists etc.

I think this might be somewhat hard to accomplish due to the fact we compile/parse the template before we render it, so partials are evaluated even if they're wrapped in sections which are empty.

Is there some way around this? Any planned support?

@abh
Copy link
Contributor

abh commented Apr 3, 2013

Is it in the mustache spec (does hogan.js or the ruby mustache library have this feature)?

@hoisie hoisie closed this as completed Apr 7, 2013
@hoisie hoisie reopened this Apr 7, 2013
@hoisie
Copy link
Owner

hoisie commented Apr 7, 2013

I must admit recursive partials do seem a bit strange to me, but I can imagine a use case.

@deuill
Copy link
Author

deuill commented Apr 12, 2013

It's supported in the canonical Ruby implementation, which has a test case for recursive partials. They're useful for data structures with arbitrary depths, such as category lists etc, and help keep the code clean. For example:

category.mustache

<div class="category">
  <div class="name">{{name}}</div>
  {{#sub}}
  {{> category}}
  {{/sub}}
</div>

data.json

[
    {
        "name": "Birds",
        "sub": [
            {
                "name": "Corvidae",
                "sub": [
                    {
                        "name": "Crows"
                    },
                    {
                        "name": "Ravens"
                    },
                    {
                        "name": "Rooks"
                    }
                ]
            },
            {
                "name": "Chickens"
            }
        ]
    },
    {
        "name": "People"
    }
]

The above example would be much dirtier and would not cover all test cases if not for recursive partials. I guess one solution would be to parse each partial seperately during compilation and inserting them conditionally in the rendering phase.

@hoisie
Copy link
Owner

hoisie commented Apr 12, 2013

Nice example @ThoughtMonster ! This should definitely be supported. The parsing is recursive, so there might be a quick solution if can some how mark partials as already visited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants