/// <summary> /// Render template using Razor engine /// </summary> /// <param name="template"></param> /// <param name="model"></param> /// <param name="viewBag"> </param> /// <param name="cacheName"></param> /// <param name="resolveType"></param> /// <returns></returns> public static string CompileAndRun(string template, dynamic model, DynamicViewBag viewBag = null, string cacheName = "", ResolveType resolveType = ResolveType.Global) { Type type = model.GetType(); // Add template to cache if not existed var key = TryCompileAndAddTemplate(template, cacheName, type, resolveType); // Parse the template return(RazorEngineServiceExtensions.Run(Engine.Razor, key, type, model, viewBag)); }
public static string Generate <T>(string temp, T model) { //var config = new TemplateServiceConfiguration(); //config.ReferenceResolver = new ReferenceResolver(); //var service = RazorEngineService.Create(config); return(RazorEngineServiceExtensions.RunCompile( Engine.Razor, temp, App.IdWorker.NextStringId(), typeof(T), model, null ) .Replace(""", "\"") .Replace("<c>", "") .Replace("</c>", "")); }
protected virtual void RenderModule(HtmlTextWriter writer, object jsonData) { string str = "TemplateCacheKey-" + this.ID; string key = "TemplateFileCacheKey-" + this.ID; string str3 = ""; string str4 = HttpContext.Current.Cache[key] as string; if (string.IsNullOrEmpty(str4) || (str4.Length == 0)) { string path = HttpContext.Current.Request.MapPath(this.TemplateFile); str4 = File.ReadAllText(path); str3 = RazorEngineServiceExtensions.RunCompile(Engine.Razor, new LoadedTemplateSource(str4, null), str, null, jsonData, null); HttpContext.Current.Cache.Insert(key, str4, new CacheDependency(path), DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null); writer.Write(str3); } else { str3 = RazorEngineServiceExtensions.IsTemplateCached(Engine.Razor, str, null) ? RazorEngineServiceExtensions.Run(Engine.Razor, str, null, jsonData, null) : RazorEngineServiceExtensions.RunCompile(Engine.Razor, new LoadedTemplateSource(str4, null), str, null, jsonData, null); writer.Write(str3); } }
public override IEnumerable <IRow> Operate(IEnumerable <IRow> rows) { var fileBasedTemplate = Context.Process.Templates.FirstOrDefault(t => t.Name == Context.Operation.Template); if (fileBasedTemplate != null) { Context.Operation.Template = fileBasedTemplate.Content; } _input = Context.Entity.GetFieldMatches(Context.Operation.Template).ToArray(); var key = GetHashCode(Context.Operation.Template, _input); if (!Cache.TryGetValue(key, out _service)) { var config = new TemplateServiceConfiguration { DisableTempFileLocking = true, EncodedStringFactory = Context.Operation.ContentType == "html" ? (IEncodedStringFactory) new HtmlEncodedStringFactory() : new RawStringFactory(), Language = Language.CSharp, CachingProvider = new DefaultCachingProvider(t => { }) }; _service = RazorEngineService.Create(config); } try { RazorEngineServiceExtensions.Compile(_service, (string)Context.Operation.Template, (string)Context.Key, typeof(ExpandoObject)); Cache[key] = _service; } catch (Exception ex) { Context.Error(Context.Operation.Template.Replace("{", "{{").Replace("}", "}}")); Context.Error(ex.Message); Run = false; } return(base.Operate(rows)); }