示例#1
0
        private string ParseTemplateWithModel <T>(string template, T model, string name)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(template))
                {
                    return(string.Empty);
                }
                var razorEngineconfig = new TemplateServiceConfiguration
                {
                    BaseTemplateType = typeof(HtmlTemplateBase <>)
                };

                using (var service = RazorEngineService.Create(razorEngineconfig))
                {
                    if (!CachedFileAccess.IsCompiled(name))
                    {
                        service.Compile(template, name, typeof(T));
                        CachedFileAccess.SetCompiled(name);
                    }
                    return(service.Run(name, typeof(T)));
                }
            }
            catch (RazorEngine.Templating.TemplateCompilationException ex)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var e in ex.CompilerErrors)
                {
                    sb.AppendFormat("{0}\n", e.ToString().Replace(e.FileName, string.Empty));
                }
                throw new JSFileParserException(string.Format("Failure to parse template {0}. See Errors:\n{1}", _filename, sb.ToString()));
            }
        }
示例#2
0
        private string ParseTemplate(string template, string name)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(template))
                {
                    return(string.Empty);
                }

                var razorEngineconfig = new TemplateServiceConfiguration
                {
                    BaseTemplateType = typeof(HtmlTemplateBase)
                };

                using (var service = RazorEngineService.Create(razorEngineconfig))
                {
                    if (!CachedFileAccess.IsCompiled(name))
                    {
                        //protect from random crashes of the RazonEngine service compiler
                        lock (lockerObject)
                        {
                            service.Compile(template, name);
                            CachedFileAccess.SetCompiled(name);
                        }
                    }

                    return(service.Run(name));
                }
            }
            catch (TemplateCompilationException ex)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var e in ex.CompilerErrors)
                {
                    sb.AppendFormat("{0}\n", e.ToString().Replace(e.FileName, string.Empty));
                }
                throw new JSFileParserException(string.Format("Failure to parse template {0}. See Errors:\n{1}", _filename, sb.ToString()));
            }
        }
示例#3
0
 private static string GetJs(string filePath)
 {
     return(CachedFileAccess.ReadAllText(filePath));
 }