ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Embedding template only IF specific entry is displayed (?)

November 08, 2011 3:57am

Subscribe [2]
  • #1 / Nov 08, 2011 3:57am

    ZEDG

    32 posts

    Im not sure if what i describe below is the correct way to do things in EE, so any help is appreciated.

    So i have one ‘main’ template that displays a channel - that has one field {content}.

    {exp:channel:entries channel="content_page"}
    <h1>{title}</h1>
    <div id="body">
    {content}
    </div>
    {/exp:channel:entries}

    Now what i want, is for another template to get embeded, ONLY if a specific entry is being displayed.

    So something like this:

    {exp:channel:entries channel="content_page"}
    <h1>{title}</h1>
    <div id="body">
    {content}
    
        {embed="activities/events"}
        This would only get embedded if a specific entry ('entry id?' or 'page?') is being displayed.
    
    
    </div>
    {/exp:channel:entries}

    Where the template called ‘events’ would consist of code such as:

    {exp:channel:entries channel="whats_happening" sort="asc"}
         <h2>{title}</h2>
    <p>     {event_outline}<br />
    {/exp:channel:entries}


    I have tried a few things to no avail, but i have no idea whether you would actually do it this way. Would you use an ‘if’ statement to embed a template, and if so can you have a template showing open/close channel:entries as above? Or would you use relationships?


    ..So rather than have separate templates for each channel, i simply embed a template that contains channel info, within the ‘main’ template.

    ??

  • #2 / Nov 08, 2011 6:23am

    John St-Amand

    865 posts

    Yes, you can definitely do that (I’ve done it myself many times).  One suggestion for you though - a tip I picked up at EECI from Jacob Russell’s presentation - if you are not passing a variable through your embed, use a snippet instead - it’ll run faster and not fire the EE template engine.

    So you would have something like this:

    {exp:channel:entries channel="content_page" OTHER_PARAMETERS_GO_HERE}
    {title}
    {content}
    
    {if entry_id == "203"}
    {snippet_events}
    {/if}
    
    {/exp:channel:entries}

    And then your snippet would be the same as you embed would have been:

    {exp:channel:entries channel="whats_happening" sort="asc" OTHER_PARAMETERS_GO_HERE}
         <h2>{title}</h2>
    <p>     {event_outline}<br />
    {/exp:channel:entries}

    Hope that helps!

  • #3 / Nov 08, 2011 10:52am

    ZEDG

    32 posts

    THANKS for your help!

    Something doesnt appear to be working though. It does not render on the page correctly.
    (On the actual page the snippet should be appearing, the layout gets messy - as in some div closing tags, etc are missing? On the other pages, any (html, etc) code that should appear below the snippet code, gets removed)


    So my snippet (whats-happening-snippet):

    {exp:channel:entries channel="whats_happening" sort="asc"}
    <h2>{title}<a href="http://{content_doc1}%22name=%22{title}%22&gt&lt/a&gt/h2h5{event_summary}/h5{event_outline}&ltdiv" class="box">{content_doc1name}</a> {file_extension}: {file_size})</li>
      {/exp:jco_getfileinfo}
      </ul>
     </div>
    {/if}
    
    {if entry_id == "126"}
    {whats-happening-snippet}
    {/if}
    
    </div>
    {/exp:channel:entries}


    What would the issue be??

    If i remove the open and close channel code lines from the snippet, everything appears correctly.

  • #4 / Nov 08, 2011 7:40pm

    John St-Amand

    865 posts

    Crap! Sorry, I was perhaps getting a little too keen on snippet use for my own good and therein led you astray.  It would seem EE treats a snippet that has an entries tag in it differently - so if you embed a snippet that has an entries tag in it within an entries tag pair, is has the same effect as nesting entries tag pairs - so it in fact WON’T work.  You will have to do what you want to do with an embed - the code would be exactly what I outlined previously, but you would use an embed instead of the snippet.  My apologies for the confusion - I wasn’t thinking about it properly.

    So you would do this:

    {exp:channel:entries channel="content_page" OTHER_PARAMETERS_GO_HERE}
    {title}
    {content}
    
    {if entry_id == "203"}
    {embed="embeds/events"}
    {/if}
    
    {/exp:channel:entries}

    AND in the embedded template:

    {exp:channel:entries channel="whats_happening" sort="asc" OTHER_PARAMETERS_GO_HERE}
         <h2>{title}</h2>
    <p>     {event_outline}<br />
    {/exp:channel:entries}

    Let me know if you run into any further trouble with it.

  • #5 / Nov 08, 2011 8:05pm

    ZEDG

    32 posts

    OK no problem. Now working perfectly.
    Thanks for your help.

    (Now i know all about snippets so you taught me something useful)!

  • #6 / Nov 08, 2011 8:10pm

    John St-Amand

    865 posts

    Well that’s what I was aiming for, More than anything, since that’s what the EE community did for me when I got started - even if I didn’t take the most direct route in this case. Best of luck with the rest of your project.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases