Created
August 21, 2013 12:35
-
-
Save kalmanolah/6293906 to your computer and use it in GitHub Desktop.
Symfony2 Twig extension for generating gravatar links for stuff. Accepts objects with a `getEmail()` function, arrays with an `email` key, or just a string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This goes in your services.yml: | |
* | |
* services: | |
* example.twig.gravatar_extension: | |
* class: My\ExampleBundle\Twig\GravatarExtension | |
* tags: | |
* - { name: twig.extension } | |
*/ | |
namespace My\ExampleBundle\Twig; | |
class GravatarExtension extends \Twig_Extension | |
{ | |
private $gravatar_base = '//gravatar.com/avatar/'; | |
public function getFilters() | |
{ | |
return array( | |
new \Twig_SimpleFilter('gravatar', array($this, 'gravatarFilter')), | |
); | |
} | |
public function gravatarFilter($input, $size = 40, $default = 'identicon') | |
{ | |
if (is_object($input)) { | |
$input = $input->getEmail(); | |
} | |
if (is_array($input)) { | |
$input = $input['email']; | |
} | |
$final = $this->gravatar_base . md5( strtolower( $input ) ) . '.png?s=' . $size . '&d=' . $default; | |
return $final; | |
} | |
public function getName() | |
{ | |
return 'gravatar_extension'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment