generator-chisel icon indicating copy to clipboard operation
generator-chisel copied to clipboard

Make it more clear how to extend Twig with own functions and filters

Open luboskmetko opened this issue 6 years ago • 1 comments

luboskmetko avatar Apr 17 '18 11:04 luboskmetko

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;
	}

mortensassi avatar Apr 20 '20 14:04 mortensassi