Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Add support to allow Tag Helpers to generate content before and after source element #341

Closed
DamianEdwards opened this issue Apr 1, 2015 · 1 comment

Comments

@DamianEdwards
Copy link
Member

Tag Helpers can specify content to appear before and after the contents of the HTML element they're operating on. Sometimes however, a Tag Helper will want to generate content that appears before or after the element itself, e.g. the <link> and <script> Tag Helpers in MVC. Currently, that requires them to manually rebuild the original element by suppressing the default output and constructing the original HTML using the information from TagHelperContext and TagHelperOutput. This should be simpler for Tag Helpers to do.

We should add new properties to TagHelperOutput that enable this, e.g.:

public TagHelperContent PreElement { get; }
public TagHelperContent PostElement { get; }

Example

Source file:

<my-tag-helper>
    Content in source
</my-tag-helper>

Tag Helper file:

public void Process(TagHelperContext context, TagHelperOutput output)
{
    var nl = Environment.NewLine;
    var br = "<br />" + nl;
    output.PreElement.Append("This will appear before source element" + br);
    output.PreContent.Append(nl + "This will appear before source content" + br);
    output.PostContent.Append(br + "This will appear after source content" + nl);
    output.PostElement.Append(br + "This will appear after source element");
}

Output:

This will appear before source element<br />
<my-tag-helper>
This will appear before source content<br />
    Content in source<br />
This will appear after source content
</my-tag-helper><br />
This will appear after source element
@danroth27 danroth27 added this to the 4.0.0-beta5 milestone Apr 2, 2015
@NTaylorMullen NTaylorMullen self-assigned this Apr 13, 2015
NTaylorMullen added a commit that referenced this issue Apr 13, 2015
- These two new properties will enable TagHelper authors to render content before and after the TagHelper's HTML element.
- Added tests to correspond with existing test coverage.
- Modified existing tests to double check for TagHelperOutput.Pre/PostElement.

#341
NTaylorMullen added a commit to aspnet/Mvc that referenced this issue Apr 13, 2015
- Added unit tests to validate that the properties were rendered correctly.
- Modified functional tests to utilize PreElement and PostElement.

aspnet/Razor#341
NTaylorMullen added a commit to aspnet/Mvc that referenced this issue Apr 14, 2015
- Added unit tests to validate that the properties were rendered correctly.
- Modified functional tests to utilize PreElement and PostElement.

aspnet/Razor#341
NTaylorMullen added a commit that referenced this issue Apr 14, 2015
- These two new properties will enable TagHelper authors to render content before and after the TagHelper's HTML element.
- Added tests to correspond with existing test coverage.
- Modified existing tests to double check for TagHelperOutput.Pre/PostElement.
- Refactored all DefaultTagHelperContent pieces of TagHelperOutput to be get only properties.

#341
@NTaylorMullen
Copy link
Member

c9c8e80

aspnet/Mvc@2c4c35e

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants