public static bool Render(string scriptfile, string scriptsPath, ModuleInstanceContext moduleContext, out string result, out string message) { bool retval = false; try { var razorScriptFile = Path.Combine(scriptsPath, scriptfile); if (File.Exists(razorScriptFile) || File.Exists(HostingEnvironment.MapPath(razorScriptFile))) { //Common.Logger.DebugFormat("Rendering Scriptfile [{0}]", HostingEnvironment.MapPath(razorScriptFile)); var engine = new RazorEngine(razorScriptFile, moduleContext, null); var writer = new StringWriter(); engine.Render(writer); result = writer.ToString(); message = ""; retval = true; } else { //Common.Logger.ErrorFormat("Script not found: {0}", razorScriptFile); message = "Script not found"; result = ""; } } catch (Exception e) { message = String.Format("Rendering razor file {0} failed", scriptfile); //Common.Logger.Error(message, e); result = ""; } return(retval); }
private string ExecuteRazor(FileUri template, dynamic model) { string webConfig = template.PhysicalFullDirectory; webConfig = webConfig.Remove(webConfig.LastIndexOf("\\")) + "\\web.config"; if (!File.Exists(webConfig)) { string filename = HostingEnvironment.MapPath("~/DesktopModules/OpenContent/Templates/web.config"); File.Copy(filename, webConfig); } var writer = new StringWriter(); try { var razorEngine = new RazorEngine("~/" + template.FilePath, ModuleContext, LocalResourceFile); razorEngine.Render(writer, model); } catch (Exception ex) { LoggingUtils.RenderEngineException(this, ex); string stack = string.Join("\n", ex.StackTrace.Split('\n').Where(s => s.Contains("\\Portals\\") && s.Contains("in")).Select(s => s.Substring(s.IndexOf("in"))).ToArray()); throw new TemplateException("Failed to render Razor template " + template.FilePath + "\n" + stack, ex, model, template.FilePath); } return(writer.ToString()); }
/// <summary> /// Evaluates token replacement. /// </summary> public string Evaluate(TEntity data, string template) { var razor = new RazorEngine(template, ModuleContext, LocalResourceFile); { var w = new StringWriter(); razor.Render <TEntity>(w, data); { return(w.ToString()); } } }
/// <summary> /// Evaluates token replacement. /// </summary> public string Evaluate(T model, string template) { var razor = new RazorEngine(template, ModuleContext, LocalResourceFile); { var writer = new StringWriter(); razor.Render <T>(writer, model); { return(writer.ToString()); } } }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (!(string.IsNullOrEmpty(RazorScriptFile))) { var razorEngine = new RazorEngine(RazorScriptFile, ModuleContext, LocalResourceFile); var writer = new StringWriter(); razorEngine.Render(writer); Controls.Add(new LiteralControl(HttpUtility.HtmlDecode(writer.ToString()))); } }
/// <summary> /// Evaluates token replacement. /// </summary> public string Evaluate(UserBadge data, decimal points, string template) { var razor = new RazorEngine(template, ModuleContext, LocalResourceFile); { var page = (razor.Webpage as ProfileWebPage); page.ActivityPoints = points; var w = new StringWriter(); razor.Render <UserBadge>(w, data); { return(w.ToString()); } } }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); try { string viewPath = ""; viewPath = UrlHelper.GetViewPath(this.ModuleContext.Configuration.DesktopModule.FolderName, widgetSettings.Theme + "/Widgets/ListArticles", widgetSettings.TemplateFile); if (!string.IsNullOrEmpty(viewPath)) { var razorEngine = new RazorEngine(viewPath, this.ModuleContext, LocalResourceFile); var writer = new StringWriter(); var vc = new ViewController(this.ModuleContext, this.moduleSettings, 0, widgetSettings.ModuleId, widgetSettings.TabId, this.UserInfo); var statusPublished = new List <Components.Enums.ArticleStatus>(); statusPublished.Add(Components.Enums.ArticleStatus.Live); Templates.ViewModels.ListArticlesViewModel vm = new ListArticlesViewModel(); vm.Articles = vc.getArticles(widgetSettings.ModuleId, 0, widgetSettings.ArticlesPerPage, "", statusPublished, true, -1, null, null, DateTime.Now, widgetSettings.SortBy, (widgetSettings.SortDirection.ToLower() == "asc")); vm.TabId = widgetSettings.TabId; if (vm != null) { razorEngine.Render(writer, vm); } else { razorEngine.Render(writer); } Controls.Add(new LiteralControl(writer.ToString())); } } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); try { if (! (string.IsNullOrEmpty(RazorScriptFile))) { var razorEngine = new RazorEngine(RazorScriptFile, ModuleContext, LocalResourceFile); var writer = new StringWriter(); razorEngine.Render(writer); Controls.Add(new LiteralControl(Server.HtmlDecode(writer.ToString()))); } } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } }
private StringWriter RenderTemplate(string virtualPath, dynamic model) { var page = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); var httpContext = new HttpContextWrapper(HttpContext.Current); var pageContext = new WebPageContext(httpContext, page, model); var writer = new StringWriter(); if (page is WebPage) { page.ExecutePageHierarchy(pageContext, writer); } else { var razorEngine = new RazorEngine(virtualPath, null, null); razorEngine.Render <dynamic>(writer, model); } return(writer); }
private void RenderHtmlUsingRazor(BTBWeatherFeedInfo info, string razorTemplate) { //if we don't have a custom XSLT defined then we should just use the //default one which we install with the module string razorFolder = HttpContext.Current.Server.MapPath("~/DesktopModules/BTBYahooWeather/Templates/Razor"); string pathCheck = Path.Combine(razorFolder, razorTemplate); if (!File.Exists(pathCheck)) { //if we don't have the file call back to the default //xslt file which should exist in all instances razorTemplate = "weatherReport.cshtml"; } using (var stringWriter = new StringWriter()) { var razorEngine = new RazorEngine("~/DesktopModules/BTBYahooWeather/Templates/Razor/" + razorTemplate, null, null); razorEngine.Render <MainModel>(stringWriter, new MainModel(info.ModuleId, info.Feed)); info.TransformedFeed = stringWriter.ToString(); } }
public void Render(object source, HtmlTextWriter htmlWriter, TemplateDefinition liveDefinition) { if (!(string.IsNullOrEmpty(liveDefinition.TemplateVirtualPath))) { var resolver = new PathResolver(liveDefinition.Folder); dynamic model = new ExpandoObject(); model.Source = source; model.ControlID = DNNContext.Current.HostControl.ClientID; model.Options = ConvertToJson(liveDefinition.ClientOptions); model.DNNPath = resolver.Resolve("/", PathResolver.RelativeTo.Dnn); model.ManifestPath = resolver.Resolve("/", PathResolver.RelativeTo.Manifest); model.PortalPath = resolver.Resolve("/", PathResolver.RelativeTo.Portal); model.SkinPath = resolver.Resolve("/", PathResolver.RelativeTo.Skin); var modelDictionary = model as IDictionary <string, object>; liveDefinition.TemplateArguments.ForEach(a => modelDictionary.Add(a.Name, a.Value)); var razorEngine = new RazorEngine(liveDefinition.TemplateVirtualPath, null, null); var writer = new StringWriter(); razorEngine.Render <dynamic>(writer, model); htmlWriter.Write(writer.ToString()); } }
public void Render(object source, HtmlTextWriter htmlWriter, TemplateDefinition liveDefinition) { if (!(string.IsNullOrEmpty(liveDefinition.TemplateVirtualPath))) { var resolver = new PathResolver(liveDefinition.Folder); dynamic model = new ExpandoObject(); model.Source = source; model.ControlID = DNNContext.Current.HostControl.ClientID; model.Options = ConvertToJson(liveDefinition.ClientOptions); model.DNNPath = resolver.Resolve("/", PathResolver.RelativeTo.Dnn); model.ManifestPath = resolver.Resolve("/", PathResolver.RelativeTo.Manifest); model.PortalPath = resolver.Resolve("/", PathResolver.RelativeTo.Portal); model.SkinPath = resolver.Resolve("/", PathResolver.RelativeTo.Skin); var modelDictionary = model as IDictionary<string, object>; liveDefinition.TemplateArguments.ForEach(a => modelDictionary.Add(a.Name, a.Value)); var razorEngine = new RazorEngine(liveDefinition.TemplateVirtualPath, null, null); var writer = new StringWriter(); razorEngine.Render<dynamic>(writer, model); htmlWriter.Write(writer.ToString()); } }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); try { if (isView) { //var razorEngine = new RazorEngine(RazorScriptFile, ModuleContext, LocalResourceFile); string viewPath = ""; if (_viewType == ViewType.razorview && !string.IsNullOrEmpty(qsRazorView)) { viewPath = UrlHelper.GetViewPath(this.ModuleContext.Configuration.DesktopModule.FolderName, _settings.BasicRenderingTemplate, qsRazorView); } else { viewPath = UrlHelper.GetViewPath(this.ModuleContext.Configuration.DesktopModule.FolderName, _settings.BasicRenderingTemplate, _viewType.ToString()); } if (!string.IsNullOrEmpty(viewPath)) { var razorEngine = new RazorEngine(viewPath, this.ModuleContext, LocalResourceFile); var writer = new StringWriter(); dynamic vm = null; var vc = new ViewController(this.ModuleContext, this._settings, this._pageIndex, this.ModuleId, this.TabId, this.UserInfo); switch (_viewType) { case ViewType.viewcurrent: vm = vc.getViewCurrentModel(); break; case ViewType.articleview: vm = vc.getArticleViewModel(); break; case ViewType.categoryview: vm = vc.getCategoryViewModel(); break; case ViewType.archiveview: vm = vc.getArchiveViewModel(); break; case ViewType.tagview: vm = vc.getTagViewModel(); break; case ViewType.razorview: vm = vc.getRazorViewModel(); break; default: break; } if (vm != null) { razorEngine.Render(writer, vm); } else { razorEngine.Render(writer); } if (viewOnly) { string responseString = writer.ToString(); responseString = Regex.Replace(responseString, @"^\s*$[\r\n]*", string.Empty, RegexOptions.Multiline).Trim(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.Write(responseString); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.End(); } else { Controls.Add(new LiteralControl(writer.ToString())); } } } } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } }