Skip to content

Instantly share code, notes, and snippets.

@MissaouiChedy
Last active February 20, 2017 22:10
Show Gist options
  • Save MissaouiChedy/3053d1507779fa39d7ecc2b8b58c1a54 to your computer and use it in GitHub Desktop.
Save MissaouiChedy/3053d1507779fa39d7ecc2b8b58c1a54 to your computer and use it in GitHub Desktop.
public class WrapperTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes.SetAttribute(new TagHelperAttribute("class", "wrapper-tag-helper"));
string innerContent = renderInnerTagHelper(context);
string finalContent = $@"<p>Wrapper</p>
{innerContent}";
output.Content.SetHtmlContent(finalContent);
}
/*
* This methods creates, processes and renders the InnerTagHelper
*/
private string renderInnerTagHelper(TagHelperContext context)
{
InnerTagHelper innerTagHelper = new InnerTagHelper();
// Create a TagHelperOutput instance
TagHelperOutput innerOutput = new TagHelperOutput(
tagName: "",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
Task.Factory.StartNew<TagHelperContent>(
() => new DefaultTagHelperContent()
)
);
// Process the InnerTagHelper instance
innerTagHelper.Process(context, innerOutput);
// Render and return the tag helper attributes and content
return $"<{innerOutput.TagName} {renderHtmlAttributes(innerOutput)}>{innerOutput.Content.GetContent()}</{innerOutput.TagName}>";
}
.
.
<code omitted here>
.
.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment