Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
using System; using System.Linq; using System.Text; using Microsoft.Web.Samples; using System.IO; using System.Web; using System.Web.UI; using System.Collections; namespace MonoRail.Samples.SpriteHelper { public class Sprite { private static Control helperControl = CreateHelperControl(); public static string ImportStylesheet(string virtualPath) { ImageOptimizations.EnsureInitialized(); if (Path.HasExtension(virtualPath)) { virtualPath = Path.GetDirectoryName(virtualPath); } HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); string cssFileName = ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) ?? ImageOptimizations.LowCompatibilityCssFileName; virtualPath = Path.Combine(virtualPath, cssFileName); string physicalPath = HttpContext.Current.Server.MapPath(virtualPath); if (File.Exists(physicalPath)) { StringWriter sw = new StringWriter(); using (HtmlTextWriter html = new HtmlTextWriter(sw)) { html.AddAttribute(HtmlTextWriterAttribute.Href, ResolveUrl(virtualPath)); html.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet"); html.AddAttribute(HtmlTextWriterAttribute.Type, "text/css"); html.AddAttribute("media", "all"); html.RenderBeginTag(HtmlTextWriterTag.Link); } return sw.ToString(); } return String.Empty; } public static string MakeCssClassName(string pathToImage) { return ImageOptimizations.MakeCssClassName(pathToImage); } public static string Image(string virtualPath) { return Image(virtualPath, null); } public static string Image(string virtualPath, IDictionary htmlAttributes) { ImageOptimizations.EnsureInitialized(); HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); StringWriter sw = new StringWriter(); using (HtmlTextWriter html = new HtmlTextWriter(sw)) { if (htmlAttributes != null) { foreach (DictionaryEntry entry in htmlAttributes) { html.AddAttribute((entry.Key ?? String.Empty).ToString(), (entry.Value ?? String.Empty).ToString()); } } if (ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) == null) { html.AddAttribute(HtmlTextWriterAttribute.Src, ResolveUrl(virtualPath)); } else { html.AddAttribute(HtmlTextWriterAttribute.Class, ImageOptimizations.MakeCssClassName(virtualPath)); html.AddAttribute(HtmlTextWriterAttribute.Src, ResolveUrl(ImageOptimizations.GetBlankImageSource(httpContext.Request.Browser))); } html.RenderBeginTag(HtmlTextWriterTag.Img); } return sw.ToString(); } private static Control CreateHelperControl() { var control = new Control(); control.AppRelativeTemplateSourceDirectory = "~/"; return control; } private static string ResolveUrl(string path) { return helperControl.ResolveClientUrl(path); } } }
$Sprite.ImportStylesheet("~/App_Sprites/categories/") <li class="$Sprite.MakeCssClassName("~/App_Sprites/categories/dotNet.png")"><a href="#" class="categories">Programming</a></li> <a href="#">$Sprite.Image("~/App_Sprites/popular/visualStudio.png", "%{alt='visualStudio'}") </a>
Remember Me
a@href@title, strike