Ternary Operator

1,945 views
Skip to first unread message

Matt

unread,
Oct 12, 2009, 8:21:57 PM10/12/09
to Smarty Developers
Does Smarty3 have support for the ternary operator? If not, could it
be added? It would be very useful to simplify common statements, such
as this:

<table>
{foreach $row as $r}
<tr class="{$r@iteration%2?'even':'odd'}">
<td>{$r}</td>
</tr>
{/foreach}
</table>

Karel S.

unread,
Oct 13, 2009, 5:44:45 AM10/13/09
to Smarty Developers
You can use the cycle plugin.

<tr class="{cycle values="even,odd"}">

http://www.smarty.net/manual/en/language.function.cycle.php

Matt

unread,
Oct 15, 2009, 2:41:46 AM10/15/09
to Smarty Developers
Thanks Karel. I wasn't aware of that particular plugin, which was a
wonderful solution to that problem. However, I am still curious as to
the status of the ternary operator in Smarty3, or any equivalent
simple conditional printing mechanism.

Digital 3i Soluções em Website

unread,
Oct 15, 2009, 10:08:02 AM10/15/09
to smarty-d...@googlegroups.com
Tews,

the ternary operator is extremely necessary because avoid big instrucions in the template code. For simple code, de cycle plugin meets the need, however for complex context it does not meet. For example:

instead of writing

{if $auditor == 1}
   Auditor Medical
{else}
   It's me
{/if}

I could use
{$auditor == 1 : 'Auditor Medical' : 'It's Me'}

Stay there the sugestion.

Att

André Pinheiro.

2009/10/15 Matt <ma...@frigidmedia.com>

mitchenall

unread,
Oct 15, 2009, 11:28:50 AM10/15/09
to Smarty Developers
The first is a lot more readable though, even to a non-programmer.

On Oct 15, 3:08 pm, Digital 3i Soluções em Website
<digita...@gmail.com> wrote:
> Tews,
>
> the ternary operator is extremely necessary because avoid big instrucions in
> the template code. For simple code, de cycle plugin meets the need, however
> for complex context it does not meet. For example:
>
> instead of writing
>
> {if $auditor == 1}
>    Auditor Medical
> {else}
>    It's me
> {/if}
>
> I could use
> {$auditor == 1 : 'Auditor Medical' : 'It's Me'}
>
> Stay there the sugestion.
>
> Att
>
> André Pinheiro.
>
> 2009/10/15 Matt <m...@frigidmedia.com>

Sander Aarts

unread,
Oct 15, 2009, 12:01:00 PM10/15/09
to smarty-d...@googlegroups.com
I'd welcome a ternary operator as well.
If {$x=$y==2?$foo:$bar} is not possible (syntax wise), maybe one of the following suggestions is:

{$x=($y==2)?$foo:$bar}
{$x=($y==2?$foo:$bar)}
{$x=?|$x==2:$foo:$bar}

cheers,
Sander


Digital 3i Soluções em Website schreef:

U.Tews

unread,
Oct 15, 2009, 5:25:08 PM10/15/09
to Smarty Developers
The ternary operator is impelmented now. It can be used on assignments
and output.

Examples:
{$x=($y==2)?$foo:$bar}
{assign var=x value=($y==2)?$foo:$bar}
{($y==2)?$foo:$bar}

If you modifiers you have to put the expression into paranteses to
avoid ambiguity of the ':'.
{($y==2)?($foo|truncate):$bar}

The condition always needs to be in paranteses.
> > 2009/10/15 Matt <m...@frigidmedia.com <mailto:m...@frigidmedia.com>>
>
> >     Thanks Karel.  I wasn't aware of that particular plugin, which was a
> >     wonderful solution to that problem.  However, I am still curious as to
> >     the status of the ternary operator in Smarty3, or any equivalent
> >     simple conditional printing mechanism.
>
> >     On Oct 13, 2:44 am, "Karel S." <ka...@hardware.info

Digital 3i Soluções em Website

unread,
Oct 15, 2009, 8:03:52 PM10/15/09
to smarty-d...@googlegroups.com
OK!

Very Very good

2009/10/15 U.Tews <uwe....@googlemail.com>

Sander Aarts

unread,
Oct 16, 2009, 4:15:20 AM10/16/09
to smarty-d...@googlegroups.com
Great, now I only have to convince my colleagues to update to Smarty 3 a.s.a.p. ;)

cheers,
Sander


U.Tews schreef:
Reply all
Reply to author
Forward
0 new messages