Consider the following Process
function of a custom TagHelper:
public override async void Process(TagHelperContext context, TagHelperOutput output) {
// ...
var htmlContent = (await output.GetChildContentAsync()).GetContent();
output.Content.SetHtmlContent(htmlContent);
}
I can use the following and it will render the nested taghelper:
<custom-button>
<icon name="search" />
</custom-button>
However I need something more like this:
<custom-button icon="search"></custom-button>
That means I need to somehow render the icon taghelper inside the custom-button taghelper. Here's what I tried and failed:
public override async void Process(TagHelperContext context, TagHelperOutput output) {
// ...
// Set the content (first with the icon tag)
output.Content.SetHtmlContent("<icon name=\"search\" />");
// Render the icon
var htmlContent = (await output.GetChildContentAsync()).GetContent();
// Set the final content
output.Content.SetHtmlContent(htmlContent);
}
Is there any way to achieve this?
EDIT! Here's the full CustomButtonTagHelper code:
[HtmlTargetElement("custom-button")]
public class CustomButtonTagHelper : TagHelper
{
private readonly IFileVersionProvider _fileVersionProvider;
[ViewContext]
public ViewContext ViewContext { get; set; }
public CustomButtonTagHelper(IFileVersionProvider fileVersionProvider)
{
_fileVersionProvider = fileVersionProvider;
}
[HtmlAttributeName("icon")]
public string Icon { get; set; }
public override async void Process(TagHelperContext context, TagHelperOutput output) {
// ...
output.TagMode = TagMode.StartTagAndEndTag;
output.TagName = "button";
// Set the content (first with the icon tag)
output.Content.SetHtmlContent($"<icon name=\"{this.Icon}\" />");
// Render the icon
var htmlContent = (await output.GetChildContentAsync()).GetContent();
// Set the final content
output.Content.SetHtmlContent(htmlContent);
}
}