question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Blazor] @if directive

See original GitHub issue

Is 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

https://i.ibb.co/8gsnjg7/Fm68-Ngz-Xw-AA8xqv.png

Issue Analytics

  • State:open
  • Created 8 months ago
  • Reactions:86
  • Comments:42 (12 by maintainers)

github_iconTop GitHub Comments

10reactions
SteveSandersonMScommented, Jan 20, 2023

The @if directive 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 @foreach attribute 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 to person in some other attribute on the element, breaking the syntactical hierarchy.

I think @if is 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.

9reactions
javiercncommented, Jan 20, 2023

@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 aspx and 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor if, else and else if statement
if, else and else if are conditional statements that are used to execute different block of codes depending on different conditions. In the...
Read more >
asp.net core - Blazor conditional if statement for onclick
Blazor conditional if statement for onclick ... I have a span which should have an onclick attribute if the IsActive bool is true....
Read more >
Directives
Directives are built-in macros that alter the transpiled C# code that is generated from Razor mark-up. Directives are used by preceding the identifier...
Read more >
How do I conditionally add Blazor components' template?
The if statement is used to display a component or fragment conditionally. If the condition evaluates to true, the component will be loaded...
Read more >
Conditional Blazor Styles (without the if statements)
If you're building Blazor components you're probably used to driving the UI from data. At its simplest this might be to show a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found