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.

Snippets not rendering

April 29, 2011 8:49pm

Subscribe [4]
  • #1 / Apr 29, 2011 8:49pm

    Chad Crowell

    242 posts

    Can anyone tell me why, every time I’ve tried to use snippets in EE2, they never work? Its so simple, but never works.  If we can add any EE code to snippets, why is it that this code in a snippet produces nothing except {read_more} in the template?

    {exp:channel:entries channel="books" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks" dynamic="no" rdf="off" limit="1" orderby="date" sort="desc" search:cf_books_homepage_title="not IS_EMPTY"}
                <a href="http://{page_uri}" class="float-left">{exp:imgsizer:size src="cf_books_cover" width="96" alt="{cf_books_homepage_title}"}</a>
                <h2><a href="http://{page_uri}">{cf_books_homepage_title}</a>
                {cf_books_homepage_description}
                <a href="http://{page_uri}">{read_more}</a>
            {/exp:channel:entries}
  • #2 / Apr 29, 2011 8:53pm

    Greg Aker

    6022 posts

    Chad, that’s odd as can be.

    I’m going to split off this thread into tech support so we can get to the bottom of it.

    Couple questions, what extensions are you running, and do you have a global variable or custom field named read_more?

    -greg

  • #3 / Apr 29, 2011 9:07pm

    Chad Crowell

    242 posts

    Thanks Greg.  I’ll break it down a bit for you.

    So, if I have the snippet {home_featured_book} and:

    put “hello” into it, “hello” is printed to the screen as expected.

    if I put:

    {exp:channel:entries channel="articles" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks" dynamic="no" rdf="off" limit="3"}
    {title}        
    {/exp:channel:entries}

    I get “Smart Publications” printed, which is the title of the entry that the snippet call is inside of on the template.  Shouldn’t the snippet be processed before the channel tag?  Below is the full template:

    {exp:channel:entries channel="pages" disable="categories|category_fields|member_data|pagination|trackbacks" rdf="off"}
    
        {if no_results}
        {redirect="404"}
        {/if}
        
        {embed="includes/_header" title="{title}"}
    
        <aside id="promo">
    
            {embed="includes/_left_col"}
    
        </aside>
    
        <section id="featured-content">
        ///
        {home_featured_book}
        ///
    
        </section>
        
        <section id="article-list">
            <header>
                <h1>{recent_health_articles}</h1>
            </header>
            {embed="includes/_recent_articles" limit="3"}
        </section>
        
        <section id="featured-book">
            <header>
                <h1>{from_our_library}</h1>
            </header>
            {embed="includes/_featured_book"}
        </section>
    
        <aside id="related">
    
            {embed="includes/_right_col"}
    
        </aside>
        
        {embed="includes/_footer"}
    
    {/exp:channel:entries}
  • #4 / Apr 29, 2011 9:07pm

    Chad Crowell

    242 posts

    and this is a 2.1.3 upgrade project from 1.6.9

  • #5 / Apr 29, 2011 9:22pm

    Greg Aker

    6022 posts

    Oh, I gotcha.

    In this case, an embed is probably a better option.

    Think of it this way.  At the point Snippets are parsed, it’s one to one replacement of that variable with your snippet code.  So if you pop open system/expressionengine/libraries/Template.php, look around line 320, and you’ll see:

    /** -------------------------------------
    /**  Parse manual variables and Snippets
    /** -------------------------------------*/
            
    // These are variables that can be set in the path.php file
    
    if (count($this->EE->config->_global_vars) > 0)
    {
        $this->log_item("Snippets (Keys): ".implode('|', array_keys($this->EE->config->_global_vars)));
        $this->log_item("Snippets (Values): ".trim(implode('|', $this->EE->config->_global_vars)));
    
        foreach ($this->EE->config->_global_vars as $key => $val)
        {
            $this->template = str_replace(LD.$key.RD, $val, $this->template);
        }
                
        // in case any of these variables have EE comments of their own
        $this->template = $this->remove_ee_comments($this->template);
    }

    Right after that code block you can add:

    echo '<pre>';
    echo htmlentities($this->template);
    echo '</pre><p>‘; exit;
    </pre>

    And when you visit the template, in your browser, you get:

    {exp:channel:entries channel="pages" disable="categories|category_fields|member_data|pagination|trackbacks" rdf="off"}
    
        {if no_results}
        {redirect="404"}
        {/if}
        
        {embed="includes/_header" title="{title}"}
    
        <aside id="promo">
    
            {embed="includes/_left_col"}
    
        </aside>
    
        <section id="featured-content">
        ///
        {exp:channel:entries channel="articles" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks" dynamic="no" rdf="off" limit="3"}
    {title}        
    {/exp:channel:entries} 
        ///
    
        </section>
        
        <section id="article-list">
            <header>
                <h1>{recent_health_articles}</h1>
            </header>
            {embed="includes/_recent_articles" limit="3"}
        </section>
        
        <section id="featured-book">
            <header>
                <h1>{from_our_library}</h1>
            </header>
            {embed="includes/_featured_book"}
        </section>
    
        <aside id="related">
    
            {embed="includes/_right_col"}
    
        </aside>
        
        {embed="includes/_footer"}
    
    {/exp:channel:entries}

    Then, we eventually move on to parsing of the exp tags, and you’re in a situation with nested tags.

    Make sense?

    -greg

  • #6 / Apr 29, 2011 9:23pm

    Lisa Wess

    20502 posts

    Hey Chad,

    Snippets are parsed exactly as if they’re in the template you’re calling.  So what you’re actually saying there is thus:

    {exp:channel:entries channel="pages" disable="categories|category_fields|member_data|pagination|trackbacks" rdf="off"}
    
        {if no_results}
        {redirect="404"}
        {/if}
        
        {embed="includes/_header" title="{title}"}
    
        <aside id="promo">
    
            {embed="includes/_left_col"}
    
        </aside>
    
        <section id="featured-content">
        ///
    {exp:channel:entries channel="articles" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks" dynamic="no" rdf="off" limit="3"}
    {title}        
    {/exp:channel:entries}
        ///
    
        </section>
        
        <section id="article-list">
            <header>
                <h1>{recent_health_articles}</h1>
            </header>
            {embed="includes/_recent_articles" limit="3"}
        </section>
        
        <section id="featured-book">
            <header>
                <h1>{from_our_library}</h1>
            </header>
            {embed="includes/_featured_book"}
        </section>
    
        <aside id="related">
    
            {embed="includes/_right_col"}
    
        </aside>
        
        {embed="includes/_footer"}
    
    {/exp:channel:entries}

    Which when you see it that way, I’m sure you’ll agree won’t work.  What is your end goal with this code? Perhaps we can help you come up with a better solution.

  • #7 / Apr 29, 2011 9:30pm

    Greg Aker

    6022 posts

    Teehee, I think this might mark the first time I beat Lisa to an answer with nearly identical content 😛

    I think you’ll find some interesting things going on in the template parser Chad.  Since you do know PHP, when you have some free time make a template or two and drop in that code so you can see exactly how the template parser is working to render the content.  I think you’ll learn some interesting things, and be happy you did it! 

    -greg

  • #8 / Apr 30, 2011 12:44am

    Chad Crowell

    242 posts

    Thanks guys, that makes total sense. Riddle me this, though. Isn’t the way you promote Snippets that you can put EE code in them?  http://ellislab.com/expressionengine/user-guide/templates/globals/snippets.html doesn’t note any constraints where it states this.  I’ll add to the notes on the page about this situation but I had avoided snippets to this point because I always tried to do something like what you see above and they never worked. I just thought it was buggy.

    Anyway, thanks for the explanation have a good weekend!

  • #9 / Apr 30, 2011 1:41am

    cbad

    60 posts

    I think you’ll learn some interesting things, and be happy you did it!

    wow, i am so glad I read this thread. That is so useful, and it seems moving this code around allows me to see the results of parsing at various stages. Very cool. Thanks for sharing!

    Is it possible to populate an embed with its initialized variables via typing a query in the url?

    thanks!

  • #10 / May 01, 2011 2:03am

    Chad Crowell

    242 posts

    Hey quick follow up question.  Would it be good practice to, for instance, have a _header template that is embedded on all pages and place the simple search form that lives in that header into a snippet so that it is not cached if I wanted the _header cached?

  • #11 / May 02, 2011 6:32pm

    Brandon Jones

    5500 posts

    Hi Chad,

    In that case the snippet would indeed be cached as well as part of the _header template.

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

ExpressionEngine News!

#eecms, #events, #releases