-
-
Save boboldehampsink/7354431 to your computer and use it in GitHub Desktop.
<?php | |
namespace Craft; | |
class SlugifyPlugin extends BasePlugin | |
{ | |
public function getName() | |
{ | |
return Craft::t('Slugify Twig Extension'); | |
} | |
public function getVersion() | |
{ | |
return '1.0'; | |
} | |
public function getDeveloper() | |
{ | |
return 'Bob Olde Hampsink'; | |
} | |
public function getDeveloperUrl() | |
{ | |
return 'http://www.itmundi.nl'; | |
} | |
public function hookAddTwigExtension() | |
{ | |
Craft::import('plugins.slugify.twigextensions.SlugifyTwigExtension'); | |
return new SlugifyTwigExtension(); | |
} | |
} |
<?php | |
namespace Craft; | |
class SlugifyTwigExtension extends \Twig_Extension | |
{ | |
protected $env; | |
public function getName() | |
{ | |
return 'Slugify Twig Extension'; | |
} | |
public function getFilters() | |
{ | |
return array('slugify' => new \Twig_Filter_Method($this, 'slugify')); | |
} | |
public function getFunctions() | |
{ | |
return array('slugify' => new \Twig_Function_Method($this, 'slugify')); | |
} | |
public function initRuntime(\Twig_Environment $env) | |
{ | |
$this->env = $env; | |
} | |
public function slugify($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; | |
} | |
} |
It's not very obvious from the way this plugin was released (as a Gist rather than a normal Git repo), but the plugin is expecting you to place the SlugifyTwigExtension.php file within a “twigextensions” subfolder:
plugins/
slugify/
SlugifyPlugin.php
twigextensions/
SlugifyTwigExtension.php
Thanks Brandon.
Bob, would you consider re-releasing this as a normal Git repo? It's a great plugin, but it's silly to force everyone who wants to use it to compile the folder structure on their own.
Bob I'm with Lindsey on this one - I'd be happy to pull it into a repo if you don't want to maintain it?
Hey Bob, it would be super sweet if this were a normal plugin repo... Then I could submit this pull request:
public function slugify($slug)
{
return ElementHelper::createSlug($slug);
}
It ties the plugin to the native core logic. The slugifying logic recently changed slightly, and could evolve again in the future.
someone already created the repo - and I have a pullrequest pending with Lindseys suggested change
Here's the repo maintained by me: https://github.com/boboldehampsink/slugify
Yay thanks @boboldehampsink!
to bad cleanSlug is private =( woulda been nice to be able to: craft().entries.cleanSlug("blahidy blah");