Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 578 forks source link

RenderSection is failing when there's HTML in the section #193

Closed joelmartinez closed 9 years ago

joelmartinez commented 9 years ago

In the following example, the first section renders as expected.

        Razor.Compile ("@RenderBody() @RenderSection(\"Scripts\")", "layout-working");
        Razor.Compile ("@{ Layout = \"layout-working\"; } body @section Scripts {" +
            "a script" +
            "}", "page-working");
        var workingresult = Razor.Run ("page-working");

        Razor.Compile ("@RenderBody() @RenderSection(\"Scripts\")", "layout");
        Razor.Compile ("@{ Layout = \"layout\"; } body @section Scripts {" +
            "<script />" +
            "}", "page");
        var result = Razor.Run ("page");

While the second example fails with the following exception:

image

matthid commented 9 years ago

This seems to be a duplicate of https://github.com/Antaris/RazorEngine/issues/93 or at least related. Can you please reopen if this is not the case.

joelmartinez commented 9 years ago

I can definitely see where that's similar, but in this case the first template (page-working) successfully parses and runs (which I guess means that #93 is resolved in 3.4.2?). In this case the second template, page, is the one that fails with that error, and it seems to only happen if there's html in there (as in, a script tag).

I also tried to use the Raw function to output the tag as a string, but got the same error ... not to mention I tried putting the closing brace on a new line to see if that would at least give me a workaround, but it still gave me the same error as well.

I'd re-open this, but github doesn't give me the option, I guess cause I'm not a collaborator :)

matthid commented 9 years ago

You are right, I just was looking at this at the moment and it seems to be either a bug in Microsoft.AspNet.Razor or by design and I found no quick workaround. Is similar code working in MVC templates?

joelmartinez commented 9 years ago

I don't have a computer in front of at the moment, unfortunately. But I have definitely had a scripts section in previous mvc projects ... It's a major use case for razor sections so individual pages can add/customize the scripts on a page and have them at the end of the HTML/body.

matthid commented 9 years ago

I investigated this a bit more and it is in fact a bug in Microsoft.AspNet.Razor (please file a bug on them!) and ONLY happens with the EMPTY script tag. So the workaround is to use <script><script/> instead of <script />. Nice find though. Please comment when this workaround is not working for you.

coxp commented 9 years ago

Just to point out that I don't think self-closing script tags are valid HTML, and Razor is strict about the HTML that it will accept.

matthid commented 9 years ago

Wow, thanks for this. I read about the empty script tag a bit more and it blew my mind :). You should really stay away from it. However this still should be reported to Razor because at least the error message is not helping at all (and if you want to generate real XHTML this should work).

joelmartinez commented 9 years ago

Wow ... I guess I'd always just had non-self closing script tags in my sections and just never really thought about it. Thanks for the clarification!