En el servidor
namespace System.Web.Mvc
{
public static class HtmlHelpers
{
private const string pubDir = "/content";
private const string cssDir = "css";
private const string imageDir = "images";
private const string scriptDir = "javascript";
public static string CSS(this HtmlHelper htmlHelper, string fileName)
{
if(! fileName.EndsWith(".css"))
{
fileName += fileName + ".css";
}
return string.Format("<link rel='stylesheet' type='text/css' href='{0}/{1}/{2}'/>\n",pubDir, cssDir ,fileName);
}
public static string JavaScript(this HtmlHelper htmlHelper, string fileName)
{
if (!fileName.EndsWith(".js"))
{
fileName += fileName + ".js";
}
return string.Format("<script type='text/javascript' src='{0}/{1}/{2}' ></script>\n", pubDir, scriptDir, fileName);
}
public static string Image(this HtmlHelper htmlHelper, string fileName)
{
return string.Format("<img src='{0}/{1}/{2}' />\n", pubDir, imageDir, fileName);
}
}
}
En el lado del cliente
<%= Html.CSS("Site.css")%>
<%= Html.JavaScript("jquery-1.4.1.js")%>
<%= Html.JavaScript("colorpicker.js")%>
Una regla de la informática es que no hay que re-inventar la rueda -la otra es si funciona para qué tocar, pero con los test unitarios y demás prácticas ágiles, eso ya está quedando obsoleto-. Por lo tanto he descubierto que ya hay alguien que ha deicado tiempo y esfuerzo a desarrollar interesantes helpers
No hay comentarios:
Publicar un comentario