HandlebarsHelper.HandlebarsCompiler.Precompile C# (CSharp) Method

Precompile() public method

public Precompile ( string template, bool compress ) : string
template string
compress bool
return string
        public string Precompile(string template, bool compress)
        {
            var compiled = Engine.CallGlobalFunction<string>("precompile", template, false);
            if (compress)
            {
                compiled = Compressor.Compress(compiled);
            }
            return compiled;
        }

Usage Example

        public void Process(BundleContext context, BundleResponse response)
        {
            var compiler = new HandlebarsCompiler();
            var templates = new Dictionary<string, string>();
            var server = context.HttpContext.Server;

            foreach (var bundleFile in response.Files)
            {
                var filePath = server.MapPath(bundleFile.VirtualFile.VirtualPath);
                var bundleRelativePath = GetRelativePath(server, bundleFile, filePath);
                var templateName = namer.GenerateName(bundleRelativePath, bundleFile.VirtualFile.Name);
                var template = File.ReadAllText(filePath);
                var compiled = compiler.Precompile(template, false);

                templates[templateName] = compiled;
            }
            StringBuilder javascript = new StringBuilder();
            foreach (var templateName in templates.Keys)
            {
                javascript.AppendFormat("Ember.TEMPLATES['{0}']=", templateName);
                javascript.AppendFormat("Ember.Handlebars.template({0});", templates[templateName]);
            }

            var Compressor = new JavaScriptCompressor();
            var compressed = Compressor.Compress(javascript.ToString());

            response.ContentType = "text/javascript";
            response.Cacheability = HttpCacheability.Public;
            response.Content = compressed;
        }
All Usage Examples Of HandlebarsHelper.HandlebarsCompiler::Precompile