Using alternatives for Document.cshtml in Orchard CMS

I am currently working on a website that requires overwriting the document.cshtml file so that I can apply a specific CSS class based on the user's current location.

I have tried using URL alternatives like:

  • Document.cshtml
  • Document-url-AreaA.cshtml
  • Document-URL-AreaB.cshtml
  • document.URL-AreaC.cshtml

however, it looks like they all use Document.cshtml rather than use one based on the URL. I could easily understand that this is the intended goal, however I was wondering if the functionality above could be implemented.


Update

I believe I may have made some progress in this area, as opposed to using alternate urls, and just adding a field on Models (for the document) to just pull the site's current "Area" and apply that class to the body.

(Inside document.cshtml)

@using Orchard.Mvc.Html;
@using Orchard.UI.Resources;
@{
    RegisterLink(new LinkEntry {Type = "image/x-icon", ...});

    string title = Convert.ToString(Model.Title);
    string siteName = Convert.ToString(WorkContext.CurrentSite.SiteName);

     //Pull the Area here
    string area = Model.DesignatedAreaField;
}
<!DOCTYPE html> 
<html lang="en" class="static @Html.ClassForPage()"> 
<head> 
    <meta charset="utf-8" />
    <title>@Html.Title(title, siteName)</title> 
    @Display(Model.Head)
</head> 
<body class='@area'>
//Body goes here
@Display(Model.Body)
@Display(Model.Tail)
</body>
</html>

      

I believe this may be an easier solution than the previous one. However, I am wondering what an easy way to place the field that I could access from the Document Model would be.

+3


source to share


3 answers


Document.cshtml is a wrapper for the layout form. Packers do not support alternatives. The only way to selectively replace the document.cshtml template is to remove the existing wrapper from the wrapper collection in the layout form metadata and add your own.



But wait ... I can't think of why you want to do this. What's in the document.cshtml file is an HTML template that should be the same throughout the site. The solution as described in your update is the way to go.

+7


source


You can also move the tag <body>

to Layout.cshtml and then use Layout.cshtml URL alternatives to set the class attribute <body>'s

.



+1


source


You can check out this post: http://weblogs.asp.net/bleroy/archive/2010/12/14/switching-the-layout-in-orchard-cms.aspx . It shows how you can dynamically add your own alternatives, but the same approach can work for adding properties to a model or layout based on geolocation.

I think you can define some code inside OnResultExecuting () to dynamically add a property to the Model class. Since it is a dynamic type, you should just set it, nothing special is needed.

0


source







All Articles