Skip to content

Blazor: Modify child content? #16737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Postlagerkarte opened this issue Nov 1, 2019 · 3 comments
Closed

Blazor: Modify child content? #16737

Postlagerkarte opened this issue Nov 1, 2019 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components question
Milestone

Comments

@Postlagerkarte
Copy link

Postlagerkarte commented Nov 1, 2019

Is there a simple way to modify the rendered child content?

Example:

<MyListWrapper>
         <li class="list-group-item">Cras justo odio</li>
         <li class="list-group-item">Cras justo odio</li>
</MyListWrapper>

MyComponent.razor

<div class="mystuff">
@ChildContent
</div>

   [Parameter]
   public RenderFragment ChildContent { get; set; }

This outputs:

<div class="mystuff">
  <li class="list-group-item">Cras justo odio</li>
  <li class="list-group-item">Cras justo odio</li>
</div>

However, I want to wrap each passed li item in a special div or wrap another component around it.

So my desired output would be like this:

<div class="mystuff">
<div class="myli">
  <li class="list-group-item">Cras justo odio</li>
</div>
<div class="myli">
  <li class="list-group-item">Cras justo odio</li>
</div>
</div>

or

<div class="mystuff">
<AnotherComponent>
  <li class="list-group-item">Cras justo odio</li>
</AnotherComponent>
<AnotherComponent>
  <li class="list-group-item">Cras justo odio</li>
</AnotherComponent>
</div>

Is this supported?

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components question labels Nov 1, 2019
@javiercn
Copy link
Member

javiercn commented Nov 1, 2019

@Postlagerkarte thanks for contacting us.

I believe you could achieve this with a templated component. Check https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#templated-components

Other than that, it's not possible to get in the middle of a RenderFragment to alter it.

@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Nov 1, 2019
@Postlagerkarte
Copy link
Author

Postlagerkarte commented Nov 1, 2019

@mkArtakMSFT @javiercn :

I believe templated components don't work for the above scenario.

There had been a request for getting more access to the RenderFragment #9493 which got a handful of upvotes before it was closed ( with the hint that it might be done later )

@SteveSandersonMS was asking about use cases for getting access to the RenderFragement in the #9493 issue.

My current use case is allowing the user to easily create a draggable list. Therefore I want the user to be able to wrap a component around his <li> tags - the component would add the relevant stuff to each <li> tag to make it draggable.

Furthermore, I also was thinking about something like this

<SyntaxHighlight>
..... code here ...
</SyntaxHighlight>

@mkArtakMSFT
Copy link
Contributor

Hi.

It looks like you are posting on a closed issue!
We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

@dotnet dotnet locked and limited conversation to collaborators Nov 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

3 participants