aspnet / Razor

[Archived] Parser and code generator for CSHTML files used in view pages for MVC web apps. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
883 stars 226 forks source link

[Feature Request] Support pulgins in razor pages for rendering #2638

Closed hez2010 closed 5 years ago

hez2010 commented 5 years ago

Currently the content in a node will be rendered as what it has been written in cshtml files. I hope that you can provide the ability to use plugins.

For example, assuming we have a script compiler babel-loader and a UI library material-design:

@{
    var babeloption = new {
        Options = new { 
            presets = new [] {"babel-presets-env" },
            option = new [] { targets = new { ie = "11" } } 
        }
    };
}
<script asp-plugin-babel-loader="@babeloption">
.... // The scripts in this node will be preprocessed by plugin babel-loader with option babeloption.Options before final rendering. 
</script>

@{
    var mdoption = new {
        RenderAs = "md" // then we can use components defined in the library with prefix "md:"
    };
}

<div asp-plugin-material-design="@mdoption">
   <md:button> ... </md:button>
</div>

<xxx asp-plugin-[plugin name]="[options]">
</xxx>

The plugins can be installed from nuget or referenced from other .net standard assemblies.

This feature will brings a lot of features such as js minimize, browser polyfill, UI libraries and so on in the future (it needs supports both from official and community) and also razor will be a great solution for server rendering.

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @hez2010. @danroth27, what are your thoughts regarding this feature request?

danroth27 commented 5 years ago

Hi @hez2010. Thanks for the suggestion! It's not really clear to me what specifically you'd like to see us add to the framework. I would suggest starting out with implementing your ideas for a plugin system as a community project. Then if you uncover any specific limitations in ASP.NET Core for implementing your plugin system we'd be happy to consider addressing them. Once you've had a chance to prove out your ideas we can take a look at whether it makes sense to incorporate them directly into ASP.NET Core, but often we find that projects of this nature are best left to the community.

Since there isn't anything immediately actionable here, I'm going to go ahead and close this issue. Hope that's OK!

hez2010 commented 5 years ago

@danroth27 Thanks for reply. I suggest it because recently I used other frontend framework based on nodejs such as Vue, react and so on. All of them can install all kinds of packages(UI libraries, assemblies, preprocessors...)to extend their feature instead of writing original html to simplify the work. I found that the extensibility of razor really need to be improved. With the plugin system, we no longer need to write original html with a lot of nested elements and class tags, just using the predefined UI components in UI library is okay and it will be rendered to final html automatically. This is also available for scripts, we can use script plugins to enable polyfill, js minimizing and compilation. For example, with a plugin which can compile js to match es5 standard, we can use es6 even es7 features without worrying about the compatibility for IE 11. Another example is UI libraries, we can use the components defined in those libraries such as

<bootstrap:button color="primary">button</bootstrap:button>

, it will be automatically rendered to

<button class="btn btn-primary">button<button>

And a simple

<bootstrap:modal title="...">...</bootstrap:modal>

can be rendered to

 <div class="modal fade" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">....</h4>
            </div>
            <div class="modal-body">...</div>
            <div class="modal-footer">....</div>
        </div>
    </div>
</div>

automatically. In these cases (UI aspects) we use properties, content/children nodes just like what WPF and UWP did instead of class names and complicated nested html elements.

More or less it will simplify the frontend development and make razor a better server rendering solution.

danroth27 commented 5 years ago

@hez2010 We do have some investments in progress right now to provide a UI composition model for ASP.NET Core. In ASP.NET Core 2.1 we introduced support for Razor Class Libraries where you can prebuild some UI logic and add it to an existing project as a NuGet package. We've been also working on an experimental project called Blazor that introduces a component model that can be used both on the server and also client side in the browser via a WebAssembly based .NET runtime. We recently announced that we plan to integrate the server-side Blazor support into ASP.NET Core as part of .NET Core 3.0. It would be great if you could take a look at these efforts and see if they satisfy your scenarios.