2

I have a feeling I'm missing something incredibly basic but I'm stuck so here goes. I have a basic html page {stash:embed:layouts:standard} that holds a 2-col layout. The main column and sidebar have content from the "site" channel but in the sidebar the user has a choice to embed 1 (the most recent) item from the "news" channel (using a PT switch field). The standard way I know but I'm stumped on doing this the stash way.

layouts:standard is basically

 <div class="container">

    {exp:stash:get name="intro"}

    <div class="main" role="main">
        {exp:stash:get name="main_content"}
    </div><!-- .main -->

    <div class="sub">
        {!-- this should be conditional --}
        {exp:stash:get name="nieuws"}

        {exp:stash:get name="sidebar"}
    </div><!-- .sub -->
</div><!-- .container -->

My main template is simply

{!-- main layout --}
{stash:embed:layouts:standard} 

{!-- main content --}

{exp:channel:entries channel="site" disable="member_data|pagination" category="{segment_1_category_id}" dynamic="off" url_title="{segment_2}"}
{if no_results}{redirect="404"}{/if}

{exp:stash:set name="banner"}{cf_site_main_image}{/exp:stash:set}

{exp:stash:set name="main_title"}{cf_site_main_title}{/exp:stash:set}

{exp:stash:set name="sub_title"}{cf_site_sub_title}{/exp:stash:set}

{exp:stash:set name="intro"}{intro}{/exp:stash:set}

{exp:stash:set name="main_content"}{body}{/exp:stash:set}

{exp:stash:set name="sidebar"}{sidebar}{/exp:stash:set}

 {if cf_site_add_news == "y"}
    {!-- the single news entry here only if switch set to yes --}
 {/if}

{/exp:channel:entries}

{!-- news item for sidebar --}

{exp:stash:set name="nieuws" save="yes"  scope="site" parse_tags="yes"replace="yes" refresh="240"output="yes"}
{exp:channel:entries channel="nieuws" disable="categories|category_fields|member_data|pagination" dynamic="off" limit="1"}
    {cf_nieuws_content}
{/exp:channel:entries}
{/exp:stash:set}

What trips me up is the conditional if statement {if cf_site_add_news == "y"} - how and/or where do I process it? Am I making it clear enough?

1

First, capture the value of cf_site_add_news:

{exp:stash:set name="site_add_news" type="snippet"}{cf_site_add_news}{/exp:stash:set}

(Note that setting type="snippet" means the variable can be evaluated as a simple conditional)

Then you can use a conditional in your layout like this:

{if site_add_news == "y"} 
{stash:nieuws}
{/if}

(Note that in a stash embed, stash variables can be accessed using the syntax {stash:my_var})

| improve this answer | |
  • I had tried this but forgot the "snippet" part. Thanks Mark, very educational. This {stash:nieuws} is shorthand for {exp:stash:get name="nieuws"}? (haven't seen this before) – erwinheiser Feb 7 '13 at 10:08
  • No, {exp:stash:nieuws} is actually the "short syntax" version of the tag, {stash:nieuws} is a placeholder, in the same sense as a snippet / global variable in an EE template (these get replaced into the template rather than parsed as tags). Either can be used, but in a Stash embed it is more efficient to use a placeholder (since no extra tag parsing is required) – Mark Croxton Feb 7 '13 at 12:30
  • Thanks for the wisdom bombs Marc, feel like I'm one step closer to stash-master level. – erwinheiser Feb 7 '13 at 18:23
1

Ok, let's see if I understand. I try to do all my logic outside of my wrapper template whenever possible. If the content of your sidebar can change based on user choices, I would put the conditional logic in the partial template.

I would first build my different sidebar pieces, then assemble them using conditional logic further down.

Something along the lines of:

{!-- main layout --}
{stash:embed:layouts:standard} 

{!-- main content --}
{exp:channel:entries channel="site" disable="member_data|pagination" category="{segment_1_category_id}" dynamic="no" url_title="{segment_2}"}
    ...
    {exp:stash:set name="sidebar_site"}{sidebar}{/exp:stash:set}
    {exp:stash:set name="sidebar_add_news"}{cf_site_add_news }{/exp:stash:set}
{/exp:channel:entries}

{!-- news item for sidebar --}
{exp:stash:set_list name="sidebar_nieuws" parse_tags="yes" save="yes" replace="no" refresh="240" scope="site"}
    {exp:channel:entries channel="nieuws" disable="categories|category_fields|member_data|pagination" dynamic="no" limit="1"}
        {stash:nieuws_content}{cf_nieuws_content}{/stash:nieuws_content}
    {/exp:channel:entries}
{/exp:stash:set_list} 

{!-- build sidebar --}
{exp:stash:set name="sidebar"}

    {exp:stash:sidebar_site}

    {if "{exp:stash:sidebar_add_news}" == "y"}
        {exp:stash:get_list name="sidebar_nieuws"}
            {nieuws_content}
        {/exp:stash:get_list}
    {/if}

{/exp:stash:set}

You could further optimise this using switchee or ifelse

EDIT: as suggested by Mark, using type="snippet" will get you a (leaner) simple conditional

| improve this answer | |
  • Thanks Jerome, definitely a cool approach, although I actually prefer to have my conditionals in my layout so I can see what's being loaded. But two different approaches is educational :) – erwinheiser Feb 7 '13 at 10:11
  • Well ... As a rule of thumb, I would essentially keep my main templates free of as much logic as I can to separate logic from presentation. Wrapper templates are just holding the general structure of pages and "buckets" that can be filled with whatever the page requires in terms of content. Putting logic in these defeats their "raison d'être" which is to be as generic as possible. Logic should be elsewhere IMHO. Mark is offering a compelling argument for that principle over here: expressionengine.stackexchange.com/a/1749/141 – Jérôme Coupé Feb 7 '13 at 10:28
  • Jérôme is right, where practical the layout view shouldn't be concerned with presentational logic, it should just provide "blocks" for content to be injected into. That said, if the display of that sidebar is not going to change - this is a simple site - then there's no need to over-engineer the solution. But if you do indeed need to inject different content or layouts into the sidebar then definitely use partials. – Mark Croxton Feb 7 '13 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.