Walkthrough: Using Rules

Last updated Monday, May 23, 2016 in SPEAK for Developer
Keywords: Development

You use rules to add business logic to SPEAK pages. This walkthrough outlines how to use rules:

  • Create a rule definition item
  • Use the Rule Editor to create rules
  • Edit rules placeholders
  • Create multiple rules
  • Add and configure a Rule component

Create a rule definition item

To create a rule definition item:

  • Use the RuleDefinition template to create an item as a child of the PageSettings item of your page or application.

If you need multiple Rule Definition items for a page or an application, you can create a folder under the PageSettings item and put all the Rule Definition items in this folder.

Use the Rule Editor to create rules

To create a rule:

  1. Open a RuleDefinition item.
  2. Click Edit.

    data

  3. In the Rule Editor dialog, double-click the lines in the Conditions and Actions panes to create a rule:

    rule-editor

  4. You can add more conditions to the rule section by double-clicking lines in the Conditions pane:

    rule-1

    You toggle between the blue and underlined "and" and "or" by clicking these words. The Rule Editor adjusts the indentation of the conditions to indicate how SPEAK evaluates them. You can right-click a condition to delete it, move it up, or move it down.

  5. In the Actions pane, you can add multiple actions by double-clicking lines. Actions are always conjoined by "and." You can right-click an action to delete it, move it up, or move it down.

Edit rule placeholders

Conditions and rules contain placeholders. The placeholders are blue and underlined. You have to edit the placeholders before the rule works:

  • Some placeholders are toggles. This is the case for logical operators such as "where." Click these placeholders to toggle the value (for example, "where" -> "except where" -> "where").
  • Some placeholders refer to a component. They are often called "name." Click the placeholder to enter the name of the SPEAK component that the condition or action refers to.
  • Some placeholders refer to the value of component property. They are often called "value." If the condition or action is for one specific property only, the value placeholder has the name of this property, for example "min width."

Create multiple rules

You can create more than one rule in one RuleDefinition item:

  • In the Rules panel, right-click and select Add New Rule.

Sitecore executes rules sequentially when there are multiple rules. You cannot make rules conditional, so SPEAK executes all rules when the RuleDefinition item is triggered.

Add and Configure a Rule Component

You add a Rule component to your SPEAK page to trigger the rules that you have created in the RuleDefinition items:

  1. Add a Rule component to the page in the Layout Designer.
  2. Specify a RuleDefinition item in the RuleItemID property.
  3. Specify a component in your page in the TargetControl property. SPEAK listens for events that this component raises.
  4. Specify an event in the Trigger property. SPEAK listens for this event.

If you do not specify a control in TargetControl, SPEAK listens for events raised by the “window” default component.

You add a Rule component to your page for each event that you want to respond to, and a page can have multiple Rule components. Several different Rule components can point to the same Rule item.