Skip to content

Commit 048e97c

Browse files
committed
Merge branch 'fluoresceco-twig-extension'
2 parents 3656431 + c066bbc commit 048e97c

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

README.md

+26-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
hashidsBundle
44
=============
55

6-
##This is a bundle to use http://www.hashids.org/ as a service
6+
## This is a bundle to use http://www.hashids.org/ as a service
77

88
## Installation
99

10-
1110
#### Symfony 2.1.x <= 2.4.x: Composer
1211

1312
[Composer](http://packagist.org/about-composer) is a project dependency manager for PHP. You have to list
1413
your dependencies in a `composer.json` file:
1514

16-
``` json
15+
```json
1716
{
1817
"require": {
1918
"cayetanosoriano/hashids-bundle": "dev-master"
@@ -22,7 +21,7 @@ your dependencies in a `composer.json` file:
2221
```
2322
To actually install in your project, download the composer binary and run it:
2423

25-
``` bash
24+
```bash
2625
wget http://getcomposer.org/composer.phar
2726
# or
2827
curl -O http://getcomposer.org/composer.phar
@@ -34,7 +33,7 @@ php composer.phar install
3433

3534
Finally, enable the bundle in the kernel:
3635

37-
``` php
36+
```php
3837
<?php
3938
// app/AppKernel.php
4039

@@ -49,16 +48,17 @@ public function registerBundles()
4948
```
5049

5150
## Configuration
52-
###Add the following to your config.yml
53-
```
51+
52+
### Add the following to your config.yml
53+
```yaml
5454
cayetanosoriano_hashids:
5555
salt: "randomsalt" #optional
5656
min_hash_length: 10 #optional
5757
alphabet: "abcd..." #optional
5858
```
5959
6060
### Then use the service
61-
```
61+
```php
6262
$kcy = $this->get('hashids');
6363
```
6464

@@ -112,6 +112,24 @@ public function viewAction(User $user)
112112
```
113113

114114
### license
115+
=======
116+
### Twig extension
117+
118+
#### Declare the service to your services.yml
119+
```yaml
120+
twig.hashids_extension:
121+
class: cayetanosoriano\HashidsBundle\Twig\HashidsExtension
122+
arguments: ["@hashids"]
123+
public: false
124+
tags: [{ name: twig.extension }]
125+
```
126+
127+
#### Use the extension in your Twig templates
128+
```twig
129+
<a href="{{ path('user_profile', {'hashid': user.id|hashid_encode}) }}">View Profile</a>
130+
```
131+
132+
### License
115133
```
116134
Copyright (c) 2015 neoshadybeat[at]gmail.com
117135

Twig/HashidsExtension.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace cayetanosoriano\HashidsBundle\Twig;
4+
5+
use Hashids\Hashids;
6+
7+
/**
8+
* Twig extension to allow encoding and decoding Hashids
9+
*
10+
* @author Jaik Dean <jaik@fluoresce.co>
11+
*/
12+
class HashidsExtension extends \Twig_Extension
13+
{
14+
/**
15+
* @var Hashids\Hashids;
16+
*/
17+
private $hashids;
18+
19+
public function __construct(Hashids $hashids)
20+
{
21+
$this->hashids = $hashids;
22+
}
23+
24+
public function getFilters()
25+
{
26+
return [
27+
new \Twig_SimpleFilter('hashid_encode', [$this, 'encode']),
28+
new \Twig_SimpleFilter('hashid_decode', [$this, 'decode']),
29+
];
30+
}
31+
32+
public function encode($number)
33+
{
34+
return $this->hashids->encode($number);
35+
}
36+
37+
public function decode($hash)
38+
{
39+
return $this->hashids->decode($hash);
40+
}
41+
42+
public function getName()
43+
{
44+
return 'hashids_extension';
45+
}
46+
}

0 commit comments

Comments
 (0)