generator-chisel
generator-chisel copied to clipboard
Make it more clear how to extend Twig with own functions and filters
Hey, i'm struggling with adding a slugify
filter - I'm getting this error:
Fatal error: Uncaught Twig\Error\RuntimeError: Unable to load the "\Chisel\Extensions\Twig" runtime.
In Chisel/Extensions/Twig
:
protected function registerTwigFilters( $twig ) {
$this->registerFilter(
$twig,
'slugify',
array(
'\Chisel\Extensions\Twig',
'str_to_slug'
)
);
return $twig;
}
public function str_to_slug($slug)
{
// Remove HTML tags
$slug = preg_replace('/<(.*?)>/u', '', $slug);
// Remove inner-word punctuation.
$slug = preg_replace('/[\'"‘’“”]/u', '', $slug);
// Make it lowercase
$slug = mb_strtolower($slug, 'UTF-8');
// Get the "words". Split on anything that is not a unicode letter or number.
// Periods are OK too.
preg_match_all('/[\p{L}\p{N}\.]+/u', $slug, $words);
$words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
$slug = implode('-', $words);
return $slug;
}