ltrzesniewski / RazorBlade

Compile Razor templates at build-time without a dependency on ASP.NET.
MIT License
149 stars 7 forks source link

Tag helpers don't work #8

Closed carlreinke closed 1 year ago

carlreinke commented 1 year ago

Tag helpers appear to not work despite using @addTagHelper.

There's a lot of tag helper discovery code in the Razor SDK source generator that doesn't seem to have an equivalent in RazorBlade's source generator.

ltrzesniewski commented 1 year ago

Tag helpers are currently not implemented in RazorBlade.

To be honest I'm not sure I want to add them, as this library is meant to be lightweight. It's supposed to generate simple HTML output, not fully-fledged pages which communicate with a server. I don't really want to reimplement ASP.NET. 🙂

Also, I expect the IDE support for them to be poor (it wouldn't recognize the special tags, it would just treat them as regular markup).

In some cases, you can get a similar result by composing your templates: @(new SomeOtherTemplate(AndItsArgument)) or by using any other object which implements IEncodedContent instead.

If this is insufficient, could you tell me what you're trying to do, so I can see how tag helpers would help?

carlreinke commented 1 year ago

I'm trying to generate URLs from routes (either using the built-in anchor tag helper or using a custom tag helper). Right now I have href="@(SomeUrlHelper.GetWhateverUrl(_context, someId))", but I think I'd rather have asp-route="whatever" asp-route-id="@someId" and not have to write URL a generator method for every route.

So I can certainly live without tag helpers, it just seemed like it might be convenient.

If tag helpers aren't going to be supported, it would be nice to produce an error from the generator if the template contains an @addTagHelper directive. The Razor compiler is pretty modular, but it looks like the @addTagHelper is built-in and can't be disabled. Maybe there's a way to examine the Razor syntax tree and produce a diagnostic on the directive syntax node or replace the IRazorTagHelperBinderPhase with one that produces a diagnostic?

ltrzesniewski commented 1 year ago

Ok, I see. I think I'll play with them in the future to see how it goes.

I didn't notice that @addTagHelper was currently accepted by the parser. Thanks for the info, I'll definitely add an error if I end up not adding tag helper support.

ltrzesniewski commented 1 year ago

I ended up adding an error when trying to use tag helpers.

I tried to add support for them in the tag-helpers branch, but realized it would go off the rails.

I wanted to integrate the Microsoft.AspNetCore.Razor.Runtime package (which implements tag helpers without depending on a lot of ASP.NET stuff) with RazorBlade: the idea was that you'd get tag helper support if you reference that package. Unfortunately, Microsoft deprecated it after I started implementing the feature. 🙄

So that's a dead end - it seems the tag helpers implementation will be shipped in the SDK, and not available as a nicely self-contained external NuGet package that I could use.

Also, integrating the Razor runtime package wouldn't be seamless, since it would import stuff that RazorBlade implements on its own, and you wouldn't necessarily know which feature should be used in a given context.

Maybe someday I could revisit this, but it's best to keep things simple for now I guess.

carlreinke commented 1 year ago

Thanks for trying to make it work anyway!