View previous topic :: View next topic |
Author |
Message |
NoWhereMan Smarty Rookie
Joined: 14 Feb 2006 Posts: 15
|
Posted: Thu Feb 01, 2007 11:43 am Post subject: notempty plugin (shorthand conditional statement) |
|
|
This is very useful for simple conditional tests
Code: |
<?php
/**
* Smarty notempty modifier plugin
*
* Type: modifier
* Name: notempty
* Purpose: print a message if the input variable is not empty or null
* @author Edoardo Vacchi (NoWhereMan)
* @param string
* @param string
* @return string
*/
function smarty_modifier_notempty($string, $default = '')
{
if (isset($string) && $string !== '')
return $default;
}
?>
|
typical usage:
Code: |
{if $val }
some content with {$other_var}
{/if}
{$val|notempty:"some content with $other_var"}
|
you can simulate if/else as well:
Code: | {if $val }
some content with {$other_var}
{else}
some other content
{/if}
{$val|notempty:"some content with $other_var"|default:'some other content'}
|
Hope you'll like it |
|
Back to top |
|
JasonDS Smarty Rookie
Joined: 25 Jan 2007 Posts: 25
|
Posted: Thu Feb 01, 2007 6:33 pm Post subject: |
|
|
You may want to use count_characters:
Code: |
{* Example: {$foo|count_characters} *}
{if $foo|count_characters > 0}
{/if}
|
Also, for default just use 'default'
Code: |
{$foo|default:"This will display if there is no value"}
|
|
|
Back to top |
|
NoWhereMan Smarty Rookie
Joined: 14 Feb 2006 Posts: 15
|
Posted: Thu Feb 01, 2007 6:47 pm Post subject: |
|
|
JasonDS wrote: | You may want to use count_characters:
Code: |
{* Example: {$foo|count_characters} *}
{if $foo|count_characters > 0}
{/if}
|
Also, for default just use 'default'
Code: |
{$foo|default:"This will display if there is no value"}
|
|
default works fine as a combo with notempty to do the "else" part.
notempty will save you a good number of chars if you want to do something like
<input type="text" id="user" name="user" {$user_isvalid|notempty:'class="required"'} />
that's the reason for which I implemented it |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Feb 01, 2007 7:09 pm Post subject: |
|
|
Interesting but I wonder if it would be better if it was implemented more like a real inline if (iif). ie:
Code: | {$expression|iif:result_for_true:result_for_false} |
Untested:
Code: | function smarty_modifier_iif($condition, $iftrue, $iffalse='')
{
if( $condition ) {
return( $iftrue );
} else {
return( $iffalse );
}
} |
Just a thought; however, in my experience, it will lead to some confusing constructions in your templates. |
|
Back to top |
|
NoWhereMan Smarty Rookie
Joined: 14 Feb 2006 Posts: 15
|
Posted: Thu Feb 01, 2007 7:31 pm Post subject: |
|
|
nice one... I like the "iif" name too I might consider ripping off your idea
it's not so meaningless or confusing, it's just a kind of perlism IIRC, as a said, considere adding a particular class if a given var is set and evaluates to true
It's really worthy IMO (I would even go farther suggesting to add it in the core plugins ) |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Feb 01, 2007 7:57 pm Post subject: |
|
|
A big problem is that Smarty doesn't support parenthesis or any other grouping/nesting device of that sort. So you are going to want to do things like {$foo|iif:($bar|iif:$baz:$belie):$blitzen} -- but of course can't.
The thing is, even if Smarty had that ability, those constructs are somewhat confusing and I've written zillions of them in other languages. Don't get me wrong, I love ternaries but I think that they can be somewhat opaque -- and IMO, that is the last thing you want to be in a template layer.
best regards and have fun! |
|
Back to top |
|
NoWhereMan Smarty Rookie
Joined: 14 Feb 2006 Posts: 15
|
Posted: Thu Feb 01, 2007 8:18 pm Post subject: |
|
|
boots wrote: | A big problem is that Smarty doesn't support parenthesis or any other grouping/nesting device of that sort. So you are going to want to do things like {$foo|iif:($bar|iif:$baz:$belie):$blitzen} -- but of course can't. |
I see your point, but someone using all those nested constructs in a template should be brought to a sanatory
Quote: | The thing is, even if Smarty had that ability, those constructs are somewhat confusing and I've written zillions of them in other languages. Don't get me wrong, I love ternaries but I think that they can be somewhat opaque -- and IMO, that is the last thing you want to be in a template layer.
best regards and have fun! |
thank you, and thanks for the ideas |
|
Back to top |
|
|