Back to top

Views

Abstract: View

Functions

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

TemplateView

TemplateView extends View.

Pipelines

Running the view

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

Resolving a template name

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

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

Properties

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

Functions

TemplateView inherits all functions from View. It implements run().

  • __construct($page_id, $title='')
  • run() 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 run_page().
  • protected run_page()
  • protected run_exception($e)
  • protected render_template($template, array $context=[]) Renders a template with context. Merges in the default context.
  • protected function get_default_context() Returns the default context. The default implementation returns the view's $title and $page_id.
  • protected function get_template($view_name='')

Abstract: FormView

FormView extends TemplateView.

Pipelines

Running the view

  1. run()
  2. run_page()
  3. run_form($form)
  4. if form validates: run_form_valid($form)
    1. process_form_data($data)
    2. redirects to succes_url
  5. else: run_form_invalid($form)
    1. renders form

Properties

FormView inherits all properties from TemplateView.

  • protected $form Contains the view's form, but obsolete if you override get_form().
  • protected $success_url Contains the view's success_url, but obsolete if you override get_succes_url() or run_form_valid().

Functions

FormView inherits all functions from TemplateView. It implements run_page().

  • protected run_form($form) It is not recommended to override this function if you want to customize form handling. Override form_valid() and form_invalid() instead.
  • protected form_valid($form)
  • protected form_invalid($form)
  • abstract protected process_form_data($data) This function should implement the logic used to process the form data. The $data variable is an associated array containing field_namevalue pairs.
  • protected get_form()
  • protected get_success_url()

ModelView

ModelView extends FormView.

Pipelines

Running the view

  1. run()
  2. run_page()
  3. if view = create: run_create()
    1. get_form()
    2. run_form($form) according to FormView pipeline
  4. if view = read: run_read()
    1. get_object()
    2. renders template for view = read with context variable 'object'
  5. if view = update: run_update()
    1. get_form()
    2. populate form with get_object()
    3. run_form($form) according to FormView pipeline
  6. if view = delete: run_delete()
    1. if request method = POST
      1. delete object
      2. redirect to success url
    2. else
      1. renders template for view = delete with context variable 'object'
  7. if view = list: run_list()
    1. renders template for view = list with context variable 'objects'

Properties

ModelView inherits all properties from FormView.

  • protected $views List of implemented views. Defaults to ['create', 'read', 'update', 'delete', 'list']. Additionally a view for export to CSV ('csv') has been implemented, but is disabled by default.
  • protected $default_view The name of the view to run if the URL parameter view is not provided. Defaults to "list".
  • protected $model Contains the view's model object, but obsolete if you override get_model().

Functions

ModelView inherits all functions from FormView. It implements process_form_data() and overrides __construct(), run_page(), get_success_url() and get_template().

  • public __construct($page_id, $title='', $model=null, $form=null)
  • protected run_page() It is not recommended to override this function unless you want to create additional views. Override run_create(), run_read(), run_update(), run_delete(), run_list() and run_csv() instead.
  • protected run_create()
  • protected run_read()
  • protected run_update()
  • protected run_delete()
  • protected run_list()
  • protected run_csv() Disabled by default. Enable by overriding $views (add 'csv').
  • protected get_csv_data() Provides dataset for run_csv() as associative array.
  • protected process_form_data($data) Creates or updates object in database.
  • protected get_model()
  • protected get_object()
  • protected get_success_url() Overridden to redirect to the default view.
  • protected get_template($view_name='') Overridden to render the template for the active view if $view_name is not provided.

documentation/rack/reference/views.txt · Last modified: 2023/08/31 23:22 by Martijn Luinstra