Custom TagHelper to suppress output

2 posts, 0 answers
  1. Richard
    Richard avatar
    7 posts
    Member since:
    Sep 2018

    Posted 27 Sep 2019 Link to this post

    Hi, I have a custom TagHelper that I have written to suppress the output of an HTML element. Basically it allows me to provide a comma delimited list of policies and if the user does not pass that check, all output is suppressed. I can add these TagHelpers to a kendo TagHelper, but of course it does not work, as I know Kendo is doing a lot internally. Is there a way to get this to work though?

    If I take the if statement out of this class and wrap the Kendo component with it, it will indeed work. I've also tried messing with the Order property of the TagHelper itself, to no avail. One item I am trying to use this on is the <panelbar-item /> to limit navigation options.

    The code in the custom TagHelper class is as follows.

    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        if (!(await _authorizationService.AuthorizeAsync(ViewContext.HttpContext.User, Policy)).Succeeded)
        {
            output.SuppressOutput();
        }
    }
  2. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1240 posts

    Posted 02 Oct 2019 Link to this post

    Hello Richard,

    The Kendo TagHelpers all implement their own version of the public async Task ProcessAsync(TagHelperContext context, TagHelperOutput output). The PanelBarItemTagHelper, for example, implements its override in the following way:

    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        var type = GetType();
        if (context.Items.ContainsKey(type))
        {
            PreviousInstance = context.Items[type] as PanelBarItemTagHelper;
        }
    
        context.Items[type] = this;
        var child = await output.GetChildContentAsync();
        AddSelfToParent(context);
        context.Items[type] = PreviousInstance;
    
        output.TagName = "li";
        output.TagMode = TagMode.StartTagAndEndTag;
    
        Href = output.Attributes.FirstOrDefault(a => a.Name == "href")?.Value.ToString();
        TagBuilder enhancedItem = EnhanceItem();
        output.MergeAttributes(enhancedItem);
    
        output.Content.AppendHtml(enhancedItem.InnerHtml);
        output.Content.AppendHtml(child);
    
        if (Visible.HasValue && !Visible.Value)
        {
            output.SuppressOutput();
        }
    }

    Having that said, what I would suggest you is to plug your logic directly in the Kendo TagHelpers source code (the Kendo.Mvc\TagHelpers\PanelBar\PanelBarTagHelper.cs class) and use a custom build of the Telerik UI for ASP.NET Core package. The source code is available for download from your Telerik account.

    Regards,
    Veselin Tsvetanov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top