Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Variable assignment from {if ...} statement

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Tue Feb 07, 2012 12:29 am    Post subject: Variable assignment from {if ...} statement Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Tue Feb 07, 2012 4:36 pm    Post subject: Reply with quote

It does work in Smarty 3.

What error did you get?
Back to top
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Wed Feb 08, 2012 12:05 am    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Feb 08, 2012 4:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Wed Feb 08, 2012 4:32 pm    Post subject: Reply with quote

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
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Feb 08, 2012 4:38 pm    Post subject: Reply with quote

curious... does this work?

ericingram wrote:

{if ! ($something = get("something")) }
...
{/if}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Feb 08, 2012 4:49 pm    Post subject: Reply with quote

Code:
{if $something = !get("something")}
...
{/if}


does work.
Back to top
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Wed Feb 08, 2012 9:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP