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.

Snippet with variable

January 16, 2013 4:29pm

Subscribe [2]
  • #1 / Jan 16, 2013 4:29pm

    G - B - O

    31 posts

    Hi there,

    I want to use a snippet for generating my ‘sidebar’, a div with a few minor channel entries in it. (address data, a proverb, etc.)
    The sidebar shows different entries on various pages: the channel entries are categorized by the page where they should appear: home, portfolio, etc.

    My snippet, called {sidebar} looks like this:

    {exp:channel:entries channel="information" category="8" fixed_order="12|13|14|15" sort="asc" dynamic="no" disable="pagination|member_data|category_fields"}
    <div id="{url_title}">
    {information_content}
    </div>
    {/exp:channel:entries}

    When i call the snippet from my homepage, i want to show entries with category=“8”, but on the portfolio category has to be “9”.
    I would use embedded templates for this, but they slow the pages down a lot more then snippets do.

    Is there a way of passing a variable into a snippet?

    Thanks!

  • #2 / Jan 16, 2013 8:24pm

    John St-Amand

    865 posts

    No a variable can’t be passed into a snippet natively (could be done with Stash), but if it’s fixed, as you seem to suggest why not use a simple conditional - perhaps on the basis of a URL segment, like:

    {if segment_1 == "portfolio"}
    {exp:channel:entries channel="information" category="9"}
    Your stuff here
    {/exp:channel:entries}
    {if:elseif segment _1 == ""}
    {exp:channel:entries channel="information" category="8"}
    Your stuff here
    {/exp:channel:entries}
    {if:else}
    
    {/if}

    With an early parsing add-on like switchee of ifelse (both from croxton) you might even be able to get away with wrapping only the category parameter with the conditional, avoiding this kind of duplication.

    Hope that helps.

  • #3 / Jan 17, 2013 4:35am

    G - B - O

    31 posts

    Thanks, i’ll check them out! Is there a suggestion you’d prefer?
    Obviously i want to keep my website as flexible as possible…

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

ExpressionEngine News!

#eecms, #events, #releases