Back to top

This is an old revision of the document!


Views

Abstract: View

Functions

  • <php>abstract public function run()</php> This function should implement the logic of the view.
  • <php>protected function redirect($url, $status_code=302)</php> This function performs a redirect to the provided url.

TemplateView

<php>TemplateView</php> extends <php>View</php>.

Pipelines

Running the view

  1. <php>run()</php>
  2. if exception: <php>run_exception($exception)</php>:
    1. renders error template
  3. <php>run_form($form)</php>
  4. else: <php>run_page()</php>
    1. renders page template

Resolving a template name

  1. If <php>$this→template</php> is set, return that.
  2. If <php>$view_name</php> is set, return <php>'[base_name]_[view_name].phtml'</php>.
  3. Else return <php>'[base_name].phtml'</php>.

Where <php>$base_name</php> defaults to <php>'template_[page_id]'</php> but can be overriden by setting the <php>$template_base_name</php> property.

Properties

  • <php>protected $page_id</php> String used to identify the template name and the page title
  • <php>protected $template</php> Contains the view's form, but obsolete if you override <php>get_form()</php>.
  • <php>protected $template_base_name</php> Contains the view's success_url, but obsolete if you override <php>get_succes_url()</php> or <php>run_form_valid()</php>.
  • <php>protected $title</php> Contains a human readable page title.

Functions

<php>TemplateView</php> inherits all functions from <php>View</php>. It implements <php>run()</php>.

  • <php>public function construct($page_id, $title=)</php> * <php>public function run()</php> This function runs the logic of the view. It should not be overridden, as it makes sure exceptions are correctly rendered. If you want to customize how views are executed, you should override <php>run_page()</php>. * <php>protected function run_page()</php> * <php>protected function run_exception($e)</php> * <php>protected function render_template($template, array $context=[])</php> Renders a template with context. Merges in the default context. * <php>protected function get_default_context()</php> Returns the default context. The default implementation returns the view's <php>$title</php> and <php>$page_id</php>. * <php>protected function get_template($view_name=)</php> ===== Abstract: FormView ===== <php>FormView</php> extends <php>TemplateView</php>. ==== Pipelines ==== === Running the view === - <php>run()</php> - <php>run_page()</php> - <php>run_form($form)</php> - if form validates: <php>run_form_valid($form)</php> - <php>process_form_data($data)</php> - redirects to succes_url - else: <php>run_form_invalid($form)</php> - renders form ==== Properties ==== <php>FormView</php> inherits all properties from <php>TemplateView</php>. * <php>protected $form</php> Contains the view's form, but obsolete if you override <php>get_form()</php>. * <php>protected $success_url</php> Contains the view's success_url, but obsolete if you override <php>get_succes_url()</php> or <php>run_form_valid()</php>. ==== Functions ==== <php>FormView</php> inherits all functions from <php>TemplateView</php>. It implements <php>run_page()</php>. * <php>protected function run_form($form)</php> It is not recommended to override this function if you want to customize form handling. Override <php>form_valid()</php> and <php>form_invalid()</php> instead. * <php>protected function form_valid($form)</php> * <php>protected function form_invalid($form)</php> * <php>abstract protected function process_form_data($data)</php> This function should implement the logic used to process the form data. The <php>$data</php> variable is an associated array containing field_namevalue pairs. * <php>protected function get_form()</php> * <php>protected function get_success_url()</php> ===== ModelView ===== <php>ModelView</php> extends <php>FormView</php>. ==== Pipelines ==== === Running the view === - <php>run()</php> - <php>run_page()</php> - if view = create: <php>run_create()</php> - <php>get_form()</php> - <php>run_form($form)</php> according to FormView pipeline - if view = read: <php>run_read()</php> - <php>get_object()</php> - renders template for view = read with context variable 'object' - if view = update: <php>run_update()</php> - <php>get_form()</php> - populate form with <php>get_object()</php> - <php>run_form($form)</php> according to FormView pipeline - if view = delete: <php>run_delete()</php> - if request method = POST - delete object - redirect to success url - else - renders template for view = delete with context variable 'object' - if view = list: <php>run_list()</php> - renders template for view = list with context variable 'objects' ==== Properties ==== <php>ModelView</php> inherits all properties from <php>FormView</php>. * <php>protected $views</php> List of implemented views. Defaults to <php>['create', 'read', 'update', 'delete', 'list']</php>. * <php>protected $default_view</php> The name of the view to run if the URL parameter <php>view</php> is not provided. Defaults to <php>“list”</php>. * <php>protected $model</php> Contains the view's model object, but obsolete if you override <php>get_model()</php>. ==== Functions ==== <php>ModelView</php> inherits all functions from <php>FormView</php>. It implements <php>process_form_data()</php> and overrides <php>construct()</php>, <php>run_page()</php>, <php>get_success_url()</php> and <php>get_template()</php>.
  • <php>public function __construct($page_id, $title=, $model=null, $form=null)</php> * <php>protected function run_page()</php> It is not recommended to override this function unless you want to create additional views. Override <php>run_create()</php>, <php>run_read()</php>, <php>run_update()</php>, <php>run_delete()</php> and <php>run_list()</php> instead. * <php>protected function run_create()</php> * <php>protected function run_read()</php> * <php>protected function run_update()</php> * <php>protected function run_delete()</php> * <php>protected function run_list()</php> * <php>protected function process_form_data($data)</php> Creates or updates object in database. * <php>protected function get_model()</php> * <php>protected function get_object()</php> * <php>protected function get_success_url()</php> Overridden to redirect to the default view. * <php>protected function get_template($view_name=)</php> Overridden to render the template for the active view if <php>$view_name</php> is not provided.

documentation/rack/reference/views.1527269517.txt.gz · Last modified: 2022/04/03 16:00 (external edit)