Aspnetcore: Conditional Class support as TagHelpers similar to bind-...

Created on 8 Apr 2018  路  2Comments  路  Source: dotnet/aspnetcore

Add support via a different syntax other than using ternary operators, as it gets rather verbose when you add multiple conditions.

The idea of this would be similar to how bind-... works, syntactic sugar which translate the syntax to something that a user himself could write.
<input bind="@value" />
compiled to
<input value="@value" onchange="@((__value) => value= __value)/>.

In this case
<div class="class1" class.classNameHere="@BooleanExprOrValue"></div>
would be compiled to
<div class="class1 @(BooleanExprOrValue ? "classNameHere" : string.Empty)"></div>.

The syntax could be decided/changed, though the core part should express what I mean.

area-blazor

Most helpful comment

At the moment our goal is to retain normal HTML syntax as Razor has always done rather than creating new subsyntaxes within HTML.

If you're finding the ternary operators to be cumbersome, I think it might be more interesting to advocate for this as a C# feature rather than a Razor feature. It could work the same as in JavaScript, and conceptually would be the inverse of a null-coalescing operator. For example,

someBoolValue && someValueOfTypeT

... could be treated as equivalent to:

someBoolValue ? someValueOfTypeT : default(T)

This is equivalent to the same JS expression in most use cases, and would be consistent with the existing handling for T being boolean.

I'll close this since non-HTML syntaxes is not something we're planning to add in the short term. Hope that's OK.

All 2 comments

Made a rough implementation based on how bind-... is implemented here: Conditional Class Implementation.

At the moment our goal is to retain normal HTML syntax as Razor has always done rather than creating new subsyntaxes within HTML.

If you're finding the ternary operators to be cumbersome, I think it might be more interesting to advocate for this as a C# feature rather than a Razor feature. It could work the same as in JavaScript, and conceptually would be the inverse of a null-coalescing operator. For example,

someBoolValue && someValueOfTypeT

... could be treated as equivalent to:

someBoolValue ? someValueOfTypeT : default(T)

This is equivalent to the same JS expression in most use cases, and would be consistent with the existing handling for T being boolean.

I'll close this since non-HTML syntaxes is not something we're planning to add in the short term. Hope that's OK.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snebjorn picture snebjorn  路  3Comments

aurokk picture aurokk  路  3Comments

BrennanConroy picture BrennanConroy  路  3Comments

farhadibehnam picture farhadibehnam  路  3Comments

FourLeafClover picture FourLeafClover  路  3Comments