Management of the templates for your JavaScript app could be pain. Of course, there are few solutions out there. This library is another solution :). Okay, sort of. Really this is just better implementation of Anton Samarskyy's idea.
So main idea of this library is about to keep templates as HTML
files near of your JS
files, but serve them as single file to reduce requests. What is more interesting is that we can go further and precompile templates so we can serve templates as single js
file with our app scripts. Stay turned.
Now about implementation. ASP.NET MVC 4
introduced bundling support. In simple words this is actually means combining (and minifying) many resource files (JavaScript, CSS) to the single file to reduce number of requests. This is what we actually use.
- Bundle HTML templates to the single file
- Precompile Underscore templates (ver 1.5.1)
Assuming that you have folder structure like this
bundle configuration will be
// Create app bundle:
// main.html and example2.html are underscore templates
// app.js is our simple application
var appJs = new Bundle("~/scripts/app.js");
appJs.AddFile("~/scripts/app/main.html");
appJs.AddFile("~/scripts/app/example2.html");
appJs.AddFile("~/scripts/app/app.js");
appJs.Transform = new NoTransform("text/javascript; charset=utf-8");
//appJs.Transform = new JsMinify(); // You can minify bundle if you want
appJs.Builder = new CompiledUndrescoreTemplatesBundler();
BundleTable.Bundles.Add(appJs);
Complete example is SampleWebApp project in sources.
PM> Install-Package UnderscoreBundler
Licensed under the MIT