Skip to content
This repository has been archived by the owner on Dec 31, 2019. It is now read-only.

Custom renderer

benjaminu edited this page Dec 5, 2012 · 8 revisions


Twig renderers

To set your own column structure, you can use a custom twig renderer as below: In this example you can find how to set the use of the default twig renderer for action fields which you can override as your own needs.

/**
 * set datatable configs
 * 
 * @return \Ali\DatatableBundle\Util\Datatable
 */
private function _datatable()
{
    $datatable = $this->get('datatable');
    return $datatable->setEntity('XXXMyBundle:Entity', 'x')
        ->setFields(
        array(
            'label of field1' => 'x.field1',
            'label of field2' => 'x.field2',
            '_identifier_' => 'x.id')
        )
        ->setRenderers(
            array(
                2 => array(
                    'view' => 'AliDatatableBundle:Renderers:_actions.html.twig',
                    'params' => array(
                        'edit_route'            => 'route_edit',
                        'delete_route'          => 'route_delete',
                        'delete_form_prototype' => $datatable->getPrototype('delete_form')
                     ),
                ),
            )
        )
        ->setHasAction(true);
}

PHP Closures

Assuming the example above, you can set your custom fields renderer using PHP Closures.

/**
 * set datatable configs
 * 
 * @return \Ali\DatatableBundle\Util\Datatable
 */
private function _datatable()
{
    $controller_instance = $this;
    return $this->get('datatable')
        ->setEntity('XXXMyBundle:Entity', 'x') // replace 'XXXMyBundle:Entity' by your entity
        ->setFields(
            array(
                'Name'         => 'x.name', // Declaration for fields: 
                'Adress'       => 'x.adress', // 'label' => 'alias.field_attribute_for_dql'
                '_identifier_' => 'x.id' // you have to put the identifier field without label. Do not replace the "_identifier_"
            )                          
        )
        ->setRenderer(
            function(&$data) use ($controller_instance) {
                foreach ($data as $key => $value) {
                    if ($key == 1) { // 1 => adress field
                        $data[$key] = $controller_instance
                            ->get('templating')
                            ->render(
                                'XXXMyBundle:Module:_grid_entity.html.twig', 
                                array('data' => $value)
                            );
                    }
                }
            }
        )
        ->setOrder('x.created', 'desc')  // it's also possible to set the default order
        ->setHasAction(true); // you can disable action column from here by setting "false".
}

Screenshot