Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I precompile HandlebarsJS templates from Visual Studio?

Is it possible to precompile Handlebars Templates from a postbuild event of Visual Studio or in the App_Start of a MVC web app? Thanks so much in advance. Dale

like image 809
dalcam Avatar asked Apr 27 '13 23:04

dalcam


2 Answers

Sure, you have many options:

  1. You can install node.js for windows and npm, and configure a post-build event to the compilation (example here from previous question)
  2. If you're using ember.js, here's an implementation that uses bundle transformation to achieve precompilation.
  3. Another for ember.js that supports components, here's the implementation, that also uses bundle transformation.
  4. Here is an example of compilation on C# using the Jurassic javascript compiler
like image 89
amhed Avatar answered Sep 28 '22 10:09

amhed


One way to do this is using bundle transform and the jurrasic js compiler to generate a js file with all your compiled views and partial views in it.

public class BundleConfig
{
    public static void RegisterHandlBarBundles(BundleCollection bundles, string path)
    {
        HandleBarBundleTransform transform = new HandleBarBundleTransform();
        transform.jsPath = path;
        bundles.Add(new Bundle("~/views.js", transform).IncludeDirectory("~/views", "*.hbs", true));
        BundleTable.EnableOptimizations = true;
    }
}

This has the benefit of not requiring node.js or ember while still using a simple bundletransform hook.

The full source for the HandleBarBundleTransform is here.

This has the convenience of the ember solution(s) @ahmed posted for those not using ember.

like image 20
Adam Gordon Bell Avatar answered Sep 28 '22 09:09

Adam Gordon Bell