V9 Razor TagHelpers - Can we make your Razor HTML markup easier? #10891
Replies: 9 comments 19 replies
-
NoteCan I ask if you upvote this to please let me know who you are so I can reach out and discuss with you your thoughts on this. |
Beta Was this translation helpful? Give feedback.
-
I think the GetBlockListHtml and GetGridHtml should be tag helpers just to follow the flow of this kinda thing in core in general |
Beta Was this translation helpful? Give feedback.
-
I like these. It would be great to have them available in Core to make our lives easier and reduce the need to write them ourselves or depend on external package for them |
Beta Was this translation helpful? Give feedback.
-
Another useful one... To be able to have a @section type of thing for partials... So for example be able to load scripts included in block list items in the correct place within the master / layout, I'm not sure if that would be possible with a tag helper however. It's annoying to have to add this as a extension everytime... |
Beta Was this translation helpful? Give feedback.
-
I'm in the middle of writing my own svg-inliner. Mine needs to support both media file system and local file system. I'll dig through some older projects to see patterns in views that could be better as TagHelpers. Your |
Beta Was this translation helpful? Give feedback.
-
Nice idea Warren. I think doing something similar with an img and defining the crop would be useful, provide some configurable options (with some useful defaults perhaps). Tie it in with the new media picker.
|
Beta Was this translation helpful? Give feedback.
-
As raised on Twitter. These do make for a cleaner razor view file, but I do have a slight concern about dom elements / attributes being "server side" especially when working in teams with front end and back end devs. It's not uncommon in front end frameworks to create custom dom elements, or custom attributes. But mixing these with tag helpers could become really pretty confusing. "Is this thing a server side helper, or a client side component"? Compounded with this is the usage of additional attributes to provide data to the server side component. For example the How much of a problem this will be, I don't really know. It's just at least by using C# in your razor file, you explicitly know if it's server side vs client side, where as with these helpers, it just feels like it's going to become a whole load murkier. |
Beta Was this translation helpful? Give feedback.
-
I do like the idea, but... one small problem with "umb-if", I could see some potential issue with this in the long run. Wouldn't the tag helper directive name collide with a front-end framework directive? What if there is a yet another new frontend javascript (or even blazor WASM) framework package called Umbo, and it too has directive called Also, considering about code clarity, if we do combine this with JavaScript framework like AngularJS, KnockoutJS, VueJs, WebComponents, etc that have their own directive, I could see this confusing our readers that would be digesting our codes in future: <a umb-if="Model.ContentPickerThing != null" ng-if="!isClicked" href="@Model.ContentPickerThing?.Url()" class="btn btn-action"> Now we have two directive with "...-if". I would imagine that readers digesting this piece of code would then ask themselves this question, "is umb-if part of javascript framework or is this part of razor taghelper- how do I know which one?" - they may have harder time differentiating server-side view logic apart from client-side logic. To me, I would know umb-if is doing server-side rendering, and ng-if (angularjs's equivalent) is doing client-side view rendering, but how many developers would actually know this? For this reason, I think I'd rather favor using |
Beta Was this translation helpful? Give feedback.
-
I'll say right away that I didn't know what "tag helpers" was, and that I didn't get that they were the point of the discussion when I replied to the original tweet from Warren. I am very happy that you're opening this for discussion Warren, and I'm sure there'll be lots of opinions - so here are my 2 cents: For some of the same reasons as raised by @mattbrailsford and @rjsmith2 above, I am not a fan of mixing frontend and backend like this (especially don't recommend replacing an Rendering media has always been problematic (check the forums) and the very first thing we get questions about when helping someone implement an Umbraco site; they look up how to render media, and there's various (too many) ways to get "a URL" for an image, and every forum answer has yet another version of doing it. If this could be reduced to something akin to |
Beta Was this translation helpful? Give feedback.
-
Suggestion 💡
My suggestion is to ship Umbraco V9 with some C# TagHelpers to help make the life's easier for Developers implementing HTML markup in Razor .cshtml views.
What are TagHelpers ❓
More Resources
Microsoft Docs: Intro to TagHelpers
Microsoft Docs: Comparing TagHelpers to HTML Helpers
Microsoft Docs: Authoring TagHelpers
Examples 👀
umb-if
One example is to have a new taghelper attribute
umb-if
that can be applied to any HTML dom elementAnother use case of umb-if for easier to read IMO than nested if Razor code blocks
You can see with this screenshot Visual Studio tooling highlights and give IntelliSense for the custom tag helpers and it is thus easy to see this is not a normal HTML attribute but something custom that will run against some C# code
IfTagHelper.cs
umb-inlinesvg
This taghelper is a bool attribute where its presence on a SVG or IMG html element with a src attribute set from a SVG file in the media library, it will read the file contents (no matter where the media stored, local, Azure blob, AWS etc) and sanitize the SVG for any script tags and then add the SVG file contents directly to the DOM to make the SVG inlined.
InlineSvgTagHelper.cs
Question to community
Can you think of any examples of scenarios that can be cumbersome or tricky with Razor currently and are there any potential taghelpers that we could ship with to help ease any pain.
Beta Was this translation helpful? Give feedback.
All reactions