Skip to content
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

Feature Request: View Component Slots #37434

Open
seangwright opened this issue Oct 11, 2021 · 11 comments
Open

Feature Request: View Component Slots #37434

seangwright opened this issue Oct 11, 2021 · 11 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one needs-prototype
Milestone

Comments

@seangwright
Copy link
Contributor

seangwright commented Oct 11, 2021

Is your feature request related to a problem? Please describe.

ASP.NET Mvc has never had a robust component model, the way Web Forms did. ASP.NET Core Tag Helpers and View Components have helped improve this story, but they still lack some composition patterns.

Creating container/wrapping markup is possible with Tag Helpers, but Tag Helpers are a C#-first approach to markup and container/wrapping markup is almost always related to visuals along with functionality. HTML is far easier to maintain in Razor vs C# strings.

It would be helpful to be able to use View Components instead of Tag Helpers for container/wrapping markup when rendering common HTML that contain different children (as defined by the Razor calling the View Components).

I'm specifically requesting this for non-Blazor ASP.NET Core Razor HTML rendering and as a request for ASP.NET Core 7.

Describe the solution you'd like

There is an existing proposal (and sample/demo repository) for this feature, but the original feature request is a locked issue:
#4901

Examples can be seen in the original issue, but here is a short one:

@* Index.cshtml *@
<vc:card image="@imagePath">
    <text vc:slot="title">Card title with a <a href="...">Link</a></text>
    <div vc:slot="content">
       <p>Lorem ipsum</p>
       <p>Lorem ipsum</p>
       <p>Lorem ipsum</p>
    </div>
</vc:card>
@* ~/Components/Card/Default.cshtml *@

<div class="card" style="width: 18rem;">
  <vc:slot name="image">
    <img src="@Model.ImagePath" class="card-img-top" alt="...">
  </vc:slot>
  <div class="card-body">
    <h5 class="card-title">
        <vc:slot name="title />
    </h5>
    <div class="card-text">
        <vc:slot name="content />
    </div>
  </div>
</div>

Additional context

This is how other frameworks do slots:

Notes

If you are interested in this feature being explored for future versions of ASP.NET Core, add a thumbs up/reaction to this issue as that is how the ASP.NET Core team determines interest

To make a decision whether an issue is a good candidate or not for the upcoming release, we will look into the number of upvotes 👍 (and other reactions).

@mkArtakMSFT mkArtakMSFT added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Oct 11, 2021
@mkArtakMSFT mkArtakMSFT added this to the .NET 7 Planning milestone Oct 11, 2021
@joaofx
Copy link

joaofx commented Oct 12, 2021

+1. I miss this feature A LOT in Razor.

It helps identify and extract repetitive html or patterns that we see in the UI.

Laravel's View Engine, Blade, has components as well: https://laravel.com/docs/8.x/blade#components

@michelanjos
Copy link

+1. I had used this feature in VueJs and miss it in Razor. It will be really helpful.

@mkArtakMSFT mkArtakMSFT modified the milestones: .NET 7 Planning, Backlog Nov 11, 2021
@ghost
Copy link

ghost commented Nov 11, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@keatkeat87
Copy link

Really sad seeing this issue been removed from .NET 7 planning. This is very "basic" feature when working with components.
Currently, I am using Templated Razor delegates to pass HTML into the component as a workaround.
Will this be implemented at .NET 8?

@Eirenarch
Copy link

The MVC part of ASP.NET really needs some love and this seems like a good proposal which moves the framework in the proper direction (components over views/actions)

@ghost
Copy link

ghost commented Oct 5, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@romanimm
Copy link

romanimm commented Feb 23, 2023

The Component.InvokeAsync has a way to use "slots". Proper tag helper support would be nice though.

// index.cshtml
@await Component.InvokeAsync(typeof(AccordionViewComponent), new AccordionModel("Accordion Title", @<div>Content</div>))
public class AccordionViewComponent : ViewComponent
{
    public IViewComponentResult Invoke(AccordionModel model)
    {
        return View(model);
    }
}

public record AccordionModel(string Title, Func<object?, HelperResult> Body);
// Default.cshtml (Accordion)
@model AccordionModel

<details>
    <summary>@Model.Title</summary>
    @Model.Body.Invoke(null)
</details>

@MichalSznajder
Copy link

Templated Razor delegates is such a nice feature. If only they got some love....

@abraluve
Copy link

abraluve commented Mar 6, 2024

Voting up, there are still many active projects that cant support blazor yet.

@avveshka
Copy link

avveshka commented Jul 5, 2024

+1

@joaofx
Copy link

joaofx commented Jul 5, 2024

I have found this library the other day and so far so good: https://razor-components.techgems.net/docs/basic-usage/

I highly recommend checking it out to design Razor cshtml Components with ChildContent and Slots.

Great job @techgems!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one needs-prototype
Projects
None yet
Development

No branches or pull requests