toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.53k stars 259 forks source link

TagHelpers are not supported #21

Open DMW007 opened 8 years ago

DMW007 commented 8 years ago

Hi,

I would like to use your library for email-templates, which contains links to the application itself. Sadly, RazorLight seems to parse razor code, but not the ASP.NET Core TagHelpers. For example, the asp-controller and asp-action attribute to generate an action-controller link like this:

<a asp-controller="Account" asp-action="Login">Test</a>

Using dependency injection, a instance of IRazorLightEngineis injected in my constructor. There I call the Parsemethod like this:

var html = razorEngineLight.Parse("Shared/Test.cshtml", new object());

But in the rendered html, I don't get a parsed link like

<a href="/Account/Login">Test</a>

as expected. Instead, I get 1:1 the source code with asp-controller and action attribute like showed above. So it seems that the _ViewImports.cshtml file in the View-folder is not globally parsed like ASP.NET Core do it. I tried to add the tag helpers manually using

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

in the Razor-View, but its not working. As a workaround, I tried to use the old UrlHelpers from ASP.NET like this way:

@{
    var link = Url.Action("Login", "Account", null);
}

<a href="@link">Testlink</a>

This is valid code, as it works in a normal Razor-View. But using RazorLight, I got a RazorLight.TemplateCompilationException: Failed to compile generated razor view. See CompilationErrors for detailed information. In the details, Visual Studio show me that there is one CompilationError (Cound = 1) but there seems no way to extend or otherwise view them.

As a result, it seems not possible to create action-controller links using Razor, which is a problem in my point of view. Would be pleased if this could be fixed.

toddams commented 8 years ago

Hi there

As mentioned here #16 - taghelpers and UrlHelpers are not implemented. I am going to roll up a stable version soon, and more likely it will not be implemented there, as we are on Release Candidate now. I will add this to backlog, but expect this on the next major release.

In your case you can write pure HTML for building links for now, it's not critical, but yes, a lot of people were asking where are tag and url helpers, so it has to be added.

Thanks, stay tuned

josh-sachs commented 7 years ago

How would we go about using Html.Raw()? I believe this is impacted.

toddams commented 7 years ago

@kudoz83 You can use @Raw in order to workaround Html.Raw(), which basically the same function but outside of HtmlHelper implemented as Razor page function

josh-sachs commented 7 years ago

What do I need to import to get that method

toddams commented 7 years ago

@kudoz83 Nothing :) It's already there, give it a try

josh-sachs commented 7 years ago

Sorry if I'm just dense, but I I don't have @Raw as an option in my cshtml file. I assume I need a using or inject?

toddams commented 7 years ago

@kudoz83 The thing is that your Visual Studio has Intelisense that uses built-in Razor engine tooling, and it has no @Raw method. Visual Studio knows nothing about RazorLight and therefore - nothing about it's functions. So yea, you might not get appropriate hints from IDE, that's the thing, but just try to write @Raw and use RazorLight to compile your view. It should work. Let me know if it didn't :)

josh-sachs commented 7 years ago

Ok it works. Sorry for the confusion.

josh-sachs commented 7 years ago

Doesn't seem you can actually build a project when the views contain @Raw() - is there a workaround?

toddams commented 7 years ago

Can you show what error you are getting? That would help a lot

josh-sachs commented 7 years ago

The name Raw does not exist in the current context.

I understand that RazorLight will compile the template, but visual studio checks the CSHTML on build and shits itself.

toddams commented 7 years ago

I guess changing extension from *.cshtml to something might solve the problem

josh-sachs commented 7 years ago

In a netcore project:

"buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true,
        "compile": {
            "exclude": [
                "**/*.cshtml",
                "node_modules"
            ]
        }
    },

This will allow you to compile/publish but will not remove the errors from the output window. Ideally Razorlight should expose all of it's helpers for import into views.

josh-sachs commented 7 years ago

I found a better workaround.

@{    var rawString = new RazorLight.Text.RawString(content);   }
...
Here I want to print @(rawString) without any compile issues.

I think it would make sense to create a RazorLight static that can be injected with these helper functions, rather than just say "oh well."

toddams commented 7 years ago

Not quite sure what do you mean on "just say 'oh well'", as there is an existing solution to write Raw string, and I tried my best to explain it to you.

If you have better solution - I'm open for pull requests, and I will do appreciate your contribution

kurtgronbech commented 7 years ago

Any news about support for taghelpers and UrlHelpers? Missing support is a showstopper for me.

joetherod commented 7 years ago

ditto: Any news about support for taghelpers and UrlHelpers? Missing support is a showstopper for me. And HtmlHelpers?

toddams commented 7 years ago

This will be implemented as a part of RazorLight 2.0 that will start it's development at the moment .NET Core 2.0 will be released

dukecheng commented 7 years ago

Hi @toddams Any update for this feature?

RobSchoenaker commented 7 years ago

Looking forward to see @Html.Partial support too. There is just no way to do this @ the moment...

toddams commented 7 years ago

Are u sure? https://github.com/toddams/RazorLight/wiki/Include-(aka-Partial)

RobSchoenaker commented 7 years ago

Include != Html.Partial. I would like to see support for all Html.* calls, just like they work in 'normal' views. The reason being that I do not want to modify an existing project - over 1500 views... Any idea on how to achieve this, is more than welcome!

toddams commented 7 years ago

Can u tell the the difference between Html.Partial and Include, except name? Maybe I'm missing the point

RobSchoenaker commented 7 years ago

The difference is syntax, so you are not missing any point. It may be functionally identical, but I need the Html helpers to work... If there is a way to get the normal Html.* helpers working in RazorLight, this would open some other possibilities as well, like minifying the views before they are compiled. That is what I am looking for.

RobSchoenaker commented 7 years ago
alextof commented 6 years ago

Which is the supported tags overview right now?

gavrilyuc commented 6 years ago

Which is the supported tags overview right now?

don't work.

martonx commented 5 years ago

Badly missing functionality. I have to duplicate lots of views only because of this :( Anyway, big plus to the contributors for their good work on this promising package!

jjxtra commented 5 years ago

Still missing for years. I realize this is a free and open source project, but if anyone can give guidance on how to add html helpers to this project so that views work as they do in a vs project I'd be happy to do the work.

MohammadMokhtarii commented 4 years ago

any news on adding this feature?? tag helper is base feature of razor views in asp.net core this is very useful

rburnham52 commented 4 years ago

I was hoping to use these tag helpers for our emails so we don't need to write html like its the 90's. https://github.com/XmlmXmlmX/InkyTagHelpers

jzabroski commented 3 years ago

I think we should close this ticket and open a new one with a clear list of what needs to be done.

Off the top of my head, the core features are managing scope of the TagHelper, documented here:

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-5.0#managing-tag-helper-scope

As you can see from this list, some of these features are fairly complicated. The main concern would be making sure IntelliSense also still works.