Published by Mijingo

tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

Embedding solves a lot of issues

  • Posted by Kelsey Martens
  • May 29, 2009

If you ever have trouble parsing data, or if you’re dealing with variable collisions (usually encountered when using Addons within other module tags), consider using an embed. smile

For example, if I want to place some code from the Favorites module inside the {exp:weblog:entries} loop, certain variables and conditionals are surely to collide because of an overlap in usage ({count}, {if no_results}, etc). You can do this instead:

Main Template:

{exp:weblog:entries}
<h2>{title}</h2>
{body}

<h4>Members that Saved this entry as a Favorite:</h4>
{embed="path_to/embed" entry_id="{entry_id}"}

{
/exp:weblog:entries} 

Embedded Template:

<ul>
{exp:favorites:members entry_id="{embed:entry_id}" orderby="screen_name" sort="asc"}
<li>[{count}] <a href="{path=users/profile}{member_id}/">{screen_name}</a></li>
{if no_results}<li>No members have saved this entry as a Favorite.</li>{/if}
{
/exp:favorites:members}
</ul

The key is to also pass along any relevant variables such as {entry_id}, depending of course on what you’re embedding. In this case we needed to pass along the Entry ID of the weblog entry so that the Favorites module knows which entry you’re referring to. wink

Bridget07:58 on 05.31.2009

Thank you for this! This tip just solved an issue I was having with variable collisions.

Kenny Meyers15:31 on 06.02.2009

Not only that, but for even more complex issues you can various types of PHP parsing (Using “before” and “after”) to do even more complex stuff!

Jason Morehead05:51 on 06.03.2009

I’ve used this on several sites, and it does indeed come in very handy.

However, I’ve found that I need to be careful because depending on what is contained in the embed, there can be some adverse effects on a site’s performance (i.e., increasing the number of DB queries).