|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
kevinreeves Smarty n00b
Joined: 10 Mar 2004 Posts: 1
|
Posted: Wed Mar 10, 2004 10:53 pm Post subject: "unless" modifier function |
|
|
Hi everyone,
I've found myself wanting a way to do a sort of conditional statement
when displaying things like colors around text. I would really like to
use the common "( if true ) ? true : false;" syntax, but there doesn't
seem to be anything analogous in Smarty. So, I've developed this
modifier function that I've called "unless".
unless.php
--------------------------------------------
Code: |
function unless( $string, $if, $else )
{
$condition = "\$ret = ( $if ) ? \$else : \$string;";
eval( $condition );
return( $ret );
}
|
foo.tpl
--------------------------------------------
Code: |
<font style="color:{"green"|unless:"$balance < 0":"red"}">
{$balance}
</font>
|
The problem that I'm having is in the use of smarty variables in the
params to the unless function ( i.e. $balance ). When that variable is an
associative array, it does not work as I'd like. For example, when it's
used inside of a {foreach} loop:
Code: |
{ foreach from=$transactions item=trans }
<font style="color:{"green"|unless:"$trans.balance < 0":"red"}">
{$trans.balance}
</font>
{ /foreach }
|
In this example, "$trans.balance" is not passed in as the value of that
array element, but rather as "Array.balance".
I welcome any opinions on how this could be better written. Or, perhaps,
there's a better "Smarty" way to do this.
Thanks,
-- kevin |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Mar 10, 2004 11:09 pm Post subject: |
|
|
{"green"|unless:"$trans.balance < 0":"red"}
{if $trans.balance gte 0}green{else}red{/if}
What are you gaining besides obfuscation? |
|
Back to top |
|
|
|
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
|