Kamilius
Kamilius

Reputation: 587

Umbraco inline razor @section in head tag

I'm trying to create razor @section in one of my views, using umbraco:Macro tag, in order to add scripts/styles specific for each view into head tag. My current code looks like this(head section of master page):

<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" type="text/css" href="/css/foundation.min.css">
    <script type="text/javascript" src="/js/jquery-1.10.1.min.js"></script>
    <script type="text/javascript" src="/js/responsiveslides.js"></script>
    <script type="text/javascript" src="/js/js.js"></script>

<umbraco:Macro runat="server" language="cshtml">
    @if(IsSectionDefined("JSIncludes"))
    {
        @RenderSection("JSIncludes")
    }
    @if(IsSectionDefined("CSSIncludes"))
    {
        @RenderSection("CSSIncludes")
    }
</umbraco:Macro>
</head>

and this(actual section in view):

<asp:Content ContentPlaceHolderId="ContentPlaceHolderDefault" runat="server">
<umbraco:Macro runat="server" language="cshtml">
    @section JSIncludes
    {
        <script type="text/javascript" src="/js/main_page.js"></script>
    }
</umbraco:Macro>

But when i'm trying to open a page, i'm getting "Error loading MacroEngine script (file: )" error on top of my page. Do anyone know the reason of this? Would be great, if somebody knew how to add things like this properly. Thanks in advance.

Upvotes: 0

Views: 1491

Answers (1)

Electric Sheep
Electric Sheep

Reputation: 4330

You don't need to use a razor macro to add page-specific script references, use a ContentPlaceHolder

Master page:

<head>   
  <asp:ContentPlaceHolder ID="PageScripts" runat="server" />
  <asp:ContentPlaceHolder ID="PageStyles" runat="server" />

Content page:

<asp:Content ID="PageScript" runat="server" ContentPlaceHolderID="PageScripts">
    <script type="text/javascript" src="/js/main_page.js"></script>
</asp:Content>

Reference

Upvotes: 1

Related Questions