We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Can i put JS-Render template in separate javascript file?

Hello,

I am using syncfusion angular grid with some template binding you can make your self more clear by refer below code snippet.

This code in javascript:

$scope.columns = [
                        { field: "name", headerText: resource.templateName, template: "true", templateID: "#nameUrlTemplate", width: 300 },
                        { field: "description", headerText: resource.description, template: "true", templateID: "#descriptionUrlTemplate", width: 400 }
          ];

This code in CSHTML:

<script type="text/x-jsrender" id="nameUrlTemplate">
    {{if name != ""}}
    <a rel='nofollow' href="@Url.Action("Edit", "Home")?agenttypeid={{:agentTypeId}}&templateversion={{:templateVersion}}&dataSourceType={{:dataSourceType}}">{{:name}}</a>
    {{/if}}
</script>
<script type="text/x-jsrender" id="descriptionUrlTemplate">
    {{if description != ""}}
    <a rel='nofollow' href="@Url.Action("Edit", "Home")?agenttypeid={{:agentTypeId}}&templateversion={{:templateVersion}}&dataSourceType={{:dataSourceType}}">{{:description}}</a>
    {{/if}}
</script>

this code is working very fine but i placed it in cshtml file. so problem is i have to follow some coding standard and because of that i have to put this code code in separate javascript file as per codding  standard.

so is there any way to put this code in separate javascript.

aspect quick reply.

Many thanks,
Rakesh.

1 Reply

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team September 4, 2015 10:36 AM UTC

Hi Rakesh,

Thanks for contacting Syncfusion support.
Your requirement is to include the Jsrender template in a separate js file. In your code example, there is a serverside code, which is a razor syntax.

<script type="text/x-jsrender" id="nameUrlTemplate">

        {{if name != ""}}

        <a rel='nofollow' href="@Url.Action(" edit", "Home" )?agenttypeid={{:agentTypeId}}&templateversion ={{:templateVersion}}&dataSourceType ={{:dataSourceType}}">{{:name}}</a>

        {{/if}}

    </script>


Normally, if any razor syntax is given in the view, the razor engine will parse it and build a respective view. The Razor parser itself is designed to process a stream of characters given in the View, to generate a C# or VB class which can be compiled.
The razor engine is able to parse only the razor syntax present in the view or any JS code embedded inline in the view. The Razor engine will not parse the external JS file referred in the page. Hence, place the template in the view if you want to use server codes inside the template string.
Regards,
Seeni Sakthi Kumar S.

Loader.
Live Chat Icon For mobile
Up arrow icon