Tagged: gravatar

Avatars in Grails with Gravatar

On every site we often need ability to show user avatars. The simplest way is to use external serive Gravatar.com.

There exists even a plugin for Grails but you don’t need it.
You can use gravatars in gsp files like this:

<img src="https://www.gravatar.com/avatar/${user.email.encodeAsMD5()}"/>

That’s all. But if you want you can create a tag in taglib:

class TagLib {
    static encodeAsForTags = [ gravatar: [taglib: 'none'] ]

    /**
     * Render img tag with user avatar from Gravatar.com
     * @emptyTag
     *
     * @attr user User
     */
    def gravatar = { attrs ->
        User user = attrs.user
        assert user
        out << "<img src='https://www.gravatar.com/avatar/${user.email.encodeAsMD5()}'/>"
    }
}