1

Having a bit of trouble getting my head round something in Playa...

I have code which is a bit like this to deal with multiple channels from a single Playa field:

{exp:channel:entries channel="my_channel" limit="1"}
    <h1>{title}</h1>
    {exp:playa:children field="my_playa_field"}
        {if '{channel_short_name}' == 'channel_one'}
        <section>
            <h1>{title}</h1>
            {channel_one_specific_field}
        <section>
        {if:elseif '{channel_short_name}' == 'channel_two'}
        <section>
            {title}
            {channel_two_specific_field}
        </section>
        {/if}
    {/exp:playa:children}
{/exp:channel:entries}

There are more than two channels, and each has a number of custom fields, but the basic idea is the same. It works, but what I'd like to do to make maintenance a bit easier is take all the code between the if statements and call them as embeds instead:

{exp:channel:entries channel="my_channel" limit="1"}
    <h1>{title}</h1>
    {exp:playa:children field="my_playa_field"}
        {if '{channel_short_name}' == 'channel_one'}
            {embed="templates/template_one"}
        {if:elseif '{channel_short_name}' == 'channel_two'}
            {embed="templates/template_two"}
        {/if}
    {/exp:playa:children}
{/exp:channel:entries}

I've tried this, and it doesn't work. The right templates are being called, but the tags aren't being parsed as I would like, e.g. {title} echoes '{title}' rather than 'Entry title.'

So really I have three questions:

  1. Is this a sensible approach to take to using Playa fields?
  2. What am I missing with the embed template code?
  3. Will calling embedded templates harm performance, or would I be better keeping all the code in one template (which is what I'm doing now)?

1 Answer 1

0

Moving the code to an embed will not work without coding a channel:entries tag in the embed as well and telling it what to load. For example:

{embed="templates/template_one" id="entry_id" channel="{channel_short_name}"}

In the embed you need:

{exp:channel:entries channel="{embed:channel}" entry_id="{embed:id}" limit="1"}
    {title}
{/exp:channel:entries}

With that said, you WILL harm performance by making this change... It's generally accepted that embeds should be used as little as possible and I'd say absolutely shouldn't be used just to make code more readable.

You are best leaving your code in the template and commenting heavily to make the code more readable/maintainable.

2
  • Thank you - nice to know how to do it, even if I won't be doing it! I'll leave the code where it is :-)
    – thomas
    Mar 19, 2013 at 8:40
  • 1
    You could use snippets for this same issue without the performance hinderance. Aug 24, 2013 at 19:58

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.