Orchard Core Layers - Prompt Templates
Configuring Layers for Conditional Widget Display
You are an Orchard Core expert. Generate configuration and code for managing layers and conditional widget visibility in Orchard Core.
Guidelines
- Layers are provided by the
OrchardCore.Layersmodule and control when widgets are displayed in theme zones. - A Layer has a name, description, and one or more conditions (rules) that determine when its widgets are visible.
- Widgets placed in a zone must be associated with a Layer; the widget renders only when the Layer's rules evaluate to
true. - Layers are managed from Design > Widgets in the admin panel. The layers list is an admin list named
Layerswith an Add Layer button at its top (theLayersCreateButtonshape, passed asAdminListContext.Actions); the old Add button under the list is gone. Checking a layer highlights its widgets in the zones beside the list. - The widgets in the zones render their Edit/Delete buttons through
AdminListActions, so they follow Settings → Admin → Actions layout (Buttons or one ellipsis Menu), like admin list rows. - The layer Create/Edit pages and the condition editor put Cancel first and Create/Save (
btn-primary) last in the bottom action bar. - Available zones are configured in Settings > Zones and must match theme layout sections.
- Conditions can be combined using
All(AND) andAny(OR) condition groups. - The
Alwayslayer uses aBooleancondition set totrue, so widgets on this layer always render. - Enable
OrchardCore.LayersandOrchardCore.Widgetsfeatures for full widget/layer support; Layers enablesOrchardCore.Rules. - Creating, editing, deleting, and reordering layers and their conditions requires the
ManageLayerspermission. - A rule (and an
All/Anygroup) with no conditions evaluates tofalse; use aBooleancondition set totruefor "always". - Always wrap recipe JSON in
{ "steps": [...] }. - All C# classes must use the
sealedmodifier except View Models. - Use file-scoped namespaces in C# examples.
Enabling Layer Features
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Layers",
"OrchardCore.Widgets"
],
"disable": []
}
]
}
Available Conditions
| Condition | Description |
|--------------------|-------------------------------------------------------------------------------|
| Homepage | True when the current page is the site homepage |
| Is anonymous | True when the current user is not authenticated |
| Is authenticated | True when the current user is authenticated |
| Role | Evaluates the current user's roles against a value |
| Url | Evaluates the current URL against a value |
| Culture | Evaluates the current UI culture against a value |
| Content Type | Evaluates the currently displayed content type against a value |
| Javascript | A custom script condition written in JavaScript |
| All | Condition group where all nested conditions must be true (AND logic) |
| Any | Condition group where at least one nested condition must be true (OR logic) |
| Boolean | A simple true/false condition |
Common Layer Patterns
Always Layer
The Always layer uses a Boolean condition set to true. Widgets on this layer are always visible (e.g., site footer, header navigation).
Homepage Layer
Uses the Homepage condition so widgets only display on the homepage (e.g., hero banners, featured content).
Authenticated Layer
Uses the Is authenticated condition so widgets display only for logged-in users (e.g., dashboard links, user menus).
Anonymous Layer
Uses the Is anonymous condition so widgets display only for unauthenticated visitors (e.g., login prompts, signup banners).
Declaring Zones in Themes
Zones must be declared as sections in the theme layout. Widgets are rendered in matching zones.
Liquid
{% render_section "Header", required: false %}
{% render_section "Content", required: false %}
{% render_section "Footer", required: false %}
Razor
@await RenderSectionAsync("Header", required: false)
@await RenderSectionAsync("Content", required: false)
@await RenderSectionAsync("Footer", required: false)
Configuring Zones
Set available zone names in Settings > Zones in the admin. Common zone names include:
HeaderNavigationBeforeContentContentAfterContentSidebarFooter
Recipe: Zones
{
"steps": [
{
"name": "Settings",
"LayerSettings": {
"Zones": [ "Header", "Content", "Footer" ]
}
}
]
}
Recipe: Creating Layers
Name must match the registered condition factory name (the condition class
name). Conditions nested inside an All/Any group also need $type
(for example "OrchardCore.Rules.Models.UrlCondition, OrchardCore.Rules"),
which the All Layers deployment step emits. Supplying LayerRule for an
existing layer replaces its whole condition list, and an unknown condition name
fails the step without saving.
{
"steps": [
{
"name": "Layers",
"Layers": [
{
"Name": "Always",
"Description": "Widgets on this layer are always displayed.",
"LayerRule": {
"Conditions": [
{
"Name": "BooleanCondition",
"Value": true
}
]
}
},
{
"Name": "Homepage",
"Description": "Widgets on this layer are displayed on the homepage only.",
"LayerRule": {
"Conditions": [
{
"Name": "HomepageCondition"
}
]
}
},
{
"Name": "Authenticated",
"Description": "Widgets on this layer are visible to authenticated users only.",
"LayerRule": {
"Conditions": [
{
"Name": "IsAuthenticatedCondition"
}
]
}
},
{
"Name": "Anonymous",
"Description": "Widgets on this layer are visible to anonymous users only.",
"LayerRule": {
"Conditions": [
{
"Name": "IsAnonymousCondition"
}
]
}
}
]
}
]
}
Recipe: Adding a Widget to a Layer and Zone
{
"steps": [
{
"name": "Content",
"data": [
{
"ContentItemId": "[js:uuid()]",
"ContentType": "HtmlWidget",
"DisplayText": "Footer Copyright",
"Latest": true,
"Published": true,
"LayerMetadata": {
"Layer": "Always",
"Zone": "Footer",
"Position": 10
},
"HtmlBodyPart": {
"Html": "<p>© 2025 My Company. All rights reserved.</p>"
}
}
]
}
]
}
Using Condition Groups
Combine conditions with All (AND) or Any (OR) groups for complex rules:
Example: Show widget on homepage for authenticated users only
Use an All group with both Homepage and Is authenticated conditions:
- Navigate to Design > Widgets.
- Create or edit a Layer.
- Add an All condition group.
- Inside the group, add a Homepage condition and an Is authenticated condition.
Example: Show widget on homepage or for admin role
Use an Any group with a Homepage condition and a Role condition set to Administrator:
- Navigate to Design > Widgets.
- Create or edit a Layer.
- Add an Any condition group.
- Inside the group, add a Homepage condition and a Role condition set to
Administrator.
Creating Custom Conditions
Implement custom layer conditions by following the Rules module pattern. Refer to the OrchardCore.Rules module for guidance on creating custom condition evaluators.
JavaScript Conditions
The Javascript condition evaluates an expression that must convert to a
Boolean. Layers adds these global methods:
| Function | Behavior |
|---|---|
| isHomepage() | The request path is the tenant home page. |
| isAnonymous() / isAuthenticated() | Authentication state of the current user. |
| isInRole(role) | Role name, case-insensitive. |
| url(pattern) | Case-insensitive path match; a trailing * makes it a prefix match. |
| culture(name) | Current culture or its parent culture, case-insensitive. |
// Show widgets on the blog section for signed-in users.
isAuthenticated() && url("/blog*")
- Saving validates the script: syntax errors (Jint
ScriptPreparationException),
runtime exceptions, and non-Boolean results are reported as validation
messages instead of failing the request.
- Each condition runs with a clean global scope. Variables, functions, or
const declarations from one layer's condition are not visible to another,
so write each condition as a self-contained expression.
- Conditions are bounded by
HttpContext.RequestAborted; a script still
running when the client disconnects is stopped with an
OperationCanceledException. Configure time or statement limits through
IOptions<Jint.Options> (see orchardcore-scripting).
- Prefer the structured
Url,Role,Culture, andHomepageconditions
over JavaScript when they express the rule; they are faster and easier to
audit.
Widget Management Workflow
- Enable
OrchardCore.LayersandOrchardCore.Widgetsfeatures. - Configure zones in Settings > Zones.
- Create layers with appropriate conditions in Design > Widgets.
- Create widget content types with the
Widgetstereotype. - Add widgets to zones and associate them with layers.
- Widgets display only when their associated layer's conditions evaluate to
true.
Upgrading from 3.x
- JavaScript conditions of different layers used to share one script scope,
so a var or const declared in one could break another. Each condition is
now isolated; remove any reliance on values declared by another layer.
- Invalid JavaScript conditions are rejected with a validation message on save
instead of an unhandled exception.
- The Add link below the layers list became an Add Layer button at the top of the list (
LayersCreateButton). UI tests and overrides ofViews/Admin/Index.cshtmlthat looked for the old link must update.