1

I'm new to Stash and Switchee and this is probably a parse order issue, but I am not quite sure how. Maybe someone that understands this and Switchee a bit more is able and wouldn't mind explaining why the below isn't working.

On my page template I embed my layouts/default template. At the top of that I have this Switchee conditional:

{exp:switchee variable="{segment_1}" parse="inward"}
  {case value=""}
    {sn-home-data}
  {/case}
  {case default="yes"}
    {sn-page-data}
  {/case}
{/exp:switchee}

The sn-page-data snippets contains this:

{exp:stash:set parse_tags="yes" parse_conditionals="yes"}
  {exp:channel:entries limit="1" channel="page" disable="categories|member_data|pagination" cache="yes" refresh="6400"}
    {stash:page_title}
      {title}
    {/stash:page_title}
  {/exp:channel:entries}
{/exp:stash:set}

When I am calling the {exp:stash:page_title} variable in the layouts/default template nothing returns.

If I remove parse="inward" from the Switchee tag it does work, but if I understand correctly this setting is needed to give Switchee that performance advantage over EE conditionals...

3

This is a tag parse order problem, and it arises because you are (if I understand you correctly) setting your 'page_title' variable in the view template (the embed) - and trying to access it in the same template. Something like this:

{exp:switchee variable="{segment_1}" parse="inward"}
  {case value=""}
    {sn-home-data}
  {/case}
  {case default="yes"}
    {sn-page-data}
  {/case}
{/exp:switchee}

{exp:stash:page_title}

Module tags are parsed outwards > and parse="inward" makes Switchee behave like a module tag, therefore the parse order is:

  1. Switchee parsed, returns the unparsed content of {sn-home-data} or {sn-page-data} to the template, depending on the value of {segment_1}

  2. {exp:stash:page_title} is parsed. It returns nothing, because the variable doesn't exist yet.

  3. Now either {sn-home-data} or {sn-page-data} are parsed and the value of {exp:stash:page_title} is set.

So why does removing parse="inward" from Switchee work? Because Switchee is a plugin tag, which are parsed last, after any module tags they wrap (effectively inwards > outwards"). Thus both {sn-home-data} or {sn-page-data} would be parsed in step 1.

The easiest solution to this problem is to move your Switchee expression to the parent template. It has no business being in a view template in any case, since you should really be trying to decouple your view and from the model/presentational logic.

If you don't want to do that then you can subvert parse order with the process parameter like so, to move the Stash get tag to the end of parsing:

{exp:switchee variable="{segment_1}" parse="inward"}
  {case value=""}
    {sn-home-data}
  {/case}
  {case default="yes"}
    {sn-page-data}
  {/case}
{/exp:switchee}

{exp:stash:get name="page_title" process="end"}
| improve this answer | |
  • Thank you so much for taking the time to explain this so brilliantly Mark. That makes perfect sense :) – Katrin Jan 27 '14 at 11:36
  • Since I have posted the question I have actually changed my set-up to do exactly what you have recommended: I am calling/setting the data with the help of Switchee tags in the parent template and am assembling/getting the data in the embed/view template. Took me some time to get my head around this, but am getting there :) Thank you for these great add-ons. – Katrin Jan 27 '14 at 11:42
0

So... is that second block of code supposed to be a "set" or a "get"?

I think you're just missing a process="end" on one of your blocks.

I would probably do more like so...

Set the page_title variable:

{exp:stash:parse}
   {exp:channel:entries limit="1" channel="page" 
    disable="categories|member_data|pagination" cache="yes" refresh="6400"}
      {exp:stash:set name="page_title" value="{title}"}
   {/exp:channel:entries}
{/exp:stash:parse}

Getting the variable later:

{exp:stash:parse process="end"}
    {stash:page_title}
{/exp:stash:parse}

The process="end" portion ensures that this gets parsed after the other one.

Note: You may still need the parse conditionals in your example, etc.

Also, as a footnote - I've problems with caching with Switchee. You may want to try doing this with the cache off. And then loop back to make the caching work right.

| improve this answer | |
  • Thanks Sam. I have added your proposed code (with the caching parameters removed) to the top of the template within the Switchee tags and then further down get the page_title with your code, but nothing returns. Again, if I remove parse="inward" from the Switchee tag it does work and returns the value. – Katrin Jan 19 '14 at 14:01

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.