mirror of
https://github.com/getgrav/grav.git
synced 2026-05-07 00:06:37 +02:00
Added new random string static and twig function
This commit is contained in:
@@ -68,7 +68,8 @@ class TwigExtension extends \Twig_Extension
|
||||
new \Twig_SimpleFunction('url', [$this, 'urlFunc']),
|
||||
new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('gist', [$this, 'gistFunc'])
|
||||
new \Twig_SimpleFunction('gist', [$this, 'gistFunc']),
|
||||
new \Twig_simpleFunction('random_string', [$this, 'randomStringFunc']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -433,4 +434,16 @@ class TwigExtension extends \Twig_Extension
|
||||
{
|
||||
return '<script src="https://gist.github.com/'.$id.'.js"></script>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random string
|
||||
*
|
||||
* @param int $count
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function randomStringFunc($count = 5)
|
||||
{
|
||||
return Utils::generateRandomString($count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,4 +219,16 @@ abstract class Utils
|
||||
}
|
||||
return $truncate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random string of a given length
|
||||
*
|
||||
* @param int $length
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function generateRandomString($length = 5)
|
||||
{
|
||||
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user