0
{exp:channel:entries dynamic="no" category="{exp:stash:get name="current_category_id"}"}
    content
{/exp:channel:entries}

returns no results. However,

{exp:channel:entries dynamic="no" category="1"}
    content
{/exp:channel:entries}

returns the expected results, and

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

returns 1.

It seems to me that the first example should work. What am I missing?

Edit: here is how the stash is set. Apologies for its being somewhat contorted. segment_2_category_id is powered by seg2cat.

{exp:stash:set name="current_category_id"}
{if {segment_2_category_id} == "91"}91
{if:elseif {segment_2_category_id}=="90"}90
{if:elseif {segment_2_category_id}=="4"}4
{if:elseif {segment_2_category_id}=="3"}3
{if:elseif {segment_2_category_id}=="2"}2
{if:else}1{/if}{/exp:stash:set}
  • 1
    Can you post the code where you initially 'stash' the category ID? – Ian May 29 '13 at 14:10
  • I've added the stash:set. – Michael May 29 '13 at 14:19
  • Are you just trying to get the value of segment_2 or only if it's one of those values specified (91,90,4,3,2,1)? – Ian May 29 '13 at 14:38
  • No, I'm just trying to set the default (no segment_2) to 1 and I happen to know what the other values will be. It's a kludgy hack. :( – Michael May 29 '13 at 15:02
  • Well take a look at Switchee then as it's much cleaner and gets around the parse order issue as well as handling a default value elegantly. Also use parse="inward" as mentioned. In my example below I also include the use of a pipe character | to provide a default in the category attribute so you can do it that way too. – Ian May 29 '13 at 15:11
2

Well from the looks of it you don't really need to use Stash at all. If you use something like Low Seg2Cat or URL Helper you can just pull in {segment_2_category_id} straight into your Channel Entries tag as the parameter value:

{exp:channel:entries dynamic="no" category="{segment_2_category_id}|1"}
    content
{/exp:channel:entries}

With regards to the Stash issue however it's to do with parse order as you're including several complex conditionals in your template. Stash provides several ways to circumvent the parse order but in this case you can greatly simplify your conditional logic to use either simple conditions (just if's - no elseif's).

| improve this answer | |
  • If it's just the value of segment_2 you're looking to pull then I'd use Seg2Cat. If it's a pre-defined list of specifically those values I'd probably look to tidy up the Stash set and use Switchee for a cleaner (and much more efficient) case statement. – Ian May 29 '13 at 14:48
  • Thanks; that pipe method works well for me; it was just segment_2 that I was interested in. I still can't get parse inward to deliver exactly the results I wanted but at least I know I've that option now, and I can experiment with it later to master it. – Michael May 29 '13 at 15:20
  • Seg2Cat should grab segment_2 but if you want to stick to the Stash approach then look at including parse_conditionals="yes"(github.com/croxton/Stash#parse_conditionals--yesno) in there. Keep it as simple as possible in those conditionals though. – Ian May 29 '13 at 15:28
  • Another thing to be aware of is that you are capturing the surrounding line breaks when setting the value of current_category_id. You cannot pass line breaks in a parameter value, as it confuses the EE parser. You can remove them when either setting or getting the Stash variable with the parameter trim="yes" – Mark Croxton May 30 '13 at 8:53
0

You have to set parse as "inward" and to pay attention to quotation marks.

{exp:channel:entries dynamic="no" category="{exp:stash:get name='current_category_id'}" parse="inward"}
    content
{/exp:channel:entries}
| improve this answer | |

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.