Suggestion - add modifier contains to core smarty plugins
Posted: Wed Jun 10, 2009 10:23 pm
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
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
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: */
?>
Alane