Ryan LanciauxNew Media Mercenary

MVC HtmlHelper for Gravatar

November 13, 2008 by ryan

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

 

Anyways, I've posted the class file below. Hopefully you find it useful -- I would love to hear what your thoughts are or of any changes you would like to see.
GravatarHelper.cs (2.72 kb)

kick it on DotNetKicks.com



Related posts

Comments

November 14. 2008 01:19

Ben Scheirman

Add to MvcContrib?

Ben Scheirman

November 14. 2008 05:26

Simone

Good stuff Smile

Simone

November 14. 2008 09:24

pingback

Pingback from alvinashcraft.com

Dew Drop - November 14, 2008 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

November 14. 2008 11:44

Nikon

thx, very usefull class

Nikon

November 14. 2008 15:32

Mohammad Mahdi Ramezanpour

Nice Helper. ;)

Mohammad Mahdi Ramezanpour

November 15. 2008 22:12

brady gaster

That's pretty cool. Excellent idea!

brady gaster

December 4. 2008 12:39

Damien Guard

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

ryan

Thanks Damien -- you are definitely right! Why didn't I think to do that? Smile

ryan

December 6. 2008 03:54

Franci Penov

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

Joel

OMG, it's FRANKLIN!

Joel

December 15. 2008 08:23

packiaraj

Very Useful to me

packiaraj

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 5. 2009 19:42





© 2008 Ryan Lanciaux :: powered by BlogEngine.NET