croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
197 stars 20 forks source link

Embedded variables require brackets to evaluate in conditionals #122

Closed noslouch closed 10 years ago

noslouch commented 10 years ago

I think this might be related to #118. If I embed a variable like so:

/viewModels.group/home.html

{stash:embed:wrappers:home stash:sort="true"}

{stash:sort} will only evaluate in a conditional if I wrap it in brackets. /wrappers/home.html

{if {stash:sort}}
do some stuff
{/if}

The recommended method to evaluate variables in EE is without brackets or quotes.

I'm using stash 2.5.8 and EE 2.9.0.

croxton commented 10 years ago

"Native" stash variables (and embed variables are the same thing as stash variables) are not in the snippet/global variables array, and therefore can only be evaluated as strings in if/else conditionals. That means surrounding the variable with quotes if the value is not an integer.

If you prefer you can set snippet variables instead like this:

 {stash:embed:wrappers:home}
 {exp:stash:set name="sort" type="snippet"}asc{/exp:stash:set}

And evaluate in wrappers:home like any other global variable:

{if sort == "asc"}
   Sorting direction: {asc}
{/if}

If you're wondering why there are two variable types, consider that EE and other template tags iterate over the global variables array many times when parsing a template. More variables == more overhead.

noslouch commented 10 years ago

Could I get around using a snippet and using quotes/brackets if I set the embed variable to an integer instead of a string? I'm really just using it as a boolean.

On Mon, Oct 20, 2014 at 12:21 PM, Mark Croxton notifications@github.com wrote:

"Native" stash variables (and embed variables are the same thing as stash variables) are not in the snippet/global variables array, and therefore can only be evaluated as strings in if/else conditionals. That means surrounding the variable with quotes if the value is not an integer.

If you prefer you can set snippet variables instead like this:

{stash:embed:wrappers:home} {exp:stash:set name="sort" type="snippet"}asc{/exp:stash:set}

And evaluate in wrappers:home like any other global variable:

{if sort == "asc"} Sorting direction: {asc} {/if}

If you're wondering why there are two variable types, consider that EE and other template tags iterate over the global variables array many times when parsing a template. More variables == more overhead.

— Reply to this email directly or view it on GitHub https://github.com/croxton/Stash/issues/122#issuecomment-59792127.

croxton commented 10 years ago

No, {stash:sort} is just a placeholder that gets replaced with a string. To use it as stash:sort in a conditional would require it to be in the globals array, i.e. it has to be a snippet type.

noslouch commented 10 years ago

Gotcha. Would you accept an edit from me on the wiki to include this note?

On Monday, October 20, 2014, Mark Croxton notifications@github.com wrote:

No, {stash:sort} is just a placeholder that gets replaced with a string. To use it as stash:sort in a conditional would require it to be in the globals array, i.e. it has to be a snippet type.

— Reply to this email directly or view it on GitHub https://github.com/croxton/Stash/issues/122#issuecomment-59794736.

croxton commented 10 years ago

Sure, although there is this already: https://github.com/croxton/Stash/wiki/Variables#ifelse-advanced-conditionals