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

Tag Helpers: Remove ContentBehavior, replace with single CaptureContentAttribute and put existing functionality on the TagHelperOutput object. #173

Closed
NTaylorMullen opened this issue Oct 1, 2014 · 1 comment

Comments

@NTaylorMullen
Copy link

Right now TagHelper ContentBehavior is determined based on a ContentBehaviorAttribute and enables you to do ONE of the following: None, Append, Prepend, Replace and Modify. One limitation of this is that you can only choose one of the four primary ContentBehaviors (not None). The code that Razor generates is behind this limitation.

If we remove the ContentBehaviorAttribute and replace it with a single CaptureContentAttribute which equates to the old ContentBehavior.Modify we can implement Append, Prepend and Replace within the TagHelperOutput object. This enables us to utilize every ContentBehavior in conjunction with each other.

API Suggestion:

TagHelperOutput
- PreContent: string // This gets rendered right after the start tag is rendered
- Content: string // Setting this implies "Replace", if CaptureContentAttribute is specified, will contain HTML tag content.
- PostContent: string // This gets rendered right before the end tag is rendered

Note: Not happy with the naming of the API suggestion, couldn't think of anything better though.

Code Generation Implications:
The only differences would be an if statement around the TagHelperOutput content generation and the GenerateStartTag => GenerateStart (and "end" equivalents) because it now also renders the PreContent. If we wanted to be explicit instead of renaming GenerateStartTag to GenerateStart we could add a GeneratePreContent method which we'd call into.

__tagHelpersExecutionContext = __tagHelperScopeManager.Begin("p");
__PTagHelper = CreateTagHelper<PTagHelper>();
__tagHelpersExecutionContext.Add(__PTagHelper);

// If the CaptureContentAttribute was supplied we'd buffer the body here (same as ContentBehavior.Modify).

__tagHelpersExecutionContext.Output = await __tagHelperRunner.RunAsync(__tagHelpersExecutionContext, __bufferedBody);
WriteLiteral(__tagHelpersExecutionContext.Output.GenerateStart()); // Renders start tag and pre content
__tagHelperContent = __tagHelpersExecutionContext.Output.GenerateContent();

// If the content is not null that means it was either captured ahead of time or replaced
if (__tagHelperContent != null)
{
    WriteLiteral(__tagHelperContent);
}
else
{
    // Normal body
}

WriteLiteral(__tagHelpersExecutionContext.Output.GenerateEnd()); // Renders end tag and post content.
__tagHelpersExecutionContext = __tagHelperScopeManager.End();

/cc @DamianEdwards

@NTaylorMullen NTaylorMullen added this to the 4.0.0-rc1 milestone Oct 1, 2014
@NTaylorMullen NTaylorMullen changed the title Tag Helpers: Consider removing ContentBehavior, replace with single CaptureContentAttribute and put existing functionality on the TagHelperOutput object. Tag Helpers: Remove ContentBehavior, replace with single CaptureContentAttribute and put existing functionality on the TagHelperOutput object. Oct 21, 2014
@DamianEdwards
Copy link
Member

Dupe of #221

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