Skip to content

Instantly share code, notes, and snippets.

@SergeiGolos
Created July 30, 2013 14:44
Show Gist options
  • Save SergeiGolos/6113551 to your computer and use it in GitHub Desktop.
Save SergeiGolos/6113551 to your computer and use it in GitHub Desktop.
AngularTemplateTransform
/// <summary>
/// Handles assembling html documents single set of ng-template items.
/// </summary>
public class AngularTempalteTransform : IBundleTransform
{
/// <summary>
/// Process the bundle.
/// </summary>
/// <param name="context">The current bundle context.</param>
/// <param name="response">The response object to write to.</param>
public void Process(BundleContext context, BundleResponse response)
{
StringBuilder strBundleResponse = new StringBuilder();
foreach (BundleFile file in response.Files)
{
string matchPath = context.BundleVirtualPath.Split('/').Last();
string filename = file.VirtualFile.VirtualPath.Remove(0, file.VirtualFile.VirtualPath.IndexOf(matchPath)).Replace("\\", "/");
using (var fileStream = file.VirtualFile.Open())
{
using (var textStream = new StreamReader(fileStream))
{
strBundleResponse.AppendFormat("<script id='{0}' type='text/ng-template'>{1}</script>", filename, textStream.ReadToEnd());
}
}
}
response.Content = strBundleResponse.ToString();
}
}
/// <summary>
/// Returns the content of the requested Bundle with transforms applied.
/// </summary>
/// <param name="path">The virtual url of the bundle.</param>
/// <returns>Bundle context with applied transforms.</returns>
private string GetBundleContent(string path)
{
var bundle = BundleTable.Bundles.GetBundleFor(path);
var context = new BundleContext(HttpContext, BundleTable.Bundles, path);
var result = bundle.ApplyTransforms(context, path, bundle.EnumerateFiles(context));
return result.Content;
}
/// To get the templates to your view.
/// ViewBag.Templates = this.GetBundleContent("~/Content/views");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment