The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2009
Sign in
Based on a great suggestion from Ben Scheirman and Simone Chiaretta I have converted the Gravatar URL helper class into a HtmlHelper extension method. This was my first stab at making a HtmlHelper extension but it seems to be working nicely and helps me keep my code a lot cleaner. Instead saving the URL (which I would highly advise against) or doing some weird method calling acrobatics in the Controller, I can simply say <%= Html.Gravatar("email@email.com") %> inside the view.
I've added a couple different method definitions for various parameters. You can pass in just an e-mail address, an e-mail and gravatar parameters (see here for more info on gravatar parameters) or an e-mail address, gravatar parameters and html attributes. A quick usage summary is listed below (I'm using my brother Joel's gravatar in the examples)
<%= Html.Gravatar("email@email.com") %>
Base Gravatar Helper
<%= Html.Gravatar("email@email.com", new { s = "128", r = "pg" })%>
Gravatar helper with optional Gravatar Parameters
<%= Html.Gravatar("email@email.com", null, new { style = "border:5px solid" })%>
Helper with HTML attributes. Please note, this is still expecting the Gravatar parameters -- you can pass in null if you want to just grab the default image with no additional properties.
<%= Html.Gravatar("email@email.com", new { s = "128", r = "pg" }, new { style="border:5px solid;"})%>
Helper with both Gravatar parameters and HTML attributes
November 14. 2008 01:19
Add to MvcContrib?
Ben Scheirman
November 14. 2008 05:26
Good stuff
Simone
November 14. 2008 09:24
Pingback from alvinashcraft.com Dew Drop - November 14, 2008 | Alvin Ashcraft's Morning Dew
alvinashcraft.com
November 14. 2008 11:44
thx, very usefull class
Nikon
November 14. 2008 15:32
Nice Helper. ;)
Mohammad Mahdi Ramezanpour
November 15. 2008 22:12
That's pretty cool. Excellent idea!
brady gaster
December 4. 2008 12:39
You need to encode your attributes if you're going to build the HTML. Better yet would be to delegate the output building to the HTML classes that already do this, e.g. Html.Image [)amien
Damien Guard
December 4. 2008 12:58
Thanks Damien -- you are definitely right! Why didn't I think to do that?
ryan
December 6. 2008 03:54
Nice job, Ryan. However, you have couple of small bugs: - GetGravatar(string email, object gravatarAttributes) - you should be using continue instead of break when processing the first gravatar attribute. - GetImageTag(string source, object htmlAttributes) - you need a space between the html attributes
Franci Penov
December 12. 2008 14:39
OMG, it's FRANKLIN!
Joel
December 15. 2008 08:23
Very Useful to me
packiaraj
January 5. 2009 19:42