Added pad filter for strings (uses str_pad)

This commit is contained in:
Djamil Legato
2015-11-20 12:40:48 -08:00
parent 06d663680c
commit f9e137c994
2 changed files with 44 additions and 0 deletions

View File

@@ -70,6 +70,7 @@ class TwigExtension extends \Twig_Extension
new \Twig_SimpleFilter('randomize', [$this,'randomizeFilter']),
new \Twig_SimpleFilter('modulus', [$this,'modulusFilter']),
new \Twig_SimpleFilter('rtrim', [$this, 'rtrimFilter']),
new \Twig_SimpleFilter('pad', [$this, 'padFilter']),
new \Twig_SimpleFilter('safe_email', [$this,'safeEmailFilter']),
new \Twig_SimpleFilter('safe_truncate', ['\Grav\Common\Utils','safeTruncate']),
new \Twig_SimpleFilter('safe_truncate_html', ['\Grav\Common\Utils','safeTruncateHTML']),
@@ -537,6 +538,22 @@ class TwigExtension extends \Twig_Extension
return Utils::generateRandomString($count);
}
/**
* Pad a string to a certain length with another string
*
* @param $input
* @param $pad_length
* @param string $pad_string
* @param int $pad_type
*
* @return string
*/
public static function padFilter($input, $pad_length, $pad_string = " ", $pad_type = STR_PAD_RIGHT)
{
return str_pad($input, (int) $pad_length, $pad_string, $pad_type);
}
/**
* Cast a value to array
*

View File

@@ -100,6 +100,9 @@ abstract class Utils
return (object)array_merge((array)$obj1, (array)$obj2);
}
/**
* @return array
*/
public static function dateFormats()
{
$now = new DateTime();
@@ -300,11 +303,19 @@ abstract class Utils
return $root . implode('/', $ret);
}
/**
* @param $function
*
* @return bool
*/
public static function isFunctionDisabled($function)
{
return in_array($function, explode(',', ini_get('disable_functions')));
}
/**
* @return array
*/
public static function timezones()
{
$timezones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL);
@@ -332,6 +343,12 @@ abstract class Utils
}
/**
* @param array $source
* @param $fn
*
* @return array
*/
public static function arrayFilterRecursive(Array $source, $fn)
{
$result = array();
@@ -351,6 +368,11 @@ abstract class Utils
return $result;
}
/**
* @param $string
*
* @return bool
*/
public static function pathPrefixedByLangCode($string)
{
$languages_enabled = self::getGrav()['config']->get('system.languages.supported', []);
@@ -362,6 +384,11 @@ abstract class Utils
return false;
}
/**
* @param $date
*
* @return int
*/
public static function date2timestamp($date)
{
$config = self::getGrav()['config'];