This topic targets, and was tested with, the Orchard 1.10.2 release.
本主题针对Orchard 1.10.2版本进行了测试,并进行了测试。
In Orchard, you can use rules to determine the visibility of both layers (see Managing widgets) and elements within a Layout.
在Orchard中,您可以使用规则来确定两个层的可见性(请参阅[管理窗口小部件](管理窗口小部件))和布局中的元素。
A rule is composed of one or more conditions which are combined using one or more logical operators such as AND, OR and NOT. This article describes how to compose a rule using conditions and lists the conditions that are available within a standard Orchard installation.
规则由一个或多个条件组成,这些条件使用一个或多个逻辑运算符组合,例如AND,OR和NOT。本文介绍如何使用条件撰写规则,并列出标准Orchard安装中可用的条件。
Rules
规则
A rule is constructed by using one, or more, conditions that are checked to determine whether the rule overall evaluates to true or false. If the rule evaluates to true, the associated content will be displayed, if not, then it won't.
通过使用一个或多个条件来构造规则,这些条件被检查以确定规则总体评估为 true 或 false 。如果规则评估为 true *,则将显示关联的内容,如果不是,则不显示。
There are two basic conditions provided by the Orchard.Conditions module, these are:
Orchard.Conditions模块提供了两个基本条件,它们是:
-
authenticated
- Whether the current user is authenticated, or not -
authenticated
- 当前用户是否经过身份验证 -
url
- To allow you to check whether the current request matches a certain URL, or not -
url
- 允许您检查当前请求是否与某个URL匹配
These allow you to compose rules that determine whether a given layer or element is visible, for example:
这些允许您撰写确定给定图层或元素是否可见的规则,例如:
(not authenticated and url("~/about")) or authenticated
If this rule was applied to a certain layer, the contents of the layer would be visible if:
如果此规则应用于某个图层,则在以下情况下该图层的内容将可见:
-
the user is not logged in AND the page is the "about" page, or
-
用户未登录 AND 页面是“关于”页面,或
-
the user is logged in
-
用户已登录
There are also other conditions that you can use to compose rules, implemented by other Orchard modules, that are described in detail below.
您还可以使用其他条件来编写由其他Orchard模块实现的规则,这些条件将在下面详细介绍。
Available Conditions
可用条件
There are several conditions provided by the core Orchard modules, over and above the authenticated
and url
ones provided by the Orchard.Conditions module itself. In order to use a condition, the module it's provided by must be both installed and enabled, otherwise you may receive the below error message when attempting to use the condition:
核心Orchard模块提供了几个条件,超出了Orchard.Conditions模块本身提供的authenticated
和url
。为了使用条件,必须同时安装和启用它所提供的模块,否则在尝试使用条件时可能会收到以下错误消息:
The rule is not valid: Expression is not a boolean value
规则无效:Expression不是布尔值
The conditions provided are:
提供的条件是:
Condition Syntax | Module | Description
条件语法|模块|描述
---------------------------------------| ---------------------|-----------------------
- | --------------------- | -----------------------
authenticated | Orchard.Conditions | True if the user is logged in.
经过验证的Orchard.Conditions |如果用户已登录,则为True。
ContentType("<Type>") | Orchard.Widgets | True if the content type being view matches the content type specified e.g. ContentType("Page")
ContentType(“&lt; Type&gt;”)| Orchard.Widgets |如果正在查看的内容类型与指定的内容类型匹配,则为True 的ContentType( “页面”)
culturecode("<Culture-Code>"[]) | Orchard.Localization | Check to see if the culture of the content being rendered matches, for example culturecode("en-gb")
will match the 'English - United Kingdom' locale
culturecode(“&lt; Culture-Code&gt;”[])| Orchard.Localization |检查所呈现内容的文化是否匹配,例如culturecode(“en-gb”)
将匹配'英语 - 英国'语言环境
culturelcid(<LocaleId>[]) | Orchard.Localization | Check to see if the Locale ID of the content being rendered matches, for example culturelcid(1033)
will match the 'English - United States' locale
culturelcid(&lt; LocaleId&gt; [])| Orchard.Localization |检查要呈现的内容的区域设置ID是否匹配,例如culturelcid(1033)
将匹配'英语 - 美国'区域设置
cultureisrtl(true|false) | Orchard.Localization | Check to see if content is being rendered Right To Left, or not, for example cultureisrtl(true)
will return false for non-RTL content
cultureisrtl(true \ | false)| Orchard.Localization |检查内容是否从右到左呈现,例如cultureisrtl(true)
将为非RTL内容返回false
culturelang("<Language>"[]) | Orchard.Localization | Check the language that content is being rendered for, for example culturelang('en')
will return true for English content
culturelang(“&lt; Language&gt;”[])| Orchard.Localization |检查正在呈现内容的语言,例如culturelang('en')
将为英语内容返回true
url("<url path>") | Orchard.Conditions | True if the current URL matches any of the specified paths. If you add an asterisk (*) at the end of the path, all pages found in subfolders under that path will evaluate to true (for example, url("~/home*")
), multiple urls can be specified by separating them with ',' (for example, url("~/home*", "~/about")
).
url(“&lt; url&nbsp; path&gt;”)| Orchard.Conditions |如果当前URL与任何指定路径匹配,则为True。如果在路径的末尾添加星号(*),则在该路径下的子文件夹中找到的所有页面将评估为true(例如,url(“〜/ home *”)
),可以通过以下方式指定多个URL:用','分隔它们(例如,url(“〜/ home *”,“〜/ about”)
)。
role("<role>"[]) | Orchard.Roles | Allows you to test the current user to see if they're a member of a specific role, for example, role("Moderator")
(Note: These are case-sensitive, 'Moderator' will be matched, 'moderator' won't)
角色(“&lt; role&gt;”[])| Orchard.Roles |允许您测试当前用户以查看他们是否是特定角色的成员,例如,role(“Moderator”)
(注意:这些是区分大小写的,'主持人'将匹配,'主持人'惯于)
not | n/a | Logical NOT.
不是|不适用|逻辑不。
and | n/a | Logical AND.
和|不适用|逻辑和。
or | n/a | Logical OR.
或者不适用|逻辑或。
The presence of []
in the condition syntax denotes the fact that this condition accepts multiple parameters. Examples of using these conditions with multiple parameters are given below.
条件语法中存在[]
表示该条件接受多个参数的事实。下面给出使用具有多个参数的这些条件的示例。
You can find modules that implement additional conditions by searching the gallery for the term 'rules'.
您可以通过[在图库中搜索术语“规则”]找到实现其他条件的模块(http://gallery.orchardproject.net/Packages/Modules?q=rules)。
Conditions that take multiple parameters
采用多个参数的条件
Several of the conditions provided within core Orchard modules allow you to specify more than one parameter. Both examples for each of the following rules (where the condition is used with a logical or
and where the condition has an array of parameters passed to it) are equally valid:
核心Orchard模块中提供的一些条件允许您指定多个参数。以下每个规则的两个示例(其中条件与逻辑“或”一起使用,并且条件具有传递给它的参数数组)同样有效:
A rule to display content for either the en-gb or en-us culture code:
显示en-gb或en-us文化代码的内容的规则:
culturecode("en-gb") or culturecode("en-us")
culturecode("en-gb", "en-us")
A rule to display content for either the English, or French language:
显示英语或法语语言内容的规则:
culturelang("en") or culturelang("fr")
culturelang("en", "fr")
A rule to display content for either of the 1033 and 2057 (en-us and en-gb) locales:
显示1033和2057(en-us和en-gb)区域设置的内容的规则:
culturelcid(1033) or culturelcid(2057)
culturelcid(1033, 2057)
A rule to display content for users who are members of the Moderator or Administrator roles:
为作为主持人或管理员角色成员的用户显示内容的规则:
roles("Moderator") or roles("Administrator")
roles("Moderator", "Administrator")
Places you can use rules
您可以使用规则的地方
You can use rules to determine the visibility of things in at least these places:
您可以使用规则来确定至少在这些地方的事物的可见性:
-
Layers - to determine when a layer is visible
-
图层 - 确定图层何时可见
-
Layouts - to determine when individual elements within the layout are visible
-
布局 - 确定布局中的各个元素何时可见
-
Pages - to determine when individual elements within the page are visible (this is effectively the same as for a Layout as from Orchard v.1.9 onwards, by default the Page content type has a LayoutPart instead of the BodyPart)
-
页面 - 确定页面中的各个元素何时可见(这实际上与布局相同,从Orchard v.1.9开始,默认情况下,页面内容类型具有LayoutPart而不是BodyPart)