[Blazor] @if directive
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
I’d like to be able to create markup that is nice to look at and easy for a non-programming colleague to improve the visual design of.
@if (IWantToWriteNiceCode)
{
<ThisDoesntFloatMyBoat/>
}
This isn’t nice to look at, it’s quite verbose, and my non-programming colleague would undoubtedly break the logic when changing the contents to make them look good.
Describe the solution you’d like
I’d like to be able to have if and foreach directives, so my colleague only needs to know not to change any html attributes that start with an @ symbol (as is already the case).
<ThisDoesFloatMyBoat @if=IWantToWriteNiceCode/>
The preceding code is much nicer to look at, quicker to type, uses only a single line so uses less space, and easy to see what can be moved/altered without breaking things.
I would also like to be able to replace the following
@foreach(var person in People)
{
<li>@person.Name</li>
}
With something more simple such as
<li @foreach-person="People">@person.Name</li>
I resist giving judgement on a switch statement 😃
Additional context
Issue Analytics
- State:
- Created 8 months ago
- Reactions:86
- Comments:42 (12 by maintainers)

Top Related StackOverflow Question
The
@ifdirective attribute looks good to me. It has clear meaning and doesn’t introduce any new difficulty.I would recommend not getting bogged down in the
@foreachattribute though. That’s far weirder, declaring the name of a variable as a suffix of an attribute name. Something else like@foreach="person in People"is more obvious but it’s still a bit of a mess conceptually because you might also want to refer topersonin some other attribute on the element, breaking the syntactical hierarchy.I think
@ifis something we could pursue as an attribute in the short term, but am not sold on other flow constructions (while,for,foreach,switch,try/catch,using, etc) having new special-case alternate syntax on top of their existing C#-style syntax.@mrpmorris I understand the appeal of the solution you are proposing.
However, I do not agree with the reasoning to add something like this to Razor. You might prefer a more declarative style over a more imperative style, but that is to a large degree a matter of taste/preference.
Adding these types of constructs/primitives goes against the core philosophy or Razor, which is expressing the logic for rendering the UI directly in C#, without having to learn a new DSL. We already had this model with
aspxand we moved to something like Razor for a reason.I do not see the value of adding yet another way of doing the same thing to the language. I understand that it might appeal to some design preference or aesthetic sense, but I do not believe that there is a meaningful advantage over using the language vs having an alternative way based on a DSL.
On the contrary the drawback is that it adds two different ways of doing the same thing at a very low level.
This is something that I would say we are not likely to change, and if we do, I will think we would end up with a different language that is just not Razor anymore.
I agree with @SteveSandersonMS that we might potentially add something to conditionally render a block, but I would go as far as to say that it would not be called
@if.