adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
565 stars 84 forks source link

when empty value prop on html tag,razor doc parse error #130

Closed mirrortom closed 1 year ago

mirrortom commented 1 year ago

`

`

when the prop

value=""

then razor parse doc is

nullable disable

WriteLiteral("\"");
BeginWriteAttribute("value", " value=\"", 252, "", 468, 5); WriteAttributeValue("", 260, ">

", 260, 7, true); ......

Why are HTML tags parsed into C# code?

adoconnection commented 1 year ago

Why are HTML tags parsed into C# code?

because razor is compiled template engine, thats why its really fast

this code works for me, I dont see any issues:

class Program
{
    static string Content = @"<input name=""firstname"" type=""text"" value=""""/><div>@Model.Name</div>";

    static void Main(string[] args)
    {
        IRazorEngine razorEngine = new RazorEngine();
        IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);

        string result = template.Run(new
        {
            Name = "Alexander"
        });

        Console.WriteLine(result);
        Console.ReadKey();
    }
}
mirrortom commented 1 year ago

Thanks for you reply! Author, Alexander.

I tried to write 4 commas :

<input name="firstname" type="text" value=""""/>

but same situation(source is a (.cshtml) file)

...
BeginWriteAttribute("value", " value=\"", 252, "\"", 468, 5);
...

I tested other project similar to yours, the RazorEngine and RazorLight,and didn't find this situation

I think the class RazorProjectEngine created, Some features for razor parsing are missing. I tried some features add to RazorProjectEngineBuilder,but all faild, I didn't find that one that worked.

(RazorProjectEngineBuilder builder)
{
}
mirrortom commented 1 year ago

If prop value is not empty, then the compilation is correct

// source
<input name="firstname" type="text" value="AAA" />

// Generated
WriteLiteral("\r\n <input name=\"firstname\" type=\"text\" value=\"AAA\" /> \r\n");
adoconnection commented 1 year ago

may I ask you to provide sample console app code to reproduce this problem?

mirrortom commented 1 year ago

em...,I am sorry. I recloned RazorEngineCore and tested the issue in the new console program, It was no problem! I should double-check my project. Thank you very much!


at last I found the problem, the TemplateBase.cs file was missing the BeginWriteAttribute() and EndWriteAttribute() methods