public static MvcHtmlString GetGridHtml(this HtmlHelper html, IPublishedElement contentItem, string propertyAlias, string framework) { if (propertyAlias == null) { throw new ArgumentNullException(nameof(propertyAlias)); } if (string.IsNullOrWhiteSpace(propertyAlias)) { throw new ArgumentException("Value can't be empty or consist only of white-space characters.", nameof(propertyAlias)); } var view = "Grid/" + framework; var prop = contentItem.GetProperty(propertyAlias); if (prop == null) { throw new InvalidOperationException("No property type found with alias " + propertyAlias); } var model = prop.GetValue(); var asString = model as string; if (asString != null && string.IsNullOrEmpty(asString)) { return(new MvcHtmlString(string.Empty)); } return(html.Partial(view, model)); }
/// <summary> /// Gets the value of a content's property identified by its alias, converted to a specified type. /// </summary> /// <typeparam name="T">The target property type.</typeparam> /// <param name="content">The content.</param> /// <param name="publishedValueFallback">The published value fallback implementation.</param> /// <param name="alias">The property alias.</param> /// <param name="culture">The variation language.</param> /// <param name="segment">The variation segment.</param> /// <param name="fallback">Optional fallback strategy.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The value of the content's property identified by the alias, converted to the specified type.</returns> /// <remarks> /// <para> /// The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering /// content. /// </para> /// <para> /// If no property with the specified alias exists, or if the property has no value, or if it could not be /// converted, returns <c>default(T)</c>. /// </para> /// <para> /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the /// converter. /// </para> /// <para>The alias is case-insensitive.</para> /// </remarks> public static T?Value <T>( this IPublishedElement content, IPublishedValueFallback publishedValueFallback, string alias, string?culture = null, string?segment = null, Fallback fallback = default, T?defaultValue = default) { IPublishedProperty?property = content.GetProperty(alias); // if we have a property, and it has a value, return that value if (property != null && property.HasValue(culture, segment)) { return(property.Value <T>(publishedValueFallback, culture, segment)); } // else let fallback try to get a value if (publishedValueFallback.TryGetValue(content, alias, culture, segment, fallback, defaultValue, out T? value)) { return(value); } // else... if we have a property, at least let the converter return its own // vision of 'no value' (could be an empty enumerable) - otherwise, default return(property == null ? default : property.Value <T>(publishedValueFallback, culture, segment)); }
public static MvcHtmlString GetGridHtml(this HtmlHelper html, IPublishedElement publishedElement, string propertyAlias) { if (propertyAlias == null) { return(new MvcHtmlString("")); } var model = publishedElement .GetProperty(propertyAlias) .GetValue(); return(html.Partial("Grid/bootstrap3", model)); }
// fixme - .Value() refactoring - in progress // trying to reproduce Umbraco.Field so we can get rid of it // // what we want: // - alt aliases // - recursion // - default value // - before & after (if value) // // convertLineBreaks: should be an extension string.ConvertLineBreaks() // stripParagraphs: should be an extension string.StripParagraphs() // format: should use the standard .ToString(format) // // see UmbracoComponentRenderer.Field - which is ugly ;-( // recurse first, on each alias (that's how it's done in Field) // // there is no strongly typed recurse, etc => needs to be in ModelsBuilder? // that one can only happen in ModelsBuilder as that's where the attributes are defined // the attribute that carries the alias is in ModelsBuilder! //public static TValue Value<TModel, TValue>(this TModel content, Expression<Func<TModel, TValue>> propertySelector, ...) // where TModel : IPublishedElement //{ // PropertyInfo pi = GetPropertyFromExpression(propertySelector); // var attr = pi.GetCustomAttribute<ImplementPropertyAttribute>(); // var alias = attr.Alias; // return content.Value<TValue>(alias, ...) //} // recurse should be implemented via fallback // todo - that one should be refactored, missing culture and so many things public static IHtmlString Value <T>(this IPublishedElement content, string aliases, Func <T, string> format, string alt = "") { if (format == null) { format = x => x.ToString(); } var property = aliases.Split(',') .Where(x => string.IsNullOrWhiteSpace(x) == false) .Select(x => content.GetProperty(x.Trim())) .FirstOrDefault(x => x != null); return(property != null ? new HtmlString(format(property.Value <T>())) : new HtmlString(alt)); }
/// <inheritdoc /> public IPublishedProperty GetProperty(string alias) => _content.GetProperty(alias);
/// <summary> /// Gets a value indicating whether the content has a value for a property identified by its alias. /// </summary> /// <remarks>Returns true if <c>GetProperty(alias)</c> is not <c>null</c> and <c>GetProperty(alias).HasValue</c> is <c>true</c>.</remarks> public static bool HasValue(this IPublishedElement content, string alias, string culture = null, string segment = null) { var prop = content.GetProperty(alias); return(prop != null && prop.HasValue(culture, segment)); }