Skip to content

Instantly share code, notes, and snippets.

@robertmilne
Created May 26, 2011 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmilne/993213 to your computer and use it in GitHub Desktop.
Save robertmilne/993213 to your computer and use it in GitHub Desktop.
public static class BindingExtensions
{
public static MvcHtmlString Script(this HtmlHelper helper, string innerHtml, Dictionary<string, object> attributes)
{
if (!System.Diagnostics.Debugger.IsAttached)
innerHtml = new Microsoft.Ajax.Utilities.Minifier().MinifyJavaScript(innerHtml);
var builder = new TagBuilder("script");
builder.MergeAttributes(attributes);
builder.InnerHtml = innerHtml;
return MvcHtmlString.Create(builder.ToString());
}
public static MvcHtmlString Style(this HtmlHelper helper, string innerHtml, Dictionary<string, object> attributes)
{
if (!System.Diagnostics.Debugger.IsAttached)
innerHtml = new Microsoft.Ajax.Utilities.Minifier().MinifyStyleSheet(innerHtml);
var builder = new TagBuilder("style");
builder.MergeAttributes(attributes);
builder.InnerHtml = innerHtml;
return MvcHtmlString.Create(builder.ToString());
}
}
<bindings>
<element name="style">Html.Style('child::*', new System.Collections.Generic.Dictionary&lt;string,object&gt;{{'@*'}})</element>
<element name="script">Html.Script('child::*', new System.Collections.Generic.Dictionary&lt;string,object&gt;{{'@*'}})</element>
</bindings>
<style type="text/css">#dashboard{margin:1em;padding:1em;border:1px solid #222}</style>
<script type="text/javascript">$(function(){$("#dashboard").load("/Test/Dashboard")})</script>
<content:head>
<style type="text/css" cache.expires="1200" cache="System.Diagnostics.Debugger.IsAttached">
#dashboard
{
margin: 1em;
padding: 1em;
border: 1px solid #222;
}
</style>
</content:head>
<content:scripts>
<script if="bool.Parse('false')" src="/Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" cache.expires="1200" cache="System.Diagnostics.Debugger.IsAttached">
$(function () {
// We don't need no stinkin' comments
$("#dashboard").load("!{Url.Action(MVC.Test.Dashboard())}");
});
</script>
</content:scripts>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment