Skip to content

Commit

Permalink
Added Nette & Latte integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
lookyman committed Mar 17, 2015
1 parent 3ba7f82 commit 5d48927
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Features
- No external dependencies.
- PSR-4 compatible.
- Compatible with PHP >= 5.3.3 and [HHVM](http://hhvm.com).
- Integrations for [Symfony2](http://symfony.com), [Silex](http://silex.sensiolabs.org), [Laravel](http://laravel.com), [Twig](http://twig.sensiolabs.org) and [Zend Framework 2](http://framework.zend.com/).
- Integrations for [Symfony2](http://symfony.com), [Silex](http://silex.sensiolabs.org), [Laravel](http://laravel.com), [Twig](http://twig.sensiolabs.org), [Zend Framework 2](http://framework.zend.com/), [Nette Framework](http://nette.org/) and [Latte](http://latte.nette.org/).

¹ Some Esperanto transliterations conflict with others. You need to enable the Esperanto ruleset to use these transliterations.

Expand Down Expand Up @@ -283,6 +283,53 @@ return array(
);
```

### Nette Framework

Slugify contains a Nette extension that allows you to use it as a service in your Nette application. You only need to register it in your `config.neon`:

```yml
# app/config/config.neon

extensions:
slugify: Cocur\Slugify\Bridge\Nette\SlugifyExtension
```

You can now use the `Cocur\Slugify\SlugifyInterface` service everywhere in your application, for example in your presenter:

```php
class MyPresenter extends \Nette\Application\UI\Presenter
{

/** @var \Cocur\Slugify\SlugifyInterface @inject */
public $slugify;

public function renderDefault()
{
$this->template->hello = $this->slugify->slugify('Hällo Wörld');
}

}
```

### Latte

If you use the Nette Framework with it's native Latte templating engine, you can use the Latte filter `slugify` in your templates after you have setup Nette extension (see above).

```smarty
{$hello|slugify}
```

If you use Latte outside of the Nette Framework you first need to add the filter to your engine:

```php
use Cocur\Slugify\Bridge\Latte\SlugifyHelper;
use Cocur\Slugify\Slugify;
use Latte;

$latte = new Latte\Engine();
$latte->addFilter('slugify', array(new SlugifyHelper(Slugify::create()), 'slugify'));
```


Changelog
---------
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"zendframework/zend-view": "~2.2",
"zendframework/zend-servicemanager": "~2.2",
"codeclimate/php-test-reporter": "dev-master",
"laravel/framework": "~4.1"
"laravel/framework": "~4.1",
"nette/di": "~2.2",
"latte/latte": "~2.2"
},
"autoload": {
"psr-4": {
Expand Down
36 changes: 36 additions & 0 deletions src/Bridge/Latte/SlugifyHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Cocur\Slugify\Bridge\Latte;

use Cocur\Slugify\SlugifyInterface;

/**
* SlugifyHelper
*
* @package cocur/slugify
* @subpackage bridge
* @author Lukáš Unger <[email protected]>
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyHelper
{

/** @var SlugifyInterface */
private $slugify;

public function __construct(SlugifyInterface $slugify)
{
$this->slugify = $slugify;
}

/**
* @param string
* @param string
* @return string
*/
public function slugify($string, $separator = '-')
{
return $this->slugify->slugify($string, $separator);
}

}
51 changes: 51 additions & 0 deletions src/Bridge/Nette/SlugifyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Cocur\Slugify\Bridge\Nette;

use Nette\DI\CompilerExtension;
use Nette\DI\ServiceDefinition;

/**
* SlugifyExtension
*
* @package cocur/slugify
* @subpackage bridge
* @author Lukáš Unger <[email protected]>
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyExtension extends CompilerExtension
{

public function loadConfiguration()
{
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('slugify'))
->setClass('Cocur\Slugify\SlugifyInterface')
->setFactory('Cocur\Slugify\Slugify');

$builder->addDefinition($this->prefix('helpers'))
->setClass('Cocur\Slugify\Bridge\Latte\SlugifyHelper')
->setInject(false);
}

public function beforeCompile()
{
$builder = $this->getContainerBuilder();

$self = $this;
$registerToLatte = function(ServiceDefinition $def) use ($self) {
$def->addSetup('addFilter', array('slugify', array($self->prefix('@helpers'), 'slugify')));
};

$latteFactory = $builder->getByType('Nette\Bridges\ApplicationLatte\ILatteFactory') ?: 'nette.latteFactory';
if ($builder->hasDefinition($latteFactory)) {
$registerToLatte($builder->getDefinition($latteFactory));
}

if ($builder->hasDefinition('nette.latte')) {
$registerToLatte($builder->getDefinition('nette.latte'));
}
}

}

0 comments on commit 5d48927

Please sign in to comment.