/// <summary> /// Process / fill a template with a set of key value pairs /// </summary> /// <param name="key"></param> /// <param name="values"></param> /// <returns></returns> public String Process(BlogViewTemplatePart key, List <BlogViewTemplateReplacement> values) { // Get the content (will return an empty content template if it fails) try { // Get the content IHtmlContent content = Get(key); // Something to process? String renderedContent = (content != null) ? HtmlHelpers.GetString(content) : ""; if (renderedContent != "") { // For each replacement text values.ForEach(replacement => { // Replace the value and check if it needs encoding for anti XSS or not renderedContent = renderedContent.Replace(ReplacementStartMarker + replacement.SearchString + ReplacementEndMarker, replacement.Encode ? WebUtility.HtmlEncode(replacement.Content) : replacement.Content); }); } // Send the rendered content back return(renderedContent); } catch (Exception ex) { throw BlogException.Passthrough(ex, new HtmlTemplateNotProcessedBlogException(ex)); } }
/// <summary> /// Standardised content fill function to provide IHtmlContent based on a /// template and a set of replacement values /// </summary> /// <param name="part">The id of the template part</param> /// <param name="contentValues">A list of replacement values to process in the template</param> /// <param name="viewModel">The viewmodel to provide a link to the templates being used</param> /// <returns></returns> private static IHtmlContent ContentFill( BlogViewTemplatePart part, List <BlogViewTemplateReplacement> contentValues, BlogViewModelBase viewModel) { // Create the tag builders to return to the calling MVC page HtmlContentBuilder contentBuilder = new HtmlContentBuilder(); // Append the processed Html to the content builder contentBuilder.AppendHtml(viewModel.Templates.Process(part, contentValues)); // Return the builder return(contentBuilder); }
/// <summary> /// Get a HtmlTemplate from the dictionary with proper error trapping /// </summary> /// <param name="key">The key to search for</param> /// <returns>The Html Content for the key</returns> public IHtmlContent Get(BlogViewTemplatePart key) => Contains(key) ? Templates[key] : throw new HtmlTemplateNotFoundBlogException();
/// <summary> /// Does the template list contain a given key? /// </summary> /// <param name="key">The key to search for</param> /// <returns>If the key exists</returns> public Boolean Contains(BlogViewTemplatePart key) => Templates.ContainsKey(key);