Skip to content

Instantly share code, notes, and snippets.

@aertmann
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aertmann/ed01e20ed79c6ded9b83 to your computer and use it in GitHub Desktop.
Save aertmann/ed01e20ed79c6ded9b83 to your computer and use it in GitHub Desktop.
Slugify helpers

View helper usage:

{a=Aertmann\Acme\ViewHelpers}
<div id="{node.properties.title -> a:slugify()}"></div>

Eel helper usage:

slug = ${String.slugify(q(node).property('title'))}
TYPO3:
TypoScript:
defaultContext:
String: 'Aertmann\Acme\Eel\Helper\StringHelper'
<?php
namespace Aertmann\Acme\ViewHelpers;
/**
* Slugify view helper to generate dom IDs/url slugs
*/
class SlugifyViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
/**
* Slugifies a value
*
* @param string $value The value to be processed
* @param array $replace The replace array (keys will be replaced with values)
* @return string $value
*/
public function render($value = '', array $replace = array('æ' => 'ae', 'Æ' => 'ae', 'ø' => 'oe', 'Ø' => 'oe', 'å' => 'aa', 'Å' => 'aa')) {
$value = $value === '' ? $this->renderChildren() : $value;
if (empty($replace) === FALSE) {
$value = strtr($value, $replace);
}
// Replace non letter or digits by -
$value = preg_replace('~[^\\pL\d]+~u', '-', $value);
// Trim incl. dashes
$value = trim($value, '-');
// Transliterate
if (function_exists('iconv') === TRUE) {
$value = iconv('utf-8', 'us-ascii//TRANSLIT', $value);
}
// Lowercase
$value = strtolower($value);
// Remove unwanted characters
$value = preg_replace('~[^-\w]+~', '', $value);
return $value;
}
}
<?php
namespace Aertmann\Acme\Eel\Helper;
use TYPO3\Flow\Annotations as Flow;
/**
* Extends the default string helper for Eel
*
* @Flow\Proxy(false)
*/
class StringHelper extends \TYPO3\Eel\Helper\StringHelper {
/**
* Slugifies a value
*
* @param string $value The value to be processed
* @param array $replace The replace array (keys will be replaced with values)
* @return string $value
*/
public function slugify($value = '', array $replace = array('æ' => 'ae', 'Æ' => 'ae', 'ø' => 'oe', 'Ø' => 'oe', 'å' => 'aa', 'Å' => 'aa')) {
if (empty($replace) === FALSE) {
$value = strtr($value, $replace);
}
// Replace non letter or digits by -
$value = preg_replace('~[^\\pL\d]+~u', '-', $value);
// Trim incl. dashes
$value = trim($value, '-');
// Transliterate
if (function_exists('iconv') === TRUE) {
$value = iconv('utf-8', 'us-ascii//TRANSLIT', $value);
}
// Lowercase
$value = strtolower($value);
// Remove unwanted characters
$value = preg_replace('~[^-\w]+~', '', $value);
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment