{"authorId":1,"title":{"title":{}},"slug":"expressionengine-embed-an-embed-within-itself","postDate":1255788025,"expiryDate":null,"enabled":1,"fields":{"2":"<p>At work this week, there was discussion on how to accomplish something in ExpressionEngine. We wanted to pull in entries that were in one category, but if there were no entries, then pull in from other categories and display that instead. We were kind of stumped as to how to do that.<\/p>\n<!--more-->\n<p>I&rsquo;m not sure if we were missing some easy way to do that, but we couldn&rsquo;t think of a nice way to accomplish it.<\/p>\n\n<h2>Let&rsquo;s Backup<\/h2>\n<p>I guess it will be helpful to give you some more context. We had created a Channel (our term for Weblog) for <strong>events<\/strong>. You could categorize the events, and they would publish in different places.<\/p>\n<p>In this instance, the homepage, we wanted to pull an event from one category, but if there were none, then just display an event from another category.<\/p>\n\n<h2>The Initial Code<\/h2>\n\n<p>This code was separated out in an embed so that we could use it in various places on the site:<\/p>\n\n<div class=\"codeblock\"><code>\n&#123;assign_variable:channel_name=\"&#123;embed:channel&#125;\"&#125;<br \/>&#123;assign_variable:category_id=\"&#123;embed:category&#125;\"&#125;<br \/><br \/>&#123;exp:weblog:entries&nbsp;weblog=\"&#123;channel_name&#125;\"&nbsp;category=\"&#123;category_id&#125;\"&nbsp;show_future_entries=\"yes\"&nbsp;show_expired=\"no\"&nbsp;limit=\"1\"&nbsp;disable=\u00e2\u20ac\u0153category_fields|custom_fields|member_data|pagination|trackbacks\u00e2\u20ac\u009d&#125;<br \/><br \/>&nbsp;&lt;h2&gt;&#123;title&#125;&lt;\/h2&gt;<br \/>&nbsp;&#123;if&nbsp;events_feature_image&#125;<br \/>&nbsp;&nbsp;&lt;p&nbsp;class=\"rightSide\"&gt;&lt;a&nbsp;href=\"&#123;url_title_path=\"&#123;embed:template_path&#125;\"&#125;\"&gt;&lt;img&nbsp;src=\"&#123;events_feature_image&#125;\"&nbsp;alt=\"\"&nbsp;\/&gt;&lt;\/a&gt;&lt;\/p&gt;<br \/>&nbsp;&#123;\/if&#125;<br \/>&nbsp;&#123;if&nbsp;events_summary&#125;<br \/>&nbsp;&nbsp;&#123;events_summary&#125;<br \/>&nbsp;&#123;\/if&#125;<br \/><br \/>&nbsp;&lt;p&gt;&lt;a&nbsp;href=\"&#123;url_title_path=\"&#123;embed:template_path&#125;\"&#125;\"&nbsp;class=\"more\"&gt;Read&nbsp;more&nbsp;&amp;raquo;&lt;\/a&gt;&lt;\/p&gt;<br \/>&#123;\/exp:weblog:entries&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>The code doesn&rsquo;t really need to be explained; it&rsquo;s not the important part of this post. So on the homepage, we had code that looked like this to pull in the event:<\/p>\n\n<div class=\"codeblock\"><code>\n&#123;embed=\"site\/.events\"&nbsp;channel=\"events\"&nbsp;category=\"7\"&nbsp;template_path=\"news-events\/events\"&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>Now, what we really wanted to be able to do was to modify the <strong>exp:weblog:entries<\/strong> code to pull from a different category if there were no results. Something like this:<\/p>\n\n<div class=\"codeblock\"><code>\n&#123;exp:weblog:entries&nbsp;weblog=\"&#123;channel_name&#125;\"&nbsp;category=\"&#123;category_id&#125;\"&nbsp;show_future_entries=\"yes\"&nbsp;show_expired=\"no\"&nbsp;limit=\"1\"&nbsp;disable=\u00e2\u20ac\u0153category_fields|custom_fields|member_data|pagination|trackbacks\u00e2\u20ac\u009d&#125;<br \/>&amp;hellip;<br \/>&nbsp;&#123;if&nbsp;no_results&#125;<br \/>&nbsp;&nbsp;Code&nbsp;&nbsp;to&nbsp;pull&nbsp;from&nbsp;a&nbsp;different&nbsp;category<br \/>&nbsp;&#123;\/if&#125;<br \/>&amp;hellip;<br \/>&#123;\/exp:weblog:entries&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>But the problem was, there really was no code that we could put in there that would pull from another category since you can&rsquo;t nest <strong>exp:weblog:entries<\/strong> tags. So that&rsquo;s when I remembered that you could stick an embed file in there that could have the <strong>exp:weblog:entries<\/strong> tag inside of it.<\/p>\n\n<p>Then we realized that the code we wanted to write would be exactly like the code in the embed file we were already working with.<\/p>\n\n<p>So we did a little investigation and found out that <a href=\"http:\/\/expressionengine.com\/docs\/templates\/embedding_templates.html\">according to the documentation (see notes section)<\/a>, you cannot embed an embed within itself.<\/p>\n\n<p>That makes sense, because you could create an infinite loop. But what if we knew that it would not return an infinite loop?<\/p>\n\n<h2>Modified Code<\/h2>\n\n<p>So we determined that what we could do is not pass in the category on the embed statement, and that would pull back any events.<\/p>\n\n<div class=\"codeblock\"><code>\n&#123;exp:weblog:entries&nbsp;weblog=\"&#123;channel_name&#125;\"&nbsp;category=\"&#123;category_id&#125;\"&nbsp;show_future_entries=\"yes\"&nbsp;show_expired=\"no\"&nbsp;limit=\"1\"&nbsp;disable=\u00e2\u20ac\u0153category_fields|custom_fields|member_data|pagination|trackbacks\u00e2\u20ac\u009d&#125;<br \/>&amp;hellip;<br \/>&nbsp;&#123;if&nbsp;no_results&#125;<br \/>&nbsp;&nbsp;&#123;embed=\"site\/.events\"&nbsp;channel=\"events\"&nbsp;template_path=\"news-events\/events\"&#125;<br \/>&nbsp;&#123;\/if&#125;<br \/>&amp;hellip;<br \/>&#123;\/exp:weblog:entries&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>See how there is no category parameter this time? Here is the full modified code:<\/p>\n\n<div class=\"codeblock\"><code>\n&#123;assign_variable:channel_name=\"&#123;embed:channel&#125;\"&#125;<br \/>&#123;assign_variable:category_id=\"&#123;embed:category&#125;\"&#125;<br \/><br \/>&#123;exp:weblog:entries&nbsp;weblog=\"&#123;channel_name&#125;\"&nbsp;category=\"&#123;category_id&#125;\"&nbsp;show_future_entries=\"yes\"&nbsp;show_expired=\"no\"&nbsp;limit=\"1\"&nbsp;disable=\u00e2\u20ac\u0153category_fields|custom_fields|member_data|pagination|trackbacks\u00e2\u20ac\u009d&#125;<br \/><br \/>&nbsp;&lt;h2&gt;&#123;title&#125;&lt;\/h2&gt;<br \/>&nbsp;&#123;if&nbsp;events_feature_image&#125;<br \/>&nbsp;&nbsp;&lt;p&nbsp;class=\"rightSide\"&gt;&lt;a&nbsp;href=\"&#123;url_title_path=\"&#123;embed:template_path&#125;\"&#125;\"&gt;&lt;img&nbsp;src=\"&#123;events_feature_image&#125;\"&nbsp;alt=\"\"&nbsp;\/&gt;&lt;\/a&gt;&lt;\/p&gt;<br \/>&nbsp;&#123;\/if&#125;<br \/>&nbsp;&#123;if&nbsp;events_summary&#125;<br \/>&nbsp;&nbsp;&#123;events_summary&#125;<br \/>&nbsp;&#123;\/if&#125;<br \/><br \/>&nbsp;&lt;p&gt;&lt;a&nbsp;href=\"&#123;url_title_path=\"&#123;embed:template_path&#125;\"&#125;\"&nbsp;class=\"more\"&gt;Read&nbsp;more&nbsp;&amp;raquo;&lt;\/a&gt;&lt;\/p&gt;<br \/><br \/>&nbsp;&lt;strong&gt;&#123;if&nbsp;no_results&#125;<br \/>&nbsp;&nbsp;&#123;embed=\"site\/.events\"&nbsp;channel=\"events\"&nbsp;template_path=\"news-events\/events\"&#125;<br \/>&nbsp;&#123;\/if&#125;&lt;\/strong&gt;<br \/>&#123;\/exp:weblog:entries&#125;&nbsp;\n\n<\/code><\/div>\n\n<p><strong>And that worked!<\/strong><\/p>\n\n<h2>Houston, we still have a problem&hellip;<\/h2>\n<p>But, we then realized that the same problem would occur if there were no future events in any categories. So, we still needed to go back and solve that problem by similarly passing the value for the <strong>show_expired<\/strong> parameter in the embed statement as well. I left that to my colleague, so I didn&rsquo;t test this code, but this is my guess of how to solve that problem:<\/p>\n\n<div class=\"codeblock\"><code>\n&#123;assign_variable:channel_name=\"&#123;embed:channel&#125;\"&#125;<br \/>&#123;assign_variable:category_id=\"&#123;embed:category&#125;\"&#125;<br \/>&lt;strong&gt;&#123;assign_variable:show_expired=\"&#123;embed:show_expired&#125;\"&#125;&lt;\/strong&gt;<br \/><br \/>&#123;exp:weblog:entries&nbsp;weblog=\"&#123;channel_name&#125;\"&nbsp;category=\"&#123;category_id&#125;\"&nbsp;show_future_entries=\"yes\"&nbsp;&lt;strong&gt;show_expired=\"&#123;show_expired&#125;\"&lt;\/strong&gt;&nbsp;limit=\"1\"&nbsp;disable=\u00e2\u20ac\u0153category_fields|custom_fields|member_data|pagination|trackbacks\u00e2\u20ac\u009d&#125;<br \/><br \/>&nbsp;&lt;h2&gt;&#123;title&#125;&lt;\/h2&gt;<br \/>&nbsp;&#123;if&nbsp;events_feature_image&#125;<br \/>&nbsp;&nbsp;&lt;p&nbsp;class=\"rightSide\"&gt;&lt;a&nbsp;href=\"&#123;url_title_path=\"&#123;embed:template_path&#125;\"&#125;\"&gt;&lt;img&nbsp;src=\"&#123;events_feature_image&#125;\"&nbsp;alt=\"\"&nbsp;\/&gt;&lt;\/a&gt;&lt;\/p&gt;<br \/>&nbsp;&#123;\/if&#125;<br \/>&nbsp;&#123;if&nbsp;events_summary&#125;<br \/>&nbsp;&nbsp;&#123;events_summary&#125;<br \/>&nbsp;&#123;\/if&#125;<br \/><br \/>&nbsp;&lt;p&gt;&lt;a&nbsp;href=\"&#123;url_title_path=\"&#123;embed:template_path&#125;\"&#125;\"&nbsp;class=\"more\"&gt;Read&nbsp;more&nbsp;&amp;raquo;&lt;\/a&gt;&lt;\/p&gt;<br \/><br \/>&nbsp;&#123;if&nbsp;no_results&#125;<br \/>&nbsp;&nbsp;&#123;embed=\"site\/.events\"&nbsp;channel=\"events\"&nbsp;&lt;strong&gt;show_expired=\"yes\"&lt;\/strong&gt;&nbsp;template_path=\"news-events\/events\"&#125;<br \/>&nbsp;&#123;\/if&#125;<br \/>&#123;\/exp:weblog:entries&#125;&nbsp;\n\n<\/code><\/div>\n\n<h2>Conclusion<\/h2>\n<p>Was there some really easy way to do this that we completely blanked on? It seemed a little <em>hacky<\/em>, but the solution worked.<\/p>","4":{"website":{}}}}