Logic and Loops

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.

<logic:if test="\Components\Website\Pages::currentPageId() == '/about-us/'"> <h2>About Us</h2> </logic:if>

Logic ELSE

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".

<logic:if test="\Components\Website\Pages::currentPageId() == '/about-us/'"> <h2>About Us</h2> </logic:if> <logic:else> <h2>Another Page</h2> </logic:else>


The ELSE tag must be defined after an IF tag. 

You can add additional Else tags with test conditions to be checked if the previous test fails:

<logic:if test="\Components\Website\Pages::currentPageId() == '/about-us/'"> <h2>About Us</h2> </logic:if> <logic:else test="\Components\Website\Pages::currentPageId() == '/services/'"> <h2>Services</h2> </logic:else> <logic:else> <h2>Another Page</h2> </logic:else>

Loops: The Data Repeater Control

<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 following example will list all Posts:

<data:repeater as="post" datasource="\Components\Website\Posts::getAll()"> <h1>[? $post['post_title'] ?]</h1> [? $post['post_content_short'] ?]; </data:repeater>


Or, to show all contacts in a specific Group:

<data:repeater as="contact" datasource="\Components\Customers\Groups::getContactsInGroup('/group-id-here/')"> <h1>[? $contact['contact_first_name'] ?]</h1> </data:repeater>

DataLimit and DataStart Attributes 

The DataLimit attribute can be used to limit the number of records displayed. For example, you may wish to only show three Posts: 

<data:repeater as="post" datalimit="3" datasource="\Components\Website\Posts::getAll()"> <h1>[? $post['post_title'] ?]</h1> [? $post['post_content_short'] ?]; </data:repeater>


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:

<data:repeater as="post" datastart="2" datasource="\Components\Website\Posts::getAll()"> <h1>[? $post['post_title'] ?]</h1> [? $post['post_content_short'] ?]; </data:repeater>


Note:
DataLimit is an array index, so the first record is at index 0:

[0 => "First Post"]
[1 => "Second Post"]
[2 => "Third Post"]

Advanced: Ordering & Filtering

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).