0

I posted this in the Stash support forum on Devot-ee, but there's more activity over here so I thought I'd try my luck in both locations. Not sure about the etiquette of posting in two forums, so apologies if it bends any noses. Here's my situation:

I have a site in which the user can turn various sections (Books, Screenwriting, News, Events etc) on or off depending on whether they are relevant to them. Whether a specific section is on or off is recorded in a Global Variable—eg. gv_include_books_section (value=[y/n]).

On the homepage I have a few columns, each of which I want to display recent entries from one section or another. The catch of course is that I don’t know in advance which sections will be on or off, AND I don’t want to use entries from the same section in more than one column. My solution is to only show entries from a section if a) it is switched ‘on’ AND b) it hasn’t been used earlier in the template. (a) is easy to check as it’s already stored in a Global Variable, and I’m using Stash to set a flag to record whether a specific section has already been shown.

I’m also using IfElse since these conditionals necessarily consist of if/else chains (Switchee got too complex as all the branchings required multiple nested Switchee tags). As far as I can see the code I have should work, but it doesn’t. I presume the issue is parse order but I'm unsure how to rectify it.

In addition to the template code; the global variables in play are:

gv_include_books_section = “n”
gv_include_screenwriting_section = “y”
gv_include_journalism_section = “y”

The template code is:

{exp:stash:set_value name="books_used" value="0" type="snippet"}
{exp:stash:set_value name="screen_used" value="0" type="snippet"}
{exp:stash:set_value name="journalism_used" value="0" type="snippet"}

{!-- column_1 --}
{exp:ifelse parse="inward"}    
    {if gv_include_books_section == "y"}
        {exp:stash:set_value name="books_used" value="1" type="snippet" replace="yes"}
        {snp_home_books}
    {if:elseif gv_include_screenwriting_section == "y"}
        {exp:stash:set_value name="screen_used" value="1" type="snippet" replace="yes"}
        {snp_home_screen}
    {if:else}
        <section>Oops</section>
    {/if}
{/exp:ifelse}

{!--  columm-2 --}
{exp:ifelse parse="inward"}    
    {if gv_include_screenwriting_section == "y" && screen_used == "0"}
        {exp:stash:set_value name="screen_used" value="1" type="snippet" replace="yes"}
        {snp_home_screen}
    {if:elseif gv_include_journalism_section == "y" && journalism_used == "0"}
        {exp:stash:set_value name="journalism_used" value="1" type="snippet" replace="yes"}
        {snp_home_journo}
    {if:else}
        <section>Oops</section>
    {/if}
{/exp:ifelse}

By my reckoning column_1 should display content from the screenwriting section (because the Books section is turned off), and column_2 should show the journalism section (because the Screenwriting section has already been used in Column 1). What’s actually happening, however, is that both columns are showing screenwriting content.

Um… help please? Thanks.

1

I'm no Mark Croxton, but I'd bet this has to do with the parsing order of the Stash variable you are setting inside the column 1 advanced conditionals. You need to make sure that column 2 isn't parsed until column 1 is fully parsed, so that those variables are set prior to column 2's IfElse running. In my understanding, the best way to do that would be make column 2 a Stash embed set to process="end".

Try this:

{!-- column_1 --}
{exp:ifelse parse="inward"}    
    {if gv_include_books_section == "y"}
        {exp:stash:set_value name="books_used" value="1" type="snippet" replace="yes"}
        {snp_home_books}
    {if:elseif gv_include_screenwriting_section == "y"}
        {exp:stash:set_value name="screen_used" value="1" type="snippet" replace="yes"}
        {snp_home_screen}
    {if:else}
        <section>Oops</section>
    {/if}
{/exp:ifelse}

{stash:embed:partials:column_2 process="end"}

Then, in your (newly created) /stash_templates/partials/column_2.html file:

{!--  columm-2 --}
{exp:ifelse parse="inward"}
    {if gv_include_screenwriting_section == "y" && screen_used == "0"}
        {exp:stash:set_value name="screen_used" value="1" type="snippet" replace="yes"}
        {snp_home_screen}
    {if:elseif gv_include_journalism_section == "y" && journalism_used == "0"}
        {exp:stash:set_value name="journalism_used" value="1" type="snippet" replace="yes"}
        {snp_home_journo}
    {if:else}
        <section>Oops</section>
    {/if}
{/exp:ifelse}

I haven't tested that, but that's where I'd begin.

| improve this answer | |
  • Hi Stephen, thanks for your input, and also for adding the pointer link over on Devot-ee. I'll give that idea a go, definitely. Yes, I suspected that the problem was that the 'x_used' flags were being set too early in the parse order. I just had no idea how to avoid that. I'll take a closer look at Stash's embed features as you suggest; so far I've only dipped my toe into Stash, using it for saving simple values and code snippets; clearly a deeper knowledge will do me many favours. Anyway, I'll report back once I've tested your idea. Thanks. – Rick Lecoat Oct 20 '13 at 15:10
  • 1
    Stephen, you're the man. That absolutely worked. And although my example code showed two columns, my template in fact requires three or four, which I can achieve by simply chaining the embeds (the embed for col2 ends with an embed for col3, and so on). Thanks. – Rick Lecoat Oct 20 '13 at 17:15
  • Cool. Glad it worked. – Stephen Callender Oct 20 '13 at 19:07

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.