Defines an alternate region to show when an If test fails. The Else tag must be defined as a sibling to an existing If tag. Contiguous Else tags with test conditions allow further conditions to be checked if the previous test fails.
The content to show if the test condition is true.
Attributes
- `evaluateAt`
string
Sets whether to evaluate the test during the render phase (when rendered) or when the object (and children) are being constructed
Note: Do not use when used for checking security (as child nodes may still be constructed and events processed)
Valid options: render, init
- `test`
New
Sets the test for the statement
Tutorials
It's common practise on eCommerce websites to display information about the contents of the current cart. This article will explain how
to code a "current cart".
Current Cart: Number Of Items In Cart
The following snippet of code displays the number of items in the current user's cart. The snippet checks to determine whether there are any
items in the cart, if there are no items present in the cart, "No Items in Cart" will be displayed.
The following snippet of code displays the total of the current user's cart. The snippet checks to determine whether there are any items in the cart, if there are no
items present in the cart, "No Items in Cart" will be displayed.
Logic is used to show content based on a test. Repeaters are used to list elements like Products, Pages, or Contacts.
Logic IF
The <logic:if> control performs a test, and renders everything inside the tag if the test is true. The test
is defined within the test="true" attribute, and is often an API call.
The following examples are usually defined in the website design to show content dynamically:
IF the current visitor is logged in, show "you are logged in".
<logic:if test="\Components\Customers\Contacts::currentIsLoggedIn()">
<p>You are logged in.</p>
</logic:if>
IF the current page is the "About Us" page, show a heading.
The <logic:else> control defines an alternate region to show in the case that an IF test fails. Extending the
above examples:
IF the current visitor is logged in, show "you are logged in". ELSE show "You are not logged in":
<logic:if test="\Components\Customers\Contacts::currentIsLoggedIn()">
<p>You are logged in.</p>
</logic:if>
<logic:else>
<p>You are not logged in.</p>
</logic:else>
IF the current page is the "About Us" page, show a heading. ELSE show a message "Another Page".
<data:repeater> is commonly used to list elements like Products, Pages, or Contacts.
The Data Repeater repeats everything inside the tag, for each item in a given data source.
The "as" attribute defines the name of the variable, and the "datasource" attribute usually defines an API call to
retrieve a Page, Product or Contact.
The DataStart attributes may be used in addition to the DataLimit attribute to show from a record, to a record. As an example, the
following code will only show all Posts, starting at the Third Post:
Click here to read more about how to use the DataFilter attribute to filter
records (eg. show all products, greater than $100 in value).
Click here to see an example illustrating how to order
records (eg. by value or alphabetically).
Examples
Else - Syntax
Use ELSE after an IF logic statement. Specify a block of code to be executed if a logic IF test failed.
HTML:
<logic:if test="">
If this logic test passes, this will render.
</logic:if>
<logic:else test="">
If the first logic test fails, but this second test passes, this will render.
</logic:else>
<logic:else>
If neither of the above tests pass, this will render.
</logic:else>
Show a different message when a contact is logged in.
HTML:
<logic:if test="\Components\Customers\Contacts::currentIsLoggedIn()">
Welcome Back! You're logged in.
</logic:if>
<logic:else>
You're not logged in, click here to login.
</logic:else>