View previous topic :: View next topic |
Author |
Message |
ericingram Smarty Rookie
Joined: 16 Nov 2011 Posts: 21
|
Posted: Tue Feb 07, 2012 12:29 am Post subject: Variable assignment from {if ...} statement |
|
|
I think this used to work in Smarty 2, but I get an error when trying to do this...
{if $something = get('something')}
...
{/if}
Why doesn't this work? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Feb 07, 2012 4:36 pm Post subject: |
|
|
It does work in Smarty 3.
What error did you get? |
|
Back to top |
|
ericingram Smarty Rookie
Joined: 16 Nov 2011 Posts: 21
|
Posted: Wed Feb 08, 2012 12:05 am Post subject: |
|
|
OK I suppose that does work, but I rarely need to do that compared to this, which does not work:
{if !$something = get("something")}
...
{/if}
Here's the error:
Syntax Error in template "../something.html" on line 6 "{if !$something = get("something")}" - Unexpected " = " |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Feb 08, 2012 4:26 pm Post subject: |
|
|
Assigments in {if} did not work at all in Smarty 2.
Smarty 3 does allow simple assignments but no combinations with logical operators.
The workaround is
Code: | {$something = get("something")}
{if !$something}
...
{/if} |
|
|
Back to top |
|
ericingram Smarty Rookie
Joined: 16 Nov 2011 Posts: 21
|
Posted: Wed Feb 08, 2012 4:32 pm Post subject: |
|
|
Having assignment in {if} is great to have and I'm enjoying working with Smarty 3. Any chance you'll be able to add logical combinations like this in the future? Or can you think of another way to make it work in one line?
Thanks a lot! |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Wed Feb 08, 2012 4:38 pm Post subject: |
|
|
curious... does this work?
ericingram wrote: |
{if ! ($something = get("something")) }
...
{/if}
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Feb 08, 2012 4:49 pm Post subject: |
|
|
Code: | {if $something = !get("something")}
...
{/if} |
does work. |
|
Back to top |
|
ericingram Smarty Rookie
Joined: 16 Nov 2011 Posts: 21
|
Posted: Wed Feb 08, 2012 9:06 pm Post subject: |
|
|
Yes! This is beautiful, and kind of interesting. I think it would produce a boolean result in plain PHP? But it makes my statements more readable.
Thank you! |
|
Back to top |
|
|