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

Add methods to TagBuilder to render the tags only #5689

Closed
sebastienros opened this issue Jan 14, 2017 · 8 comments
Closed

Add methods to TagBuilder to render the tags only #5689

sebastienros opened this issue Jan 14, 2017 · 8 comments

Comments

@sebastienros
Copy link
Member

I am using a TagBuilder to create tags dynamically in a view. In MVC5 we could render the start and end tags independently with the same tag builder without having to render the content. If the content is part of the razor view it can't be added to the instance of the tag builder as it needs to be set with InnerHtml.

A solution that I'd like would be to add two methods, RenderStartTag() and RenderEndTag().

I have tried to use the TagRenderMode without success, it renders the end tag twice.

@{
    TagBuilder tag = new TagBuilder("article");
    tag.AddCssClass("big");
    tag.TagRenderMode = TagRenderMode.StartTag;
}
@tag

<div>MY CONTENT</div>

@{
    tag.TagRenderMode = TagRenderMode.EndTag;
}
@tag

It results in

</article>
<div>MY CONTENT</div>
</article>

Ideally I could just write:

@{
    TagBuilder tag = new TagBuilder("article");
    tag.AddCssClass("big");
}
@tag.RenderStartTag()

<div>MY CONTENT</div>

@tag.RenderEndTag()

Or with a syntax that would capture the razor output:

@using(TagBuilder tag = new TagBuilder("article")) 
{
    tag.AddCssClass("big");
    <div>MY CONTENT</div>
}
@dougbu
Copy link
Member

dougbu commented Jan 23, 2017

@sebastienros the problem here is the TagBuilder reuse, specifically changing its TagRenderMode before it has rendered the first time. If you use two TagBuilders, everything would be fine.

@sebastienros
Copy link
Member Author

sebastienros commented Jan 23, 2017

This is another good reason to expose specific rendering methods, that don't use the TagRenderMode

@Eilon
Copy link
Member

Eilon commented May 12, 2017

@sebastienros - is this something you'd like to send a PR for? If not, we will close this.

@sebastienros
Copy link
Member Author

Then I will, thanks.
Who should I talk to about the design?

@dougbu
Copy link
Member

dougbu commented May 12, 2017

@sebastienros me

@Eilon
Copy link
Member

Eilon commented May 12, 2017

Not sure how much "design" there is to do here, but I like the idea of adding RenderStartTag() and RenderEndTag() that do exactly what they say.

@sebastienros
Copy link
Member Author

Not that easy after talking with @dougbu because of the bufferization.

  • We'll expose more methods representing all the available modes. RenderStartTag, RenderSelfClosingTag, RenderBody, RenderEndTag
  • These methods will return a local implementation of IHtmlContent

@dougbu
Copy link
Member

dougbu commented May 12, 2017

These methods will return a local implementation of IHtmlContent

… except RenderBody(), that can just return _innerHtml 😈

@sebastienros sebastienros assigned jbagga and unassigned sebastienros May 17, 2017
jbagga added a commit that referenced this issue May 19, 2017
@jbagga jbagga closed this as completed May 19, 2017
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

4 participants