Strange behavior of display options without a tag

Member since: 2007


It seems like the default behavior is to use the display option's name for selecting the template tag or TemplateModel if the tag is not set. This is a problem if we want to pass in a tag to use and an editor has chosen a display option. E.g.:

@Html.PropertyFor(x => x.CurrentPage.RelatedNewsArticles, new { Tag = "UseThisTag" })

Will not use "UseThisTag" when selecting template if the editor selects a display option, even though that option doesn't have a tag specified.

Now I've solved this by having my own ContentAreaRenderer and overriding GetContentAreaItemTemplateTag like this:

/// <summary>
///     Overridden so we can use the tag even though a display option is selected
///     and that display option's tag is not set.
/// </summary>
/// <param name="htmlHelper">The html helper.</param>
/// <param name="contentAreaItem"> The content area item.</param>
/// <returns>The template tag for the content area item.</returns>
protected override string GetContentAreaItemTemplateTag(HtmlHelper htmlHelper, ContentAreaItem contentAreaItem)
    var tag = htmlHelper.ViewData.GetRenderSettings<string>("Tag");

    if (string.IsNullOrEmpty(tag))
        return base.GetContentAreaItemTemplateTag(htmlHelper, contentAreaItem);

    var displayOption = contentAreaItem.LoadDisplayOption();

    if (displayOption != null && string.IsNullOrEmpty(displayOption.Tag))
        return tag;

    return base.GetContentAreaItemTemplateTag(htmlHelper, contentAreaItem);

I find this behavior a bit strange and think the default implementation should be changed.

#171244 Nov 03, 2016 15:34
  • Member since: 2011

    oh, yeah! :)

    I had to do the same stuff in my bootstrap renderer. I had even more complex case - I had to capture tag to make proper decision about which template to choose and maybe which display option to choose, if developer has set it via attributes.

    Funny part is that, tag is available in Render() method, but not in RenderContentAreaItems() anymore - as it's overwitten by display option.

    #171650 Nov 14, 2016 15:47

    Thanks for this thread/help - I find it very odd the implementation of RenderContentAreaItems overrides the tag with the display option tag.... I'd expect them to be mutually exclusive.

    #176411 Edited, Mar 17, 2017 21:18
  • Member since: 2007

    At least it shouldn't be overwritten with the display option's name when the display option doesn't have a tag specified at all.

    I guess one could argue which tag should be more exclusive, the attribute or the display option, but my main concern is the above.

    #176468 Mar 20, 2017 9:26