@using Microsoft.AspNetCore.Mvc.TagHelpers @model (Dictionary Items, int Level) @functions { private bool IsValidURL(string source) { return Uri.TryCreate(source, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); } } @if (Model.Items.Any()) { @* Use titlecase and lowercase versions for backwards-compatibility *@ string[] cartKeys = ["cart", "subtotal", "discount", "tip", "total"]; @if (Model.Items.Keys.Any(key => cartKeys.Contains(key.ToLowerInvariant()))) { _ = Model.Items.TryGetValue("cart", out var cart) || Model.Items.TryGetValue("Cart", out cart); var hasTotal = Model.Items.TryGetValue("total", out var total) || Model.Items.TryGetValue("Total", out total); var hasSubtotal = Model.Items.TryGetValue("subtotal", out var subtotal) || Model.Items.TryGetValue("subTotal", out subtotal) || Model.Items.TryGetValue("Subtotal", out subtotal); var hasDiscount = Model.Items.TryGetValue("discount", out var discount) || Model.Items.TryGetValue("Discount", out discount); var hasTip = Model.Items.TryGetValue("tip", out var tip) || Model.Items.TryGetValue("Tip", out tip); if (cart is Dictionary { Keys.Count: > 0 } cartDict) { @foreach (var (key, value) in cartDict) { } } else if (cart is ICollection { Count: > 0 } cartCollection) { @foreach (var value in cartCollection) { } } @if (hasSubtotal && (hasDiscount || hasTip)) { } @if (hasDiscount) { } @if (hasTip) { } @if (hasTotal) { } } else { foreach (var (key, value) in Model.Items) { @if (value is string str) { if (!string.IsNullOrEmpty(key)) { } } else if (value is Dictionary { Count: > 0 } subItems) { } else if (value is IEnumerable valueArray) { } } }
@key @value
@value
Subtotal @subtotal
Discount @discount
Tip @tip
Total @total
@key@* Explicitely remove whitespace at front here *@@if (IsValidURL(str)){@str}else {@str.Trim()} @{ @if (!string.IsNullOrEmpty(key)) { Write(Html.Raw($"")); Write(key); Write(Html.Raw($"")); } } @{ @if (!string.IsNullOrEmpty(key)) { Write(Html.Raw($"")); Write(key); Write(Html.Raw($"")); } } @foreach (var item in valueArray) { @if (item is Dictionary { Count: > 0 } subItems2) { } else { } }
}