Orchard CMS:基于层的条件 CSS 类 [英] Orchard CMS: Conditional CSS Class based on Layer
问题描述
如果我在特定层中,我正在尝试添加一个 css 类.
I am trying to add a css class if I am in a particular layer.
所以 2 个问题:
是否可以在 Razor 视图中识别当前层.类似的东西:
Is it possible to identify the current layer in a Razor view. Something like:
if(currentLayer == "TheHomepage") { ... }
if(currentLayer == "TheHomepage") { ... }
在图层上处理 HTML 条件是正确的方法,还是在 Orchard 中有更好的方法来做到这一点?
Is the the right way to approach HTML conditional on layer, or is there a better way to do this in Orchard?
推荐答案
如果您需要查看当前处于活动状态的图层,您可以执行以下操作:
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 more sense in a driver or controller, but if you are working only in the view layer, you can do it this way.
这篇关于Orchard CMS:基于层的条件 CSS 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!