DGS Templates (WEB Mode)

A Data Grid Store (DGS) has two modes. In the api mode, the DGS returns JSON. In the web mode, the DGS renders HTML from templates. The web mode is the default.

This page tells you:

  • which templates the DGS uses
  • where the DGS looks for a template
  • how to replace a template without a change to the default theme

Related pages:

The view object

Each store has a view object of the class StoreView. It extends Template. The actions of the store render their templates with this object.

In a DGS template, $this is the StoreView.

<?php
/**
 * @var StoreView $this
 * @var Store $store
 */
?>
<div id="orders-<?php echo $store->getIdent(); ?>">
    <?php echo $this->fetch('store/view/list/template.phtml'); ?>
</div>

A plugin template and a DGS template are different objects. A DGS template does not have the variables that the plugin assigned.

Where the DGS looks for a template

The StoreView looks in these directories, in this sequence. It uses the first file that it finds.

flowchart TD A["The action renders<br>store/list.phtml"] --> B{"File in the theme<br>templates directory?"} B -- yes --> T["Use the file of the theme"] B -- no --> C{"File in the<br>Theme package?"} C -- yes --> E["Use the default file"] C -- no --> D{"File in the templates<br>directory of the plugin<br>of the store?"} D -- yes --> P["Use the file of the plugin"] D -- no --> X["SystemException:<br>Template file not found"]
Step Directory Option or constant Default value
1 Templates of the active theme Store::OPTION_THEME_TEMPLATE_PATH (from the Core option theme_template_path) themes/<theme name>/templates/
2 Templates of the Theme package ENGINE_TEMPLATE_PATH vendor/festi-team/festi-framework-theme/templates/
3 Templates of the plugin of the store getPluginTemplatePath() of the plugin plugins/<Name>/templates/

Important facts:

  • The templates directory of the plugin is the last step. Thus a file plugins/<Name>/templates/store/list.phtml has no effect: the theme or the Theme package has a file with this name, and the DGS finds that file first.
  • The plugin directory gives the DGS a template that steps 1 and 2 do not have, for example a template that your template includes with fetch().
  • A store that has no plugin has only steps 1 and 2.
  • The DGS does not look in the project templates directory (Core::OPTION_TEMPLATE_PATH).
  • Each fetch() in a DGS template uses the same lookup. Thus you can replace one small part.
  • You can add directories to the lookup of one store. See Replace a template for one store.

The templates

All default templates are in the templates directory of the Theme package.

List

Template What it renders
store/list.phtml The list page: caption, filters, table, pager, general actions.
store/view/list/template.phtml The list for a wide screen.
store/view/list/mobile-template.phtml The list for a small screen.
store/view/list/table/head.phtml The table head.
store/view/list/table/body.phtml The table rows.
store/view/list/table/mobile-body.phtml The rows for a small screen.
store/view/list/parts/filters.phtml The filters.
store/view/list/parts/search-form.phtml The search form.
store/view/list/parts/search-result.phtml The search result line.
store/view/list/parts/row-actions.phtml The action buttons of one row.
store/view/list/parts/grouped-actions.phtml The actions for selected rows.
store/list_action.phtml One action link.
pager.php The pager. The theme supplies this template.

Variables of store/list.phtml:

Variable Content
$store The Store object.
$info The list data: URL, caption, actions, pager, options.
$data The rows from the storage.
$tableData The rows with the rendered cell values and row actions.
$filters The rendered filters.
$totals The totals of the aggregations.

Forms

Template What it renders
store/form.phtml The form of each action that has a form (insert, edit, info, remove, and others). The form shows in a dialog.
store/form_new.phtml The same form for an action with mode="new" (a full page, not a dialog).
store/form_items.phtml The fields of the form.
store/form_items_tabs.phtml The fields in tabs.
store/form_items_wizard.phtml The fields as a wizard.
store/form_items_widgets.phtml The fields in widgets.
store/form_items_hidden.phtml The hidden fields.
store/form_actions.phtml The buttons of the form.
store/form_remove.phtml The confirmation of the remove action.
store/form_section_disclaimer.phtml The disclaimer of a section.

Variables of a form template:

Variable Content
$store The Store object.
$actionInstance The action object.
$action The attributes of the action.
$what The action name, for example edit.
$items The form fields.
$sections The form sections.
$info The form data: caption, URL, token.
$values The values of the row.
$templateName The name of this template.
$appendContent HTML that a listener added after the form.

A listener for Store::EVENT_ON_FETCH_FORM can change each of these variables before the template gets them. See DGS events.

The form template of one action

Before an action renders its form, it looks for a template with the action name:

Action mode Template for the action, if it exists Default
dialog (default) store/form_<action name>.phtml store/form.phtml
new store/form_<action name>_new.phtml store/form_new.phtml

Example: the edit action uses store/form_edit.phtml, if a lookup directory has this file. This is how store/form_remove.phtml works.

Fields and filters

Directory Content
fields/ The templates of the field types: edit.phtml (the input in a form) and cell_value.phtml (the value in a list).
fields/<type>/ The templates of one field type, for example fields/select/edit.phtml.
filters/ The templates of the filter types: text.phtml, select.phtml, range.phtml, and others.

A field does not use the lookup of the StoreView. A field renders its templates from one directory. See Custom DGS Fields.

How to change the look of a DGS

First, find out if you need a template. Most changes do not need one.

You want to Use
Change captions, widths, the sequence of columns The attributes of the schema. See DGS.
Change the value in a cell The field attribute cellViewHandler. See Fields.
Change the color of a row highlights in the schema, or the event ListRowStyleEvent. See DGS events.
Add or remove fields, sections, or buttons of a form The event Store::EVENT_ON_FETCH_FORM. See DGS events.
Add HTML before or after the list The event Store::EVENT_ON_FETCH_LIST. See DGS events.
Change the input of one field A custom field class. See Custom DGS Fields.
Change the CSS The CSS file of the project. See Static Files.
Change the HTML structure of the list or of a form A template. See below.

Replace a template only when the other methods are not sufficient. A replaced template does not get the corrections of later framework versions.

Replace a template for one store

Add a directory of your plugin to the start of the lookup of the store. Do this after you make the store and before onRequest().

public function onDisplayOrders(Response &$response): bool
{
    $store = $this->createStoreInstance('orders');

    $view = $store->getView();

    $paths = $view->getTemplateLookupPaths();
    array_unshift($paths, $this->getPluginTemplatePath().'dgs/');
    $view->setTemplateLookupPaths($paths);

    $store->onRequest($response);

    return true;
}

Then put the template in that directory, with the same relative path as the default template:

plugins/
`-- Orders/
    `-- templates/
        `-- dgs/
            `-- store/
                |-- form_edit.phtml                 the form of the edit action
                `-- view/list/parts/
                    `-- row-actions.phtml           the action buttons of a row
  • The DGS uses your file for this store only.
  • The DGS uses the default file for each template that your directory does not have.
  • Replace the smallest part that contains the change. Do not copy store/list.phtml to change the row buttons.
  • Start from a copy of the default template. Keep the CSS classes and the id attributes. jimbo.js and the theme use them.

Replace a template for all stores

There are two methods.

In your own theme. Put the template in the templates directory of the theme, with the same relative path. This is step 1 of the lookup.

themes/
`-- mytheme/
    `-- templates/
        `-- store/
            `-- list.phtml

Use this method only in a theme that the project owns. Do not add files to a theme that comes from a package or a submodule (for example SmartAdmin).

With an event. Add a lookup directory to each store when the framework makes it. This method needs no change of a theme. Put the listener in the init.php of a plugin:

$this->addEventListener(Core::EVENT_ON_CREATE_STORE, function (FestiEvent &$event) {
    $store = $event->getTargetValueByKey('store');

    $view = $store->getView();

    $paths = $view->getTemplateLookupPaths();
    array_unshift($paths, __DIR__.'/templates/dgs/');
    $view->setTemplateLookupPaths($paths);
});

A different view class

The store option store_view (Store::OPTION_VIEW_MODEL) replaces the view object. Use it when a project needs a different render logic, not only different templates.

Rules

  • Do not edit the templates in vendor/ or in a theme that comes from a package.
  • Use schema attributes and events before you replace a template.
  • Replace the smallest template part.
  • Keep the CSS classes of the default templates (b-db-table, e-db-action, f-db-form, e-input). The JavaScript of the DGS finds the elements with them.
  • Use HUI components in your templates. Do not write the HTML of buttons and dialogs again.