I've taken on this EE2 project and I don't have much experience with it.
I've installed two plugins, SessionVariables and Freebies, to fetch a url segment and act according to its value.
So, basically:
{if "{exp:freebie:any name="es"}" == "true"}
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
{if:else}
{exp:session_variables:delete name="current_language"}
Hell开发者_StackOverflowo World!
{/if}
The strange thing is that whan I go to site.com/es/hello it prints "Hola Mundo" but it actually calls both function set() and delete() of session_Variable's plugin.
I'ts driving me crazy, I'd appreciate any help you can provide.
http://www.putyourlightson.net/projects/session_variables
https://github.com/averyvery/Freebie
When using advanced conditionals in EE, all tags within those conitionlas are parsed regardless, as advanced conditionals are parsed later than most other tags in the parse order. This is why the session_variables
tag is being called within both conditions in your example (even though only the first bit of content is displayed by the template parser).
The fix is to use "simple conditionals", which are parsed much earlier:
{if "{exp:freebie:any name="es"}" == "true"}
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
{/if}
{if "{exp:freebie:any name="es"}" == "false"}
{exp:session_variables:delete name="current_language"}
Hello World!
{/if}
Since this came up pretty early on a Google search I figure I'd mention the add-on IfElse. It'll return the proper condition, the ones that fail (return false) are stripped out. Ergo only the desired condition is run.
As Derek said knowledge of the parse order is vital, in this case both calls to {exp:session_variables:...}
are being run regardless of the validity of the conditions. However, with IfElse your code would run as desired while avoiding advanced conditionals so not only do you execute what you want it does it much earlier in the parse order to boot.
E.g.
{exp:ifelse parse="inward"}
{if "{exp:freebie:any name="es"}" == "true"}
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
{if:else}
{exp:session_variables:delete name="current_language"}
Hello World!
{/if}
{/exp:ifelse}
When "{exp:freebie:any name="es"}" == "true"
then {exp:session_variables:set name="current_language" value="_es"}
will run and not {exp:session_variables:delete...}
. As if your code read:
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
Also, again, this will happen at the parse order of {exp:ifelse...}
, not after advanced conditionals.
The one caveat to note is that you cannot nest {exp:ifelse...}
.
There are probably other add-ons like IfElse (check out Switchee by the same author) for the same or similar contexts, but this is what I know and use.
精彩评论