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

notempty plugin (shorthand conditional statement)

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


Joined: 14 Feb 2006
Posts: 15

PostPosted: Thu Feb 01, 2007 11:43 am    Post subject: notempty plugin (shorthand conditional statement) Reply with quote

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 Smile
Back to top
View user's profile Send private message
JasonDS
Smarty Rookie


Joined: 25 Jan 2007
Posts: 25

PostPosted: Thu Feb 01, 2007 6:33 pm    Post subject: Reply with quote

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


Joined: 14 Feb 2006
Posts: 15

PostPosted: Thu Feb 01, 2007 6:47 pm    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Feb 01, 2007 7:09 pm    Post subject: Reply with quote

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


Joined: 14 Feb 2006
Posts: 15

PostPosted: Thu Feb 01, 2007 7:31 pm    Post subject: Reply with quote

nice one... I like the "iif" name too Very Happy I might consider ripping off your idea Very Happy

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 Very Happy)
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Feb 01, 2007 7:57 pm    Post subject: Reply with quote

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


Joined: 14 Feb 2006
Posts: 15

PostPosted: Thu Feb 01, 2007 8:18 pm    Post subject: Reply with quote

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 Laughing

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 Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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