using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace WePlatform.Helpers { public static class HtmlHelpers { private class ScriptBlock : IDisposable { private const string ScriptsKey = "PartialViewScripts"; public static List PartialViewScripts { get { if (HttpContext.Current.Items[ScriptsKey] == null) HttpContext.Current.Items[ScriptsKey] = new List(); return (List)HttpContext.Current.Items[ScriptsKey]; } } readonly WebViewPage _webPageBase; public ScriptBlock(WebViewPage webPageBase) { _webPageBase = webPageBase; _webPageBase.OutputStack.Push(new StringWriter()); } public void Dispose() { PartialViewScripts.Add(((StringWriter)this._webPageBase.OutputStack.Pop()).ToString()); } } public static IDisposable BeginScripts(this HtmlHelper helper) { return new ScriptBlock((WebViewPage)helper.ViewDataContainer); } public static MvcHtmlString PartialViewScripts(this HtmlHelper helper) { return MvcHtmlString.Create(string.Join(Environment.NewLine, ScriptBlock.PartialViewScripts.Select(s => s.ToString()))); } } }