Design review: Layouts
Currently, the layouts feature uses temporary fake directives such as @(Layout<SomeType>()). Once we're able to put in Blazor-specific directives, we should remove this and clean up the API.
We'll also review to what extent we want to make it look similar to Razor Pages layouts. It's not the same thing as Razor Pages layouts because it's a stateful component architecture rather than a one-time HTML-string generator, but we may choose to make the APIs more similar.
Maybe have a convention where a file named _Layout.cshtml is by convention a layout component?
@danroth27 I suggested these days! https://github.com/aspnet/Blazor/issues/130
In a hybrid scenario, will the design continue to be a separate layout for the Blazor app or will there be a mechanism to fallback on the layout mechanism of the underlying MVC application?
Something like
app.UseBlazor(new BlazorOptions(){
UseCurrentLayout = true;
});
Please, rename the _ViewImports.cshtml inside the Shared folder that references the layout page to _ViewStart.cshtml
Today the project has two _ViewImports.cshtml
Aye!
The project structure still contains names inconsistent with what exists today in ASP.NET Core. For example, inside the Pages folder, the _ViewImports.cshtml file does what a _ViewStart.cshtml file would do in a Web project. The MainLayout.cshtml file could be called _Layout.cshtml, and the @Body property implemented by the BlazorLayoutComponent class could if called @RenderBody.
These things do not affect the functionality of the framework, it's just for consistency across platforms! Things do not work in the same way in both frameworks of course, but in the eyes of the more laymen, they will seek this kind of resemblance for a smaller learning curve. Presenting Blazor with a structure almost equal to an ASP.NET Core framework, how to differentiate the client side compilation through WebAssembly is already in itself a way of doing marketing
Following the same default file placement structure, naming the files between frameworks, decreases developer insecurity when it looks at something new, and encourages you to experience the product more intelligently
@jonathanperis - thanks this is a good feedback.
Regarding _ViewImports.cshtml this is a new feature in ASP.NET Core, and it doesn't overlap with _ViewStart. ViewImports act like includes and allow you to specify directives (not code) that are included in all of the related files. ViewStart on the other hand is a place to put code that will be executed before all of the related fails. In many ways _ViewImports replaces some of the thing there were done using web.config in the older versions of ASP.NET.
Blazor doesn't have a concept like _ViewStart and we haven't discussed adding it.
@rynowak - I'll illustrate what i'm trying to say. For clarification only
Folders in different locations. Files doing the samething, with different names.
Files doing the samething on different ways
Files doing the samething with different names
These things I'm pointing to do not affect the functionality of the framework as it stands today. As you can see, it's just for the sake of consistency across platforms. Organization, which lowers the learning curve for anyone who has never seen the new content of Blazor. Not us insiders, who are accompanying commit to commit project, but the general public, who will possibly see Blazor, if it turns into a real product of Microsoft. It is important to present consistency between different projects, even though they do the same thing differently, the developer is less confused by these situations.
The things that are most similar are done differently, but those that are no longer should be differentiated for the end customer. Blazor was developed as a Web site, only with a different engine from the current web sites, in this case WebAssembly. It was not necessary to change the structure and nomenclature solely because of the same.
My point of view. What do you think?
Cheers!
Any thoughts about this?
@jonathanperis Thanks for your suggestions about this!
The reason we haven't had much to say about it is just that we're not focusing on layouts as an urgent priority at the moment. We're designing broader aspects of the framework and will return to layouts APIs at a later date.
Also to consider: support @layout null (copied from #1224)
May I ask, why does Blazor need Layouts at all? Layouts are a really helpful feature in multi-page web applications so that many pages can absorb the same layout, but isn't Blazor intended more for Single Page Applications?
(It says "Blazor is a single-page web app framework" here: https://blazor.net/docs/introduction/faq.html)
In an SPA, everything is a component inside one single parent component, so you could get by without any Layouts at all (I think)... am I missing something?
Single Page Apps can still have multiple pages, but they are managed on the client instead of making requests back to the server. SPA frameworks typically have some sort client-side routing support for handling this, just like Blazor does.
Thank you for the response, @danroth27 .
I suppose I was imagining a scenario in which the root control simply loads child controls and sets their properties according to what it needs to be displayed in a top-down way. I'm trying to think of the benefits of having layouts. Would this be one below?
- We have a form control as its own component. Let's say it's an
<input type="password" />tag (but it could be something else). - We have a validator control with some functionality such as loading the minimum password requirements (e.g. must contain upper-case letter and symbol) from the server and enforcing them on a form field.
- We have two layouts whose only purpose is to render all the
<div>tags and CSSclass=""attributes required to make the form control look pretty and the validation messages show/hide correctly. One layout does this for Bootstrap. The other layout for Google Material Design. - At design time, the developer places the form control component and the validator component on the page and specifies which layout he wants depending on which presentation framework he's using; Bootstrap or Material Design.
By the way, I think what you're making with Blazor is by far the most exciting thing to have happened in web development in the last two decades. I'm very excited about the release and about the features that are going to be added before then. Keep up the good work.
What do you think about adding "sections" support to Layout Components?
I know that the concept to "sections" seems a little at odds with component in general, but I feel that they are useful in certain cases.
Like for having a menu that changes depending on the page/component that uses the layout.
@Menu
<div class="content">
@Body
</div>
@code {
public RenderFragment Menu { get; set; }
public RenderFragment Body { get; set; }
}
<h2>Hello, World!</h2>
<p>Foobar</p>
@section Menu {
<NavMenu />
}
The section is a RenderFragment and could therefore take an argument passed by the LayoutComponent @section Menu(arg) { /* ... */ }.
I am aware that this might get ugly if used the wrong way.
It would enable scenarios like this where you want to set parameters in a section in the LayoutComponent.
@section Menu {
<NavMenu CustomerNo="no" />
}
But then... what we actually need is template parameters.
This feature has changed quite a bit since this was initially filed. If you think there are further issues to be addressed, please file a new issue.