Calling TagHelpers from another TagHelper?
Asked Answered
H

2

8

Is there any way to get a TagHelper to render another TagHelper?

Example TagHelpers;

public class OuterTagHelper : TagHelper
{
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "";
        output.Content.SetContent("Hello <inner></inner>");
    }
}

public class InnerTagHelper : TagHelper
{
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "";
        output.Content.SetContent("World!");
    }
}

Example View;

<outer></outer>

Expected Result;

Hello World!

I know I probably should use a ViewComponent instead, but is it at all possible to achieve the expected behavior presented above?

Henri answered 21/9, 2015 at 10:4 Comment(1)
You could nest them in your razor view as in this question, but I don't think you can call another template from within the process method itself. (Maybe you could by coupling them and manually calling Process, but that doesn't sound like a great idea)Victorie
N
0

@Daniel J.G. is right, this is not recommended. Technically you could do it if you ran your content back through your highly customized razor parser and then executed the content as if it were a page - but it's almost certainly the wrong approach to your problem.

Northwesterly answered 24/9, 2015 at 17:15 Comment(0)
C
3

If you need to use some of the existing tag helper type functionality then you can access it from IHtmlGenerator. This won't help to access custom tag helper functionality but if you just need the functionality of existing stuff like label, input then this will work

[HtmlTargetElement("input", Attributes = nameof(Wrap) + ", asp-for")]
[HtmlTargetElement("select", Attributes = nameof(Wrap) + ", asp-for")]
public class FormGroupWrapperTagHelper : TagHelper
{
    public FormGroupWrapperTagHelper(IHtmlGenerator generator)
    {
        Generator = generator;
    }

    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    protected IHtmlGenerator Generator { get; }

    public ModelExpression AspFor { get; set; }
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        var generateValidationMessage = Generator.GenerateValidationMessage(ViewContext,
                                                                  AspFor.Name,
                                                                  message: null,
                                                                  tag: null,
                                                                  htmlAttributes: null);
.....
}
Clegg answered 11/11, 2015 at 0:28 Comment(0)
N
0

@Daniel J.G. is right, this is not recommended. Technically you could do it if you ran your content back through your highly customized razor parser and then executed the content as if it were a page - but it's almost certainly the wrong approach to your problem.

Northwesterly answered 24/9, 2015 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.