Templates ========= The Psi Grid component does not include any templates by default. This is because templating is highly specific - it is not possible to support all the different CSS frameworks which are in the wild. Often in a project you will discover that you will override templates anyway, which leads to templates being distributed between your project and vendor libraries, increasing complexity. We recommend that you code your own templates. This is made easier by the fact that the grid system will generate a **view** which can be traversed and contains most of the logic that you will require. This chapter will expose you to the Grid View API and show some example templates. Grid View API ------------- You create the grid view from the `Grid` object: .. code-block:: php createGrid(MyObject::class, [ /** grid config */]); // create the grid view $view = $grid->createView(); // paginator $paginator = $view->getPaginator(); // Psi\Component\Grid\View\Paginator $paginator->getPageSize(); $paginator->getCurrentPage(); $paginator->getNumberOfRecords(); $paginator->isLastPage; $paginator->getUrlParametersForPage(2); // filter bar $filterBar = $view->getFilter(); // Psi\Component\Grid\View\FilterBar $filterBar->getForm(); // return the Symfony form view instance $filterBar->getUrlParametersForReset(); $filterBar->getUrlParametersForFilter(); // table $table = $view->getTable(); $table->getHeaders(); // Psi\Component\Grid\View\Header[] // table body $body = $table->getBody(); // iterable Psi\Component\Grid\View\Body /** @var Psi\Component\Grid\View\Row */ foreach ($body as $row) { /** @var Psi\Component\Grid\View\Cell */ foreach ($row as $cell) { $cell->getContext(); // return the context (e.g. the object / data set of the row) $cell->getTemplate(); // return template reference to render the cell $cell->value; // property containing value of cell $cell->parameters; // array containing parameters of the cell } } Example Twig Template --------------------- The following Twig template is intended to help you get started, it uses Bootstrap 3 and Symfony routing functions: .. code-block:: html+twig
{# create a form here, as actions will be performed based on selected cells #}
{# Display the grid #} {% for header in grid.table.headers %} {% endfor %} {% for row in grid.table.body %} {% for cell in row %} {# You will need to create each cell template as required #} {% include "grid/" ~ cell.template ~ "Cell.twig" %} {% endfor %} {# Maybe you want to add more cells here, e.g. for actions #} {% endfor %}
{{ header.label|trans()|ucfirst }} {% if header.canBeSorted %} {% if false == header.sorted %} {% else %} {% endif %} {% endif %}
{# show the bulk actions #}
{% if grid.actionBar.availableActionNames %} {% endif %}
{# see example pager below #} {% include "pager.twig" %}
{# show the filter bar #}
The following a simple pager template: .. code-block:: html+twig {% set pager = grid.paginator %}