question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Applying Different Layouts to Different Sites

See original GitHub issue

TL;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:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tidyuicommented, Apr 19, 2021

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 from WebApp.Site.Id. There’s also a helper method to load (if available) the current global site content by calling WebApp.Site.GetContent<T>() where T 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 on ASP.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 a SelectListField<T> where T 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:

public enum ThemeTypes
{
    Default,
    Fancy
}
0reactions
josip8commented, Nov 9, 2021

For anyone wondering (just to reiterate what tidyui said), I created new site type like this:

[SiteType(Title = "Test Site")]
public class TestSite : SiteContent<TestSite>
{
    [Region]
    public SelectField<ThemeStyles> ThemeStyle { get; set; }
}


public enum ThemeStyles
{
    [Display(Description = "Theme 1 - Blue")]
    ThemeBlue,
    [Display(Description = "Theme 2 - Red")]
    ThemeRed
}

And than in _Layout.cshtml you can access this site property like this:

@{
  var theme = (await WebApp.Site.GetContentAsync<TestSite>())?.ThemeStyle;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Apply or change a slide layout
Select the slide that you want to change the layout for. · Select Home > Layout. · Select the layout that you want....
Read more >
How To Apply A Different Layout To A Slide in Microsoft ...
Another quick way to change the Layout of the Slide that is selected, is to do a right click on the Slide with...
Read more >
Liquid and alternate layouts in InDesign
You can apply different rules to different pages, depending on the layout and the goals; only one liquid page rule can be applied...
Read more >
Google Slides: Editing Master Slides and Layouts
Create unique slide layouts: If you want to create a presentation that looks different from default Google Slides themes, you could use the...
Read more >
How to Change Layouts in PowerPoint - Tutorial
Right-click the slide whose layout you want to change → Layout. Then choose the desired option from the list.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found