LemonStand Documentation

Cms_Controller

Cms_Controller is responsible for pages, partials and templates rendering. It provides methods required for building a website, acting as a glue for website elements.

The instance of Cms_Controller class is created automatically by LemonStand and available in a code of any page, template and partial as $this variable.

Class fields

  • $page – a reference to a current page. An object of the Cms_Page class.
  • $customer – a reference to a current customer. This variable is NULL if there is no customer logged in. An object of the Shop_Customer class or null.
  • $data - an array-type filed for passing variables from actions to pages and partials. See the Actions page for details.

Class methods

  • render_page() - renders a current page. This method should be called inside page templates to output a current page content.
  • render_partial($name, $parameters = array(), $options = array()) - renders a partial with specified name. The second parameter is optional array of variables to pass into the template. The first array allows to pass some extra parameters to the method. Example: 
    $this->render_partial('product_list', array('products'=>$category->products));
  • render_head() - outputs the header content of the page. You can define the header content on the Head & Blocks tab of the Create/Edit Page form. Please read this article for details.
  • render_block($block_code) - outputs a page block with the specified code. Use this method for displaying code block defined on the Head & Blocks tab of the Create/Edit Page form. If the specified block does not exist no error will occur. Please read this article for details.
  • request_param($index, $default_value = null) – returns a parameter passed to the page in the URL, by a parameter index. The indexes a zero-based. The second optional parameter specifies a default value for a parameter. It is used if a parameter with specified index is not found.
    When LemonStand parses an URL, it considers all URL segments following a page address as parameters. For example, if you have a page with address /category (the value, specified in the Page URL field in the Page Editor), and you open the page using URL /category/computers, the word computers will be considered as a parameter with index 0.
    Since version 1.1.85 of the CMS module, the $index parameter can take negative values.  In this case the method returns index'th parameter from the end of URL. For example, If the input URL was /category/men/jumpers/2, calling the method with $index=-1 would return 2 (i.e. first parameter from the end).
  • exec_action_handler($handler) - allows to execute an AJAX handler directly, from your custom code. This method is extremely helpful for customizing default server handlers. It allows you to wrap any code around a standard LemonStand handler. The following example demonstrates a custom AJAX event handler, which can be used for implementing a single-form checkout process. This code should be entered to the AJAX Handlers field of the AJAX tab on the Create/Edit Page form.
    function create_enquiry($controller, $page, $params)
    {
      if (!Shop_Cart::list_active_items())
        throw new Exception('Your cart is empty!');
    
      $controller->exec_action_handler('shop:on_checkoutSetBillingInfo');
      Shop_CheckoutData::copy_billing_to_shipping();
      Shop_CheckoutData::set_payment_method(Shop_PaymentMethod::find_by_api_code('no_payment')->id);
      Shop_CheckoutData::set_shipping_method(Shop_ShippingOption::find_by_api_code('free_shipping')->id);
      Shop_CheckoutData::place_order($controller->customer);
      
      Phpr::$response->redirect('/thankyou_page');
    }
  • get_customer_group_id() - returns an identifier of a customer group a current customer belongs to. If the customer is not logged-in, the method returns an identifier of the Guest group.
  • get_customer_group() - returns a customer group object (Shop_CustomerGroup) a current customer belongs to. If the customer is not logged in, returns the Guest group object. The result of this method can be altered by the cms:onGetCustomerGroupId event handler.
  • js_combine($files, $options = array()) - allows to combine multiple JavaScript resources. The $files parameter should contain a list of JavaScript files to include. The files should be specified relative to the LemonStand root directory. Remote resources should be prefixed with the http:// or https:// protocol specifier. Also the following aliases are supported:
    - mootools - included the MooTools core script,
    - ls_core_mootools - includes LemonStand MooTools-specific front-end library,
    - jquery - includes jQuery core script,
    - jquery_noconflict - includes jQuery noConflict() call,
    - ls_core_jquery  - includes LemonStand jQuery-specific front-end library.jqu
  • The $options array can accept the following parameters: reset_cache, skip_cache, src_mode. Example:
    <head>
      <?= $this->js_combine(array(
        'mootools',
         'ls_core_mootools',
         'http://www.digitalia.be/demo/slimbox/js/slimbox.js'
      )) ?>
      ...
    
  • css_combine($files, $options = array()) - allows to combine multiple CSS resources. The $files parameter should contain a list of CSS files to include. The files should be specified relative to the LemonStand root directory. Remote resources should be prefixed with the http:// or https:// protocol specifier. Also the following alias is supported: 
    - ls_styles -  includes LemonStand front-end CSS styles file.
    LemonStand automatically processes relative URLs in the CSS files, so you can use CSS files from different locations and it will not break images. The $options array can accept the following parameters: reset_cache, skip_cache, src_mode. Example:
    <head>
      <?= $this->css_combine(array(
        '/resources/css/styles.css',
        '/resources/css/slimbox.css'
      )) ?>
      ...
  • action() - executes the page CMS action. Use this method in custom AJAX handlers to force the controller to execute the page action.

See also:

Next: Cms_Page
Previous: Phpr_Pagination
Return to Reference