5

Does anybody know of a solution to nested playa tags?

Currently I believe that the only way to retrieve playa entires from within another playa tag pair is to use an embed, is this still the case? Can we circumvent this using Stash or something similar?

This is what I'd like to be able to do:

{exp:channel:entries channel="products" dynamic="yes"}
        {!-- Product specific stuff goes here --}

        {!-- My Playa fieldtype that relates other products to this one --}
        {cf_phone_similar_products}
        <article>
            <header>{title}</header>

            {!-- Each Product has a Related plan that I need to grab --}
            <p>Free on the {exp:playa:children field="cf_phone_free_with_a_plan"}{title}{/exp:playa:children}</p>
        </article>
        {/cf_phone_similar_products}

{/exp:channel:entries}
2
  • Did any of the answers help you? If yes, please mark the answer correct by clicking on the checkmark to the left of the answer.
    – Anna_MediaGirl
    Dec 22, 2012 at 6:27
  • Remember to set the parse_depth parameter in exp:stash:set to a suitable level (Try 3 or above). Mar 7, 2013 at 7:12

3 Answers 3

11

Using the new var_prefix (came in in version 4.2 IICRC) embeds have / can been done away with. Links to docs - http://pixelandtonic.com/playa/docs/templates

Example code:

{exp:playa:children channel="news" parse="inward" var_prefix="news"}
    {news:exp:playa:children channel="editors" var_prefix="editor"}
        {news:title} - {editor:title}
    {/news:exp:playa:children}
{/exp:playa:children}
3
  • 1
    I know that code is in the official docs for Playa but to my knowledge EE cannot parse identical nested tag pairs like that. The regular expression that parses tag pairs would match the inner closing tag for the outer opening tag. Now it could be it seems to work because those two closing tags are adjacent but I'm pretty sure if they weren't the template would break. The solution to that is to append a 4th tagpart to the inner tag pair, e.g. {exp:playa:children:inner}...{/exp:playa:children:inner} Nov 24, 2012 at 13:46
  • Mark you are right this just doesn't work as advertised. Would nested lists be an option?
    – since1976
    Nov 25, 2012 at 20:59
  • 1
    This is correct, but you were missing the “news” prefix on the nested {exp:playa:children} and {/exp:playa:children} tags. Updated it. Jan 10, 2013 at 2:44
3

I tried to do something similar with Stash but I finally rollback to an embed.

The main reason is that it was becoming hard to maintain and not effective as expected. Sometimes embeds are just the way to go, especially with nested complex template nesting tags.

Embeds are not always evil ;)

2

I think John's solution will work if all you need is "title", "entry_id", "channel" but if you need to access a field you've defined in "Plan" you'll have to pass an embed:

{exp:channel:entries channel="products" dynamic="yes"}
    {!-- Product specific stuff goes here --}

    {!-- My Playa fieldtype that relates other products to this one --}
    {exp:playa:children field="cf_phone_similar_products" parse="inward" var_prefix="prod"}
    <article>
        <header>{prod:title}</header>

        {!-- Each Product has a Related plan that I need to grab --}
        <p>Free on the {exp:playa:children field="cf_phone_free_with_a_plan" var_prefix="plan"}{plan:title}{/exp:playa:children}</p>
    </article>
    {/exp:playa:children}

{/exp:channel:entries}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.