public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var localizedResourceService = HostContainer.GetInstance <IEzCMSLocalizedResourceService>(); var type = Type.GetType(DataType); if (type != null) { var razorValidMessage = string.Empty; try { var cacheName = Subject.GetTemplateCacheName(Body); EzRazorEngineHelper.TryCompileAndAddTemplate(Body, cacheName, type); } catch (TemplateParsingException exception) { razorValidMessage = exception.Message; } catch (TemplateCompilationException exception) { razorValidMessage = string.Join("\n", exception.CompilerErrors.Select(e => e.ErrorText)); } catch (Exception exception) { razorValidMessage = exception.Message; } if (!string.IsNullOrEmpty(razorValidMessage)) { yield return(new ValidationResult(string.Format(localizedResourceService.T("EmailTemplate_Message_TemplateCompileFailure"), razorValidMessage), new[] { "Body" })); } } }
/// <summary> /// Validate the model /// </summary> /// <param name="context">the validation context</param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext context) { var widgetService = HostContainer.GetInstance <IWidgetService>(); var localizedResourceService = HostContainer.GetInstance <IEzCMSLocalizedResourceService>(); if (_pageTemplateService.IsPageTemplateNameExisted(Id, Name)) { yield return(new ValidationResult(localizedResourceService.T("PageTemplate_Message_ExistingName"), new[] { "Name" })); } //Check if content is valid if (widgetService.IsPageTemplateValid(Content)) { yield return(new ValidationResult(localizedResourceService.T("PageTemplate_Message_InvalidContentFormat"), new[] { "Content" })); } var razorValidMessage = string.Empty; try { var cacheName = Name.GetTemplateCacheName(Content); // Template is updated here EzRazorEngineHelper.TryCompileAndAddTemplate(Content, cacheName, typeof(PageRenderModel), ResolveType.Layout); } catch (TemplateParsingException exception) { razorValidMessage = exception.Message; } catch (TemplateCompilationException exception) { razorValidMessage = string.Join("\n", exception.CompilerErrors.Select(e => e.ErrorText)); } catch (Exception exception) { razorValidMessage = exception.Message; } if (!string.IsNullOrEmpty(razorValidMessage)) { yield return(new ValidationResult(string.Format(localizedResourceService.T("PageTemplate_Message_TemplateCompileFailure"), razorValidMessage), new[] { "Content" })); } }
/// <summary> /// Add template to cache /// </summary> /// <param name="template"></param> /// <param name="cacheName"></param> /// <param name="type"></param> /// <param name="resolveType"></param> public static ITemplateKey TryCompileAndAddTemplate(string template, string cacheName, Type type, ResolveType resolveType = ResolveType.Global) { return(EzRazorEngineHelper.TryCompileAndAddTemplate(template, cacheName, type, resolveType)); }
/// <summary> /// Reset cache name for all hierarchies templates of page /// If null then reset all the application /// </summary> /// <param name="pageTemplateId"></param> public void Initialize(int?pageTemplateId = null) { var currentPageTemplate = GetById(pageTemplateId); // Get all templates List <PageTemplate> pageTemplates; // If has current template, then only refresh the Template tree if (currentPageTemplate != null) { pageTemplates = _pageTemplateRepository.GetHierarchies(currentPageTemplate).ToList(); } // If no current template then reload all trees else { pageTemplates = GetAll().OrderBy(t => t.Hierarchy).ToList(); } // Reset all effected templates foreach (var pageTemplate in pageTemplates) { var template = pageTemplate.Content.ParseProperties(typeof(PageRenderModel)); var parentTemplate = GetById(pageTemplate.ParentId); //If parent template cache name is null, then it has parsing errors and all children templates is not valid if (parentTemplate != null && string.IsNullOrEmpty(parentTemplate.CacheName)) { pageTemplate.IsValid = false; pageTemplate.CacheName = string.Empty; pageTemplate.CompileMessage = parentTemplate.CompileMessage; } //Generate child template with parent as master else if (parentTemplate != null) { var parentCacheName = parentTemplate.CacheName; template = template.InsertMasterPage(parentCacheName); // Generate new template cache name base on parent Template and content pageTemplate.CacheName = pageTemplate.Name.GetTemplateCacheName(template); } else { pageTemplate.CacheName = pageTemplate.Name.GetTemplateCacheName(template); } if (!string.IsNullOrEmpty(pageTemplate.CacheName)) { var compileMessage = string.Empty; try { var templateKey = EzRazorEngineHelper.TryCompileAndAddTemplate(template, pageTemplate.CacheName, typeof(PageRenderModel), ResolveType.Layout); pageTemplate.IsValid = true; pageTemplate.CompileMessage = string.Empty; pageTemplate.CacheName = templateKey.Name; } catch (TemplateParsingException exception) { compileMessage = exception.Message; } catch (TemplateCompilationException exception) { compileMessage = string.Join("\n", exception.CompilerErrors.Select(e => e.ErrorText)); } catch (Exception exception) { compileMessage = exception.Message; } finally { if (!string.IsNullOrEmpty(compileMessage)) { pageTemplate.IsValid = false; pageTemplate.CompileMessage = TFormat("PageTemplate_Message_InvalidPageTemplate", pageTemplate.Name, compileMessage); pageTemplate.CacheName = string.Empty; } } } Update(pageTemplate); } }