Equivalent to Sitecore Razor Assistant

I am trying to include various scripts on different pages in Sitecore and cannot find a very good way to do this. In a regular mvc project, I could add the @Section {} helper and use it on different partial views to indicate exactly where these scripts will be displayed in the layout, but I could not find an equivalent for how the Razor helper is implemented with Sitecore. I would like to do this without using a place holder, I don't want to add a view to Sitecore every time I need to add a script file.

Thanks in advance.

+4
source share
3 answers

Sitecore MVC, RenderScripts() body.

@Html.RenderScripts()

:

public static IHtmlString RenderScripts(this HtmlHelper htmlHelper)
    {
       var templates = (from object key in htmlHelper.ViewContext.HttpContext.Items.Keys 
                        where key.ToString().StartsWith("_script_") 
                        select htmlHelper.ViewContext.HttpContext.Items[key]).OfType<Func<object, HelperResult>>()
                            .Select(template => template(null)).ToList();
        foreach (var template in templates)
        {
            htmlHelper.ViewContext.Writer.Write(template);
        }
        return MvcHtmlString.Empty;
    }

MVC Razor, .js, , - :

        @Html.Script(
        @<script src="@Url.Content("~/js/custom/orderdetail.js?t=11172015")" type="text/javascript"></script>
    )

Script:

        public static MvcHtmlString Script(this HtmlHelper htmlHelper, Func<object, HelperResult> template)
    {
        htmlHelper.ViewContext.HttpContext.Items["_script_" + Guid.NewGuid()] = template;
        return MvcHtmlString.Empty;
    }

, , , .

+2

, .
@Section , Sitecore Razor , MVC.
Sitecore MVC - , .

, <body> , <head> .
Sitecore MVC , , @Section.

, , (, </body>), , .

+5

, Sitecore. MVC4, Bundles . . http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification . Bundle script , @Scripts.Render()

+1
source

Source: https://habr.com/ru/post/1530997/


All Articles