Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slugify Twig Extension - Use like this: {{ user.fullName | slugify }}. Uses Craft's core slug technology.
<?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;
}
}
@rkingon

This comment has been minimized.

Copy link

rkingon commented Dec 8, 2013

to bad cleanSlug is private =( woulda been nice to be able to: craft().entries.cleanSlug("blahidy blah");

@brandonkelly

This comment has been minimized.

Copy link

brandonkelly commented Mar 17, 2014

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
@lindseydiloreto

This comment has been minimized.

Copy link

lindseydiloreto commented Aug 4, 2014

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.

@joshangell

This comment has been minimized.

Copy link

joshangell commented Aug 8, 2014

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?

@lindseydiloreto

This comment has been minimized.

Copy link

lindseydiloreto commented Sep 9, 2014

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.

@maxxst

This comment has been minimized.

Copy link

maxxst commented Sep 19, 2014

someone already created the repo - and I have a pullrequest pending with Lindseys suggested change

https://github.com/nodepub/CraftSlugifyPlugin

@boboldehampsink

This comment has been minimized.

Copy link
Owner Author

boboldehampsink commented Oct 2, 2014

Here's the repo maintained by me: https://github.com/boboldehampsink/slugify

@joshangell

This comment has been minimized.

Copy link

joshangell commented Oct 15, 2014

Yay thanks @boboldehampsink!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.