Page views usage

It is recommended page views to be created in init() method:

public function init() {
    
parent::init();

    
$v = new Site_Web_Page_View($this); // view name is not specified so default 'default' will be used
    
$this->add_view($v); // registering the view
}

class Site_Web_Page_View {
__constructor ( Web_Page $page [, string $name [, string $pages_tpl_path [, string $subdir]]] )
}

page
WP object of current page
name
Name of the view. This is automatically exported as $_view. Must be alphanumeric, starting with letter, underscore allowed, 100 characters max.. Default value is 'default'.
pages_tpl_path
Relative path to directory where page TPLs can be found. Automatically exported as $_path. Default value is 'pages/'. This path is relative to templates engine tpl path (aka, base tpl path, which is %ROOT%hidden/tpl). In case of Smarty template engine base tpl path is set at boot time and value is taken from hidden/conf/machine_specific.conf (SMARTY_TEMPLATE_PATH key).
subdir
Used for grouping inside language subdirs. Automatically exported as $_subdir. Default value is '' (empty). It is tpl's job to switch properly using this variable (example: {include file="`$_path``$_language`/`$_subdir``$_page`/`$_page`_`$_view`_`$_language`.tpl"}).

Each WP must return instance of Web_Page_View from its run() or event handling method. You can get and return one of registered (added) view using $this->get_view('view_name');. Example:

public function run() {
    ... 
// some functionality
    
$view $this->get_view('default'); // getting already created and added (registered) view

    
return $view// returning view object
}

You have to specify view name as parameter to get_view(). This name must be name of a view which is already added to the page (in init() method). If you miss to specify view name - 'default' will be used as default value.

Theoretically it is possible page view to be created just before returning it by run() or event handling method but this is not recommendable. Although it is more efficient it lowers the readability of the code. By creating and registering views in init() method (which is on top) you declare loudly: this page can handle these views, so reader can get a quick idea about what to expect.