Suggestion - add modifier contains to core smarty plugins

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
curlypinky
Forum Members
Forum Members
Posts: 109
Joined: Thu Sep 04, 2008 3:49 am
Location: Hawaii

Suggestion - add modifier contains to core smarty plugins

Post 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
alby

Re: Suggestion - add modifier contains to core smarty plugins

Post 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
rlparker25
Forum Members
Forum Members
Posts: 57
Joined: Thu Jan 17, 2008 3:58 pm

Re: Suggestion - add modifier contains to core smarty plugins

Post 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
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Suggestion - add modifier contains to core smarty plugins

Post by Nullig »

As the first poster stated, save the file as:

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

Nullig
Post Reply

Return to “Developers Discussion”