From f22d3cdef3c8ea549044bd3d27860339c050eb8c Mon Sep 17 00:00:00 2001 From: Jamie Gilbert Date: Fri, 5 Feb 2016 14:33:02 +0000 Subject: [PATCH] Modified HTML helper to allow you to render a single fieldset from an Archtype model. (I Needed to render the first review in a list of review blocks) --- .../Extensions/HtmlHelperExtensions.cs | 72 +++++++++++++------ 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/HtmlHelperExtensions.cs b/app/Umbraco/Umbraco.Archetype/Extensions/HtmlHelperExtensions.cs index 7606b6a..ee47164 100644 --- a/app/Umbraco/Umbraco.Archetype/Extensions/HtmlHelperExtensions.cs +++ b/app/Umbraco/Umbraco.Archetype/Extensions/HtmlHelperExtensions.cs @@ -12,6 +12,56 @@ namespace Archetype.Extensions /// public static class HtmlHelperExtensions { + /// + /// Renders a single archtype partial from a fieldset + /// + /// The HTML helper. + /// The fieldset model. + /// + public static IHtmlString RenderArchetypePartial(this HtmlHelper htmlHelper, ArchetypeFieldsetModel fieldsetModel) + { + return RenderPartial(htmlHelper, fieldsetModel, null, null); + } + + /// + /// Renders the fieldset partial + /// + /// The HTML helper. + /// + /// The partial path. + /// The view data dictionary. + /// + private static IHtmlString RenderPartial(HtmlHelper htmlHelper, ArchetypeFieldsetModel fieldsetModel, string partialPath, ViewDataDictionary viewDataDictionary) + { + var context = HttpContext.Current; + + if (fieldsetModel == null || context == null) + { + return new HtmlString(""); + } + + var sb = new StringBuilder(); + + var pathToPartials = "~/Views/Partials/Archetype/"; + if (!string.IsNullOrEmpty(partialPath)) + { + pathToPartials = partialPath; + } + + var partial = pathToPartials + fieldsetModel.Alias + ".cshtml"; + + if (System.IO.File.Exists(context.Server.MapPath(partial))) + { + sb.AppendLine(htmlHelper.Partial(partial, fieldsetModel, viewDataDictionary).ToString()); + } + else + { + LogHelper.Info(string.Format("The partial for {0} could not be found. Please create a partial with that name or rename your alias.", context.Server.MapPath(partial))); + } + + return new HtmlString(sb.ToString()); + } + /// /// Renders the archetype partials. /// @@ -72,31 +122,11 @@ namespace Archetype.Extensions { var context = HttpContext.Current; - if (archetypeModel == null || context == null) - { - return new HtmlString(""); - } - var sb = new StringBuilder(); - var pathToPartials = "~/Views/Partials/Archetype/"; - if (!string.IsNullOrEmpty(partialPath)) - { - pathToPartials = partialPath; - } - foreach (var fieldsetModel in archetypeModel) { - var partial = pathToPartials + fieldsetModel.Alias + ".cshtml"; - - if (System.IO.File.Exists(context.Server.MapPath(partial))) - { - sb.AppendLine(htmlHelper.Partial(partial, fieldsetModel, viewDataDictionary).ToString()); - } - else - { - LogHelper.Info(string.Format("The partial for {0} could not be found. Please create a partial with that name or rename your alias.", context.Server.MapPath(partial))); - } + sb.AppendLine(RenderPartial(htmlHelper, fieldsetModel, partialPath, viewDataDictionary).ToString()); } return new HtmlString(sb.ToString());