Nesting an ExpressionEngine Channel Entry Tag Pair within another Channel Entry Tag Pair?

jchamb picture jchamb · Dec 7, 2011 · Viewed 7.3k times · Source

I did some searching around but I can't seem to find any information on embedding a channel entry tag pair within another.

When I try the following code in a template, it breaks the page and I see the opening {reverse_related_entries sort="desc"} displayed as plain text:

{exp:channel:entries channel="pages"}
    {reverse_related_entries sort="desc"}
        {if show_testimonial}
            {exp:channel:entries channel="testimonials" orderby="random" limit="1"}
                <blockquote>
                    {testimony}
                    <cite>
                        <span class="cite_name">{cite_name}</span><br />
                        <span class="cite_org">{cite_org}</span><br />
                        <span class="cite_title">{cite_title}</span>
                    </cite>
                </blockquote>
            {/exp:channel:entries}
        {/if}
    {/reverse_related_entries}
{/exp:channel:entries}

Is there a way in ExpressionEngine to nest a channel entry tag pair inside itself?

Answer

rjb picture rjb · Dec 7, 2011

In order to nest a {exp:channel:entries} tag pair inside of itself, you'll need to embed the template within another template using an {embed} variable.

To do so, just modify your main channel entries tag to look like the following:

{exp:channel:entries channel="pages"}
    {reverse_related_entries sort="desc"}
        {if show_testimonial}
            {embed="template_group/template"}
        {/if}
    {/reverse_related_entries}
{/exp:channel:entries}

Then, create a new template with the contents of your nested channel entries tag pair:

{exp:channel:entries channel="testimonials" orderby="random" limit="1"}
    <blockquote>
        {testimony}
        <cite>
            <span class="cite_name">{cite_name}</span><br />
            <span class="cite_org">{cite_org}</span><br />
            <span class="cite_title">{cite_title}</span>
        </cite>
    </blockquote>
{/exp:channel:entries}

Which you can include in any ExpressionEngine template using the following syntax, as shown earlier:

{embed="template_group/template"}

Using embed templates are a standard way around some of ExpressionEngine's quirks and Parse Order (PDF, 32 KB), but they do carry a performance penalty with them so be mindful in deciding between using an {embed} and a {snippet}.