Page 1 of 1

Suggestion - add modifier contains to core smarty plugins

Posted: Wed Jun 10, 2009 10:23 pm
by curlypinky
I found a great plugin for the smarty lib today that allows the modifier 'contains' for searching strings for a particular value - I used this to create a conditional statement to allow my news template to discern between pdf and docs and image files so it would create the correct link. I think the addition of this plugin to the core installation would be an asset.

I found this at http://www.phpinsider.com/smarty-forum/ ... hp?t=10384
posted by JasonDS and used joeri210's version (code below)

saved file as: lib/smarty/plugins/modifier.contains.php

Code: Select all

<?php

/**
 * Smarty shared plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Function: smarty_contains
 * Purpose:  Used to find a string in a string
 * Example: contains( 'Jason was here', 'here' ) returns true
 * Example2: contains( 'Jason was here', 'ason' ) returns false
 * Usage string: {$foo|contains:"bar"}
 * Usage array: {$foo|@contains:"bar"}
 * @author Jason Strese <Jason dot Strese at gmail dot com>
 * @param string
 * @return string
 */
function smarty_modifier_contains($string, $find, $cases = false)
{
   $count = 0;
   if( is_string($string) && !empty($string) )
   {
      if($cases) $count = substr_count($string, $find);
      else $count = substr_count(strtolower($string), strtolower($find) );
   }
   elseif( is_array($string) && count($string) )
   {
      if($cases)
      {
         foreach($string as $str) {
            if($str == $find) $count++;
         }
      } else {
         foreach($string as $str) {
            if(strtolower($str) == strtolower($find)) $count++;
         }
      }
   }
   return $count;
}

/* vim: set expandtab: */ 
?>
If you don't think this belongs in the core, how is the correct way to add this in without putting it in core directories?
Alane

Re: Suggestion - add modifier contains to core smarty plugins

Posted: Thu Jun 11, 2009 7:14 am
by alby
curlypinky wrote: If you don't think this belongs in the core, how is the correct way to add this in without putting it in core directories?
plugins folder

Alby

Re: Suggestion - add modifier contains to core smarty plugins

Posted: Fri Jun 26, 2009 6:37 pm
by rlparker25
hi,

i am having the same problem and could really use this contains plugin

i have tried uploading it the plugins folder (I found tow and have tried both) and I get ann error saying it needs adding to core.load_plugins.php

Could someone please tell me how to add a smarty plugin?

thanks

Rachael

Re: Suggestion - add modifier contains to core smarty plugins

Posted: Fri Jun 26, 2009 11:50 pm
by Nullig
As the first poster stated, save the file as:

save the file as: lib/smarty/plugins/modifier.contains.php

Nullig