This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

Actions

This page continues the Programming pages article.

Actions allow you to execute some PHP code before the page is rendered. For each page you can write your own action code, or select an existing action from the drop-down menu. Of course, you don't need to develop an action for every page on your website – you use actions only when you need them.

For example, on a category page you want to display a list of the category products. You need to create the Category object (the $category variable) and then load the list of products to the $products variable. Normally you would place the PHP code for preparing the variables right into your page code. Actions allows you to separate the preparation code from the page code. Another good thing about actions – most of the common cases are already implemented in actions provided by LemonStand modules. The example with the category page is implemented in the shop:category action.

As action code is executed before a page is rendered, actions could be used for processing forms. Many actions provided by LemonStand can handle events like adding a product to the shopping cart, or even processing payments.

In the Page Editor there is a special tab designated for actions. On that page there are three elements – the drop-down list of existing actions (provided by LemonStand modules) and two text areas allowing you to write PHP code to be executed before and after the action selected in the drop-down list. Continuing the example with the category page – you need to select the shop:category action in the drop-down list. After that the $category variable becomes available in the page code. You can use this variable to access the category product list and display products on the page.

LemonStand executes action code in the following order:

  1. Pre-action PHP code
  2. Action
  3. Post-action PHP code

If, during the execution of any of these steps an error occurs, the subsequent steps will not be executed. The common usage of the pre-action PHP code is checking form data before executing an action, provided by LemonStand.

Most of the built-in actions are documented. When you select an action in the drop-down menu, the action documentation is displayed in the Action Info tab on the right side of the page.

Usually you will not need to write custom action code as LemonStand has built-in actions for all typical store pages and cases.

Passing values from an action to a page, template or partial code

Marking a navigation link as the currently viewed page is the most common case where you would where you would pass a value from a page to a template.  For example, you may want to highlight the “Shop” tab when a visitor views any of the store pages. Best practices in LemonStand suggest code reusing, using partials. In the case with navigation tabs it means that the tabs should be placed in the page template or a partial rather than repeating them on each page. But as a single template or partial could be used multiple times on different pages, how would you mark a current tab?

Fortunately, you can create a PHP variable in the page action and then access the variable inside a template, page or partial code. To create a variable, go to the Action tab and paste the following code into the Pre Action Code field:

$this->data['current_tab'] = 'shop';

The $this variable refers to the controller object (Cms_Controller class variable). Each element you create in the $data field of the controller object turns into a PHP variable which you can access inside a page, template or partial code.

Now, when you have the $current_tab variable defined, you can use it in a template or partial where you output the tabs markup. The following code snippet demonstrates possible code for displaying the Shop tab.

...
<li <? if (isset($current_tab) && $current_tab == 'shop'): ?>class="current"<? endif ?>>Shop</li>
...
...
<li {% if current_tab == 'shop' %}class="current"{% endif %}>Shop</li>
...

See also:

Next: AJAX
Previous: Programming Pages
Return to Programming Pages