Orchard CMS: Conditional Level CSS Class

I am trying to add a css class if I am in a specific layer.

So, 2 questions:

  • Is it possible to identify the current layer in the Razor view. Something like:

    if (currentLayer == "TheHomepage") {...}

  • Is HTML approaching the layer right, or is there a better way to do this in Orchard?

0
source share
3 answers

If you need to see which layers are currently active, you can do something like this:

@using Orchard.Widgets.Services @{ var widgetsService = WorkContext.Resolve<IWidgetsService>(); var ruleManager = WorkContext.Resolve<IRuleManager>(); var activeLayerNames = new List<string>(); foreach (var layer in widgetsService.GetLayers()) { try { if (ruleManager.Matches(layer.LayerRule)) { activeLayers.Add(layer.Name); } } catch(Exception ex) { // Problem occurred during layer rule evaluation. // Just treat it as though the layer rule did not match. } } if (activeLayerNames.Contains("TheHomePage")) { /* ... Your code here ... */ } } 

Much of the code above makes sense in the driver or controller, but if you work only at the presentation level, you can do it this way.

+2
source

You can create a widget that includes the necessary @{Style.Include} , and then add it to the layer.

Follow these instructions to create a new widget using razor code: Create simple custom Orchard widgets , name the new widget CssIncluder

Then add this view to your theme, you can use the form trace tool if you want:

Widget-CssIncluder.cshtml:

 @{ Model.Metadata.Wrappers.Clear(); Style.Include("somestyle.css"); } 

Finally, add the widget to the layer of your choice. Be sure to uncheck the header rendering option to get clean code.

0
source

Based on Katsuyuki's answer, I created an extension method for WorkContext to convert all active layers to css classes.

 using Orchard; using Orchard.Widgets.Services; using System.Collections.Generic; namespace KidsMinistryTeam.Theme.Extensions { static public class WorkContextExtensions { static public IList<string> GetLayerCssClasses(this WorkContext workContext) { var widgetsService = workContext.Resolve<IWidgetsService>(); var ruleManager = workContext.Resolve<IRuleManager>(); var classNames = new List<string>(); foreach (var layer in widgetsService.GetLayers()) { try { if (ruleManager.Matches(layer.LayerRule)) { classNames.Add(string.Format("{0}-layer", layer.Name.ToLower())); //add any additional class sanitizing logic here } } catch { } } return classNames; } } } 

Then adding it to Model.Classes in my Layout.cshtml theme Now I can style based on active layers.

 foreach(string className in WorkContext.GetLayerCssClasses()) { Model.Classes.Add(className); } 
0
source

All Articles