Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active August 29, 2015 14:17
Show Gist options
  • Save mattbrailsford/8ab62c2c71479a6b6a5f to your computer and use it in GitHub Desktop.
Save mattbrailsford/8ab62c2c71479a6b6a5f to your computer and use it in GitHub Desktop.
Helper methods for registering JS / CSS dependencies, including inline blocks.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using System.Web.WebPages;
namespace UmbracoCms.Web
{
public static class HtmlHelperExtensions
{
private const string _jsIncludesViewDataName = "RenderJavaScriptIncludes";
private const string _jsViewDataName = "RenderJavaScript";
private const string _cssIncludeViewDataName = "RenderStyleIncludes";
private const string _cssViewDataName = "RenderStyle";
public static HtmlHelper IncludeJs(this HtmlHelper htmlHelper, string key, string url)
{
var scriptHash = htmlHelper.ViewContext.HttpContext
.Items[_jsIncludesViewDataName] as Dictionary<string, string>;
if (scriptHash != null)
{
if (!scriptHash.ContainsKey(key))
{
scriptHash.Add(key, url);
}
}
else
{
scriptHash = new Dictionary<string, string> { { key, url } };
htmlHelper.ViewContext.HttpContext.Items.Add(_jsIncludesViewDataName, scriptHash);
}
return htmlHelper;
}
public static HtmlHelper IncludeJs(this HtmlHelper htmlHelper,
Func<object, HelperResult> template, bool includeScriptTags = true)
{
// No key, so just give it a random one
return htmlHelper.IncludeJs(Guid.NewGuid().ToString(), template, includeScriptTags);
}
public static HtmlHelper IncludeJs(this HtmlHelper htmlHelper, string key,
Func<object, HelperResult> template, bool includeScriptTags = true)
{
var scriptHash = htmlHelper.ViewContext.HttpContext
.Items[_jsViewDataName] as Dictionary<string, string>;
var val = includeScriptTags
? string.Format("<script type=\"text/javascript\">{0}</script>", template(null))
: template(null).ToString();
if (scriptHash != null)
{
if (!scriptHash.ContainsKey(key))
{
scriptHash.Add(key, val);
}
}
else
{
scriptHash = new Dictionary<string, string> { { key, val } };
htmlHelper.ViewContext.HttpContext.Items.Add(_jsViewDataName, scriptHash);
}
return htmlHelper;
}
public static MvcHtmlString RenderJsIncludes(this HtmlHelper htmlHelper)
{
var result = new StringBuilder();
// Render includes
var includesHash = htmlHelper.ViewContext.HttpContext
.Items[_jsIncludesViewDataName] as Dictionary<string, string>;
if (includesHash != null)
{
foreach (var script in includesHash.Values)
{
result.AppendLine(string.Format(
"<script type=\"text/javascript\" src=\"{0}\"></script>",
script));
}
}
// Render inline scripts
var inlineHash = htmlHelper.ViewContext.HttpContext
.Items[_jsViewDataName] as Dictionary<string, string>;
if (inlineHash != null)
{
foreach (var script in inlineHash.Values)
{
result.AppendLine(script);
}
}
return MvcHtmlString.Create(result.ToString());
}
public static HtmlHelper IncludeCss(this HtmlHelper htmlHelper, string key, string url)
{
var scriptHash = htmlHelper.ViewContext.HttpContext
.Items[_cssIncludeViewDataName] as Dictionary<string, string>;
if (scriptHash != null)
{
if (!scriptHash.ContainsKey(key))
{
scriptHash.Add(key, url);
}
}
else
{
scriptHash = new Dictionary<string, string> { { key, url } };
htmlHelper.ViewContext.HttpContext.Items.Add(_cssIncludeViewDataName, scriptHash);
}
return htmlHelper;
}
public static HtmlHelper IncludeCss(this HtmlHelper htmlHelper,
Func<object, HelperResult> template, bool includeStyleTags = true)
{
// No key, so just give it a random one
return htmlHelper.IncludeCss(Guid.NewGuid().ToString(), template, includeStyleTags);
}
public static HtmlHelper IncludeCss(this HtmlHelper htmlHelper, string key,
Func<object, HelperResult> template, bool includeStyleTags = true)
{
var scriptHash = htmlHelper.ViewContext.HttpContext
.Items[_cssViewDataName] as Dictionary<string, string>;
var val = includeStyleTags
? string.Format("<style type=\"text/css\">{0}</style>", template(null))
: template(null).ToString();
if (scriptHash != null)
{
if (!scriptHash.ContainsKey(key))
{
scriptHash.Add(key, val);
}
}
else
{
scriptHash = new Dictionary<string, string> { { key, val } };
htmlHelper.ViewContext.HttpContext.Items.Add(_cssViewDataName, scriptHash);
}
return htmlHelper;
}
public static MvcHtmlString RenderCssIncludes(this HtmlHelper htmlHelper)
{
var result = new StringBuilder();
// Render includes
var includesHash = htmlHelper.ViewContext.HttpContext
.Items[_cssIncludeViewDataName] as Dictionary<string, string>;
if (includesHash != null)
{
foreach (var css in includesHash.Values)
{
result.AppendLine(string.Format(
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/{0}\">",
css));
}
}
// Render inline css
var inlineHash = htmlHelper.ViewContext.HttpContext
.Items[_cssViewDataName] as Dictionary<string, string>;
if (inlineHash != null)
{
foreach (var css in inlineHash.Values)
{
result.AppendLine(css);
}
}
return MvcHtmlString.Create(result.ToString());
}
}
}
@using UmbracoCms.Web
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>@Model.Name</title>
@Html.RenderCssIncludes()
</head>
<body>
@Html.Partial("TestPartial")
<!-- Javascripts -->
@Html.RenderJsIncludes()
</body>
</html>
@using UmbracoCms.Web
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
Html.IncludeJs("jquery", "/scripts/jquery.min.js")
.IncludeJs(
@<script type="text/javascript">
// Initi javascript here...
</script>, false);
}
<!-- MARKUP HERE -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment