2

Lets say I have this code

{exp:channel:entries
  limit="3"
  channel="inventory"
  paginate="bottom"
}
...
{/exp:channel:entries}

Lets say I want to make one of the parameters dynamic based on a result of a plugin tag

{exp:channel:entries
     limit="3"
     {if {exp:foo:bar param_key="param_value"}}
       channel="inventory"
     {/if}
     paginate="bottom"
 }
 ...
{/exp:channel:entries}

When I run this, I get a parse error.

How do you solve this parse order problem?

UPDATE:

I am now using Stash to be able to set variables.

So what I did was set the variable....

{if {exp:foo:bar param_key="param_value"}}
     {exp:stash:set name="my_inventory_name"}inventory="{segment_2}"{/exp:stash:set}
{if:else}
     {exp:stash:set name="my_inventory_name"}{/exp:stash:set}
{/if}

then use it inside the tag

 {exp:channel:entries
         limit="3"
         {exp:stash:get name="my_inventory_name"}
         paginate="bottom"
 }
 ...
 {/exp:channel:entries}

No more parse errors. But based on the results, it seems that the stash variable was not parsed because the results did not filter by it.

2

Please take a look on ExpressionEngine’s Parse Order PDF by Lodewijk Schutte

Advanced conditionals (like the one that you have) are parsed after module tags. So such setup will not work.

This blog post by Derek Allard explains the difference between simple and advanced conditionals very well.

Depending on your exact requirements, you might find helpful some third-party add-ons, like Low Variables, CE Variables or Switchee.

| improve this answer | |
  • I updated my question with more information. – developarvin Feb 4 '13 at 8:13
  • AFAIK, the updated situation is the same. {if {exp:foo:bar param_key="param_value"}} is advanced conditional and is executed AFTER stash is set. If you're simply checking whether URL segment is set you can use {if segment_2} – Yuri Salimovskiy Feb 4 '13 at 8:26
  • I used {if segment_2} initially. But then I need to add more conditions(if segment_2 matches regex a, set param else don't set param). – developarvin Feb 4 '13 at 8:32
  • Try Switchee for that - might help. I think you cal also put the check to snippet - they are processed earlier in line. Not sure however when complex conditionals in snippet will be executed. EE parse order is a complex thing :) – Yuri Salimovskiy Feb 4 '13 at 8:41
  • Already tried Switchee instead of using the native if/else conditional. Still didn't work :/ – developarvin Feb 4 '13 at 8:45
2

When you pass a tag as a parameter you need to use parse="inward" to tell the template parser to parse each of the parameter values. You also need to be careful to use double quotes to wrap the value and single quotes for the tag passed as a parameter (or vice versa - just be consistent). Finally, never use line-breaks in a parameter value - this will choke the parser. Example:

{exp:channel:entries
     limit="3"
     channel="{exp:stash:get name='my_inventory_name'}"
     parse="inward"
}

In you updated question you are trying to set a Stash variable based on an advanced conditional. This will never work as expected because of the way EE parses advanced conditionals: all possible outcomes are parsed, and the non-matching condition removed at the end of template parsing. Instead, use Switchee, IfElse or Stash's own match/against and default parameters to set the value:

{!-- match non-empty values only, use default to set the fallback value --}
{!-- using trim="yes" to remove linebreaks / whitespace --}
{exp:stash:set 
    name="my_inventory_name" 
    match="#\S+#" 
    against="{exp:foo:bar param_key='param_value'}" 
    default="default_channel" 
    trim="yes"
    parse="inward"
}
    {segment_2}
{/exp:stash:set}
| improve this answer | |
0

What if you try it like this?

{exp:chanel:entries
     limit="3"
     channel="{if {exp:foo:bar param_key='param_value'}}inventory{/if}"
     paginate="bottom"
     parse="inward"
 }

Remember to add the parse='inward' parameter.

– Wouter

| improve this answer | |
  • Having the conditional there, which is an advanced one, will still cause that to fail. – Low Feb 1 '13 at 10:25

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.