Ingeniería

Gravatars in Mephisto (article's title included)

How to display gravatars not only for comments but also in article titles in Mephisto, using a simple initializer and Liquid templates.

1 min de lectura
José Costa
José Costa Publicado el 23 ene 2009

Goal: I just want to see the gravatars not only for comments but in the article’s title also.

I have no prior knowledge of Mephisto (this is my second day with it) nor Liquid. So, here is how i did it … may be far from perfect but it seems to pull it off:

Comments, easy:

There is already a method gravatar that receives a hash with the comment properties and does the trick. So, placing the following line in the right spot of the appropriate liquid file (for me that’s the _article.liquid file) should be enough:

{{ comment | gravatar:50,'avatar'' }}

Articles are a little more tricky

Since there is no support in for gravatars for article’s author i’ve coded a simple initializer to add a new method for this purpose and keep the original code as clean as possible. This initializer adds the method gravatar_for_article to the UrlFilters module.

module UrlFilters
  def gravatar_for_article(article, size=80, default=nil)
    article = Article.find article["id"]
    gravatar({'author_email' => article.user.email,
                    'author' => article.user.login},size,default)
  end
end

The code above creates a new hash from some of the article’s properties to mock the comment structure and passes it to the original gravatar method. Now, place the following line inside the article’s header and voila.

{{ article | gravatar_for_article :60,'avatar' }}

Style it and you’re done.

Artículos que te pueden interesar

Ver todos

Construyamos juntos.

¿Listo para trabajar con un equipo que se preocupa tanto por tu éxito como tú?

+1