How can I precompile HandlebarsJS templates from Visual Studio?

后端 未结 3 1749
悲哀的现实
悲哀的现实 2021-02-03 12:57

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

3条回答
  •  不知归路
    2021-02-03 13:52

    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.

提交回复
热议问题