Applying Different Layouts to Different Sites
See original GitHub issueTL;DR: I wanted to figure out how I can switch the layouts for different websites.
EDIT: I figured out a bit of a workaround using Modules - see my comment reply below.
I have been unsuccessful in trying to switch the Layout based upon a SiteType
. I was trying to handle this in the _ViewStart.cshtml
file.
I tried something like:
string CurrentSite = Convert.ToString(WebApp.Api.SiteTypes);
switch (CurrentSite)
{
case "Standard Site": // also tried "StandardSite"
Layout = "~/Pages/Shared/_Layout2.cshtml";
break;
default:
Layout = "~/Pages/Shared/_Layout.cshtml";
break;
}
but CurrentSite
is null
. I presume it’s just not picked up at that point; otherwise I’m totally off-base.
I am using the RazorWeb
project from the Piranha source code. The Site Type is defined there like this:
[SiteType(Title = "Standard Site")]
public class StandardSite : SiteContent<StandardSite>
{
[Region]
public HtmlField Footer { get; set; }
}
Any thoughts? I was thinking of using a different Site Type for Website1 and Website 2, so that I could use different layouts and sort of mimic having a different theme for each, respectively. I might be going about this totally wrong. I am wondering if I could accomplish the same trying to switch based upon the URL. But as this is on localhost, I’m not using a www.website1.com address. Again, I could be going about this totally wrong.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
Hi there! A couple of things.
WebApp.Api.SiteTypes
is the API-service that will access the meta-data for the available registered Site Content Types, it has nothing to do with the actually selected type on the current site. When executing a page or post your can get the id of the current site fromWebApp.Site.Id
. There’s also a helper method to load (if available) the current global site content by callingWebApp.Site.GetContent<T>()
whereT
is the SiteType.You can absolutely use different subfolders for different pages where each subfolder has its own
_ViewStart.cshtml
file that specifies the Layout that should be used. This solution depends fully onASP.NET
and has nothing to do with Piranha. The downside with it is of course that the same Page can’t be used with different layouts.My suggestion would be to create a
SiteType
which has a region containing aSelectListField<T>
whereT
should be the type of a custom enum specifying the available themes. Then you would select the theme on site level, and you could load it in the Page and set the Layout, for example:For anyone wondering (just to reiterate what tidyui said), I created new site type like this:
And than in
_Layout.cshtml
you can access this site property like this: