ry167/twig-gravatar

Gravatar filters for Twig templates

4.0.0-RC 2020-01-26 21:34 UTC

This package is auto-updated.

Last update: 2024-03-27 06:44:06 UTC


README

Latest Version Software License Build Status Quality Score Total Downloads

An extension for Twig that provides simple filters for Gravatar.

Installation

Use composer to install this extension:

composer require ry167/twig-gravatar 3.0.0

Usage

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);

Usage in Symfony

    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.

Filters

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.

grAvatar

Create a Gravatar URL, This returns just the URL for the persons avatar image without the <img> tag

{{ 'example@example.com'|grAvatar }}

grHttps

Change a Gravatar URL to its secure counterpart.

{{ 'example@example.com'|grAvatar|grHttps }}

grSize

Change the Size of the Gravatar Image to the specified size in pixels.

{{ 'example@example.com'|grAvatar|grSize(1024) }}

Gravatar does not serve images greater than 2048px, and they are all squares.

grDefault

Specify a default image for if the User does not have one defined

{{ 'example@example.com'|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.

grRating

Specify a maximum rating that the image can be. Valid values are g, pg, r and x.

{{ 'example@example.com'|grAvatar|grRating('pg') }}

Prefix

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