waViewAction

Controller (action) returning an HTML string

Contents...

Parent class: waController.

See explanation of HTTP request routing in backend and in frontend.

Class waViewAction is intended for generating an HTML string, which can be either returned to browser in response to an HTTP request or used for any other purpose. Controllers based on waViewAction class are called (PHP) actions.

Main PHP action logic must be described in public method execute().

HTML templates

Each PHP class based on waViewAction (except for cases when method setTemplate is used), must have an HTML template file located in templates/actions/. HTML template files must be grouped by module ID; e.g., templates/actions/backend/, templates/actions/settings/.

An HTML template file must be named by rule [Module_id][Action_id].htmle.g., Backend.html (template for action default of module backend), BackendPage.html, SettingsSection.html.

How to use

There are several methods to use PHP actions as shown below.

Method 1. Sending an HTML string in repose to an HTTP request

To make an action class process certain HTTP requests, it must have corresponding action_id and module_id and, in case of frontend requests, routing must be set up as described in the article about request routing.

To obtain the HTTP request contents in PHP action class, use methods of waRequest class; e.g.:

$page_id = waRequest::post('id', null, waRequest::TYPE_INT);

To pass values to action's HTML template, use instance of template engine class (Smarty 3 is used by default), which is accessible via action class field $viewe.g., :

$this->view->assign('customer', $customer_data);

No additional efforts are required for sending the resulting HTML string back to user, it is done automatically.

Method 2. Returning an HTML string

An action class can be used simply for generating an HTML string, without its mandatory sending back to use. This how it can be achieved:

  1. Create an instance of action class. Pass an array of additional parameters to its constructor, if needed.
  2. Call action's method display() to get the generated HTML string.

The obtained HTML string can be used at your discretion.

Example

$page_action = new myappPageAction(
    array(
        'id' => $page_id
    )
);
$page_html = $page_action->display();

The shorthand for the above example would be as follows, with the use of wao function:

$page_html = wao(new myappPageAction(
    array(
        'id' => $page_id
    )
))->display();

Methods

public function display ($clear_assign = true)

Returns HTML string generated by action class.

Parameters

Example

$page_action = new myappPageAction(
    array(
        'id' => $page_id
    )
);
$page_html = $page_action->display();

public function getLayout ()

Returns layout class instance (waLayout) currently used by action class.

Example

$layout = $this->getLayout();

protected function getTemplate ()

Returns path to HTML template file currently used by action class.

Example

$current_template = $this->getTemplate();

public function getTheme ()

Returns instance of waTheme class containing information about frontend design theme.

Example

$theme = $this->getTheme();

protected function getThemeUrl ()

Returns URL of frontend design theme files directory.

Example

$theme_dir_url = $this->getThemeUrl();

public function setLayout (waLayout $layout = null)

Sets current layout (waLayout).

Parameters

Example

$this->setLayout(new myappBackendLayout());

public function setTemplate ($template)

Sets path to alternative HTML template file. When this method is called, an action can have no HTML template file as described above.

Parameters

Examples

//for an app
$this->setTemplate('BackendOrder');
$this->setTemplate(wa()->getAppPath('templates/actions/backend/BackendOrder'), 'myapp');
//for a plugin
$this->setTemplate('BackendOrder');
$this->setTemplate(wa()->getAppPath('plugins/myplugin/templates/actions/backend/BackendOrder'), 'myapp');

protected function setThemeTemplate ($template)

Sets HTML template file for a frontend action class.

Parameters

Example

$this->setThemeTemplate('page.html');