This project is read-only.

Include stylesheet only on homepage - easiest way

Topics: Administration, Customizing Orchard, General, Writing themes
May 17, 2011 at 4:23 PM

My homepage stylesheet overrides some header styling, which is why I want to include the Homepage stylesheet only on the homepage of the site.

Currently I'm including it from Document.cshtml (under Views of my custom theme) as:


 Is there any way to do something like this on this same page:

if  homepage { Style.Include("Homepage.css"); }

Initially I was including this stylesheet from the homepage Content entry on the Admin panel as:

<link href="/Themes/MyTheme/Styles/Homepage.css" rel="stylesheet" type="text/css" />

But this was throwing a 404 File not found (Fiddler).  And I prefer to avoid on page styling.

Any tips / ideas welcome and appreciated!

May 17, 2011 at 5:08 PM

You could use url alternates to provide a different template, or create a filter to inject a property on the layout shape that you can then interrogate in your view code;

public class HomepageLayoutFilter : FilterProvider, IResultFilter
        private readonly IWorkContextAccessor _wca;

        public HomepageLayoutFilter(IWorkContextAccessor wca)
            _wca = wca;

        public void OnResultExecuting(ResultExecutingContext filterContext)
            var workContext = _wca.GetContext();
            var routeValues = filterContext.RouteData.Values;

            if (((string)routeValues["area"]) == "HomePage")
                workContext.Layout.IsHomepage = true;

        public void OnResultExecuted(ResultExecutedContext filterContext)

With that in place, if you can interrogate WorkContext.Layout.IsHomepage eg:

if( WorkContext.Layout.IsHomepage == true )

Jun 13, 2011 at 10:59 PM

Thanks for your reply!

Where would I put the above class you're suggesting?  Does that go somewhere in my Theme files as a Model or Controller or how exactly does it play?  Sorry, pretty new to .Net and MVC

Jun 13, 2011 at 11:02 PM

You could put it in your theme, provided you have a csproj file for that theme (which you can get by generating the theme from the command-line). If all this is not understandable, I can break it down :)

Jun 13, 2011 at 11:04 PM

Yes I do have a csproj file and a theme, but where in my theme would I put the above code?  would I need to create a new file?  thanks :)

Jun 13, 2011 at 11:09 PM

Yes, just create a new file, and make sure it's added to the project.

Jun 13, 2011 at 11:23 PM

So I added <Content Include="Controller\HomepageController.cs" /> to my .csproj file within my theme and also included the if statement above in my theme's Document.cshtml file (where I want the conditional content to show up), but I'm not getting anything. I'm sure I must be doing smth wrong ...

Jun 13, 2011 at 11:29 PM
Edited Jun 13, 2011 at 11:29 PM

And I'm getting a bunch of error messages when I rebuild, like "Error 15  The type or namespace name 'FilterProvider' could not be found (are you missing a using directive or an assembly reference?)"

So I know I'm definitely doing smth wrong :)