Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Array_key_exists analogue for Smarty...

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
amagrude
Smarty n00b


Joined: 02 Dec 2004
Posts: 2

PostPosted: Wed Dec 08, 2004 3:19 am    Post subject: Array_key_exists analogue for Smarty... Reply with quote

To prevent the 'index doesn't exist' problem when dealing with arrays, I took a code snippet I noticed in the Features Request area and banged into the shape that I needed for my project.

This modifier let's you do handy things list this:

Code:

{if 'email'|isset:$ErrorList}
    {$ErrorList.email}
{/if}


which I use to conditionally intersperse validation errors amongst input fields on forms. Before this approach, I was building corresponding true/false arrays which were guaranteed to have all indexes set and took the place of the isset modifier above. While that worked, that wasn't pleasing...

Plugin code:
Code:

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.isset.php
 * Type:     modifier
 * Name:     isset
 * Purpose:  array_key_exists analogue for smarty
 * -------------------------------------------------------------
 *
 * Sample Usage:    {if ('index'|isset:$Array)}
 *
 * QUOTES ARE NECESSARY
 */

function smarty_modifier_isset($Index, $Array)
{
    return array_key_exists($Index, $Array);
}
?>


As an interesting exercise, I thought about writing one which would work for the general case of any PHP type, but I can't seem to figure out a syntax other than:

{if (''|isset:$Var)}

which isn't the cleanest syntax. Is there another way to accomplish this?

Andrew
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Dec 08, 2004 4:03 am    Post subject: Reply with quote

I guess easier is for you to decide Smile Here are some ideas--

Code:
{if array_key_exists('email', $ErrorList)}


You can use PHP functions in if statements -- they also work as modifiers when security is off (default):

Code:
{if 'email'|array_key_exists:$ErrorList)}


Another thing you can do is set $smarty->error_reporting = E_ALL & ~E_NOTICE; (see: http://smarty.php.net/manual/en/variable.error.reporting.php ) then you can use the much simpler:

Code:
{if $ErrorList.email}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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