CMS.DocumentEngine.DocumentContext.CurrentBodyClass

Mohamed ERRAIS asked on June 15, 2019 19:23

I have two pages home page and another page. I would like to apply a different style depending on the current page ?

Correct Answer

Mohamed ERRAIS answered on June 15, 2019 20:12

In the body section in the Master page I'am added :

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(CurrentDocument.DocumentName== "home")   
     CMS.DocumentEngine.DocumentContext.CurrentBodyClass += "body-home";
else
     CMS.DocumentEngine.DocumentContext.CurrentBodyClass += "body-page";
}
</script>
1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 17, 2019 21:59

Depending on where and when you load your code, it could cause an error and the page won't load at all. A better approach would be something like this with some error handling in it:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CurrentDocument != null)
        {
            if(CurrentDocument.DocumentName== "home")   
            {
                CMS.DocumentEngine.DocumentContext.CurrentBodyClass += " body-home";
            }
            else
            {
                CMS.DocumentEngine.DocumentContext.CurrentBodyClass += " body-page";
            }
        }
    }
</script> 

You might also want to add a field to the page type(s) which will allow the user to set the CSS class name from within the form data. Then you don't need to hard code what css class is being used, you simply use the setup like below:

CMS.DocumentEngine.DocumentContext.CurrentBodyClass += " " + CurrentDocument.GetStringValue("YourCssClassColumnName", "body-page");

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.