Developer forum

Forum » Templates » RE: new content type pages with Razor issue

RE: new content type pages with Razor issue

Davor Zlotrg
Reply

Hey dynamicweb,

I upgraded to version  8.4.1.6  because I want to be able to output javascript from razor template but I have an issue. 

 

I created a new page and set the content-type to be text/javascript.

The template looks like this:

<!--@If(1 = 2)-->
<div id="content-main" class="dwcontent" title="Main content" data-settings="template:moduleonly.cshtml">

</div>
<!--@EndIf-->


@{
    var props = new List<string> { "1", "2" };

}


$(document).ready(function() {
    var props = [
        @foreach (var prop in props)
        {

        }
    ];
    alert("OK");
});

 

The problem is that when this template is rendered, for some reason it appends </string> at the end of the file.

$(document).ready(function() {
    var props = [
    ];
    alert("OK");
});

</string>

 

When I don't have a foreach section it is working. How can I fix this?


Replies

 
Mikkel Ricky
Reply

The Dynamicweb layout parser gets confused by <string> in the line

var props = new List<string> { "1", "2" };

and thinks it's an unclosed html element (actually it's Html Agility Pack that we use to parse the template that gets confused).

As a workaround you can add spaces around the type inside <>, i.e. write

var props = new List< string > { "1", "2" };

In Dynamicweb 8.5 the layout parser will be fixed to handle this automatically.

Best regards,
Mikkel

 

You must be logged in to post in the forum