An extension for Twig that provides simple filters for Gravatar.
Use composer
to install this extension:
composer require ry167/twig-gravatar 3.0.0
require("vendor/autoload.php");
//Define your Twig Environment and Twig Loader
$twig->addExtension(new \TwigGravatar());
//or create a customized Twig Gravatar instance
new \TwigGravatar($default = null, $size = null, $filterPrefix = 'gr', $rating = null, $useHttps = true);
twig.extension.gravatar:
class: \TwigGravatar
arguments:
$default: ~ e.g. 'monsterid'
$size: ~ e.g. 50
$filterPrefix: ~ e.g. 'foo'
$rating: ~ e.g. 'x'
$useHttps: true
tags:
- { name: twig.extension }
You can also remove the arguments section and the default values will be used.
This Extension is designed so that you chain together the filters that you need on top of each other. You must however always start with the grAvatar filter.
Create a Gravatar URL, This returns just the URL for the persons avatar image without the <img>
tag
{{ '[email protected]'|grAvatar }}
Change a Gravatar URL to its secure counterpart.
{{ '[email protected]'|grAvatar|grHttps }}
Change the Size of the Gravatar Image to the specified size in pixels.
{{ '[email protected]'|grAvatar|grSize(1024) }}
Gravatar does not serve images greater than 2048px
, and they are all squares.
Specify a default image for if the User does not have one defined
{{ '[email protected]'|grAvatar|grDefault('http://example.com/default.png') }}
You can also use any of Gravatar's built in default images, you can see them here. Just use the code assigned to them such as mm
instead of your Image URL.
Specify a maximum rating that the image can be.
Valid values are g
, pg
, r
and x
.
{{ '[email protected]'|grAvatar|grRating('pg') }}
You can change the filter prefix from gr
to something else by changing its value in constructor.
//Define your Twig Environment and Twig Loader
$TwigGravatar = new \TwigGravatar(null, null, 'foo');
$twig->addExtension($TwigGravatar);
//Filters are now 'fooAvatar' etc