Skip to content

Commit ecad024

Browse files
authoredJun 29, 2020
Added TwigStringColumn for simple inline templating (#146)
* Added TwigStringColumn for simple inline templating * docs for TwigStringColumn
1 parent ed00d89 commit ecad024

File tree

9 files changed

+99
-2
lines changed

9 files changed

+99
-2
lines changed
 

‎docs/source/index.html.md

+27
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,33 @@ template | string | Template path resolvable by the Symfony templating component
492492

493493
<aside class="warning">Keep in mind that for most simple use cases a decorated TextColumn will perform better than a full Twig template per row.</aside>
494494

495+
## TwigStringColumn
496+
497+
```php?start_inline=1
498+
$table->add('link', TwigStringColumn::class, [
499+
'template' => '<a href="{{ url(\'employee.edit\', {id: row.id}) }}">{{ row.firstName }} {{ row.lastName }}</a>',
500+
])
501+
```
502+
503+
This column type allows you to inline a Twig template as a string used to render the column's cells. The
504+
template is rendered using the main application context by injecting the main Twig service.
505+
Additionally, the `value` and `row` parameters are being filled by the cell value and the row
506+
level context respectively.
507+
508+
This column type requires `StringLoaderExtension` to be [enabled in your Twig environment](https://symfony.com/doc/4.4/reference/dic_tags.html#twig-extension).
509+
510+
```yaml
511+
services:
512+
Twig\Extension\StringLoaderExtension:
513+
tags: [twig.extension]
514+
```
515+
516+
Option | Type | Description
517+
------ | ---- | -----------
518+
template | string | Template content resolvable by the Symfony templating component. Required without default.
519+
520+
<aside class="warning">Keep in mind that for most simple use cases a decorated TextColumn will perform better than a full Twig template per row.</aside>
521+
495522
## Implementing custom columns
496523

497524
TBD.

‎src/Column/TwigColumn.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
class TwigColumn extends AbstractColumn
2525
{
2626
/** @var Environment */
27-
private $twig;
27+
protected $twig;
2828

2929
/**
3030
* TwigColumn constructor.
3131
*/
3232
public function __construct(Environment $twig = null)
3333
{
3434
if (null === ($this->twig = $twig)) {
35-
throw new MissingDependencyException('You must have TwigBundle installed to use ' . self::class);
35+
throw new MissingDependencyException('You must have TwigBundle installed to use ' . static::class);
3636
}
3737
}
3838

‎src/Column/TwigStringColumn.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* Symfony DataTables Bundle
5+
* (c) Omines Internetbureau B.V. - https://omines.nl/
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace Omines\DataTablesBundle\Column;
14+
15+
use Omines\DataTablesBundle\Exception\MissingDependencyException;
16+
use Twig\Environment;
17+
use Twig\Extension\StringLoaderExtension;
18+
19+
/**
20+
* TwigStringColumn.
21+
*
22+
* @author Marek Víger <marek.viger@gmail.com>
23+
*/
24+
class TwigStringColumn extends TwigColumn
25+
{
26+
/**
27+
* TwigStringColumn constructor.
28+
*/
29+
public function __construct(Environment $twig = null)
30+
{
31+
parent::__construct($twig);
32+
33+
if (!$this->twig->hasExtension(StringLoaderExtension::class)) {
34+
throw new MissingDependencyException('You must have StringLoaderExtension enabled to use ' . self::class);
35+
}
36+
}
37+
38+
protected function render($value, $context)
39+
{
40+
return $this->twig->render('@DataTables/Column/twig_string.html.twig', [
41+
'column_template' => $this->getTemplate(),
42+
'row' => $context,
43+
'value' => $value,
44+
]);
45+
}
46+
}

‎src/Resources/config/services.xml

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<service id="Omines\DataTablesBundle\Column\TwigColumn">
2121
<argument type="service" id="twig" on-invalid="null" />
2222
</service>
23+
<service id="Omines\DataTablesBundle\Column\TwigStringColumn">
24+
<argument type="service" id="twig" on-invalid="null" />
25+
</service>
2326

2427
<!-- Exporters -->
2528
<service id="Omines\DataTablesBundle\Exporter\DataTableExporterCollection">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{- include(template_from_string(column_template)) -}}

‎tests/Fixtures/AppBundle/Controller/PlainController.php

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Omines\DataTablesBundle\Column\DateTimeColumn;
1818
use Omines\DataTablesBundle\Column\TextColumn;
1919
use Omines\DataTablesBundle\Column\TwigColumn;
20+
use Omines\DataTablesBundle\Column\TwigStringColumn;
2021
use Omines\DataTablesBundle\DataTable;
2122
use Omines\DataTablesBundle\DataTableFactory;
2223
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -45,6 +46,9 @@ public function tableAction(Request $request, DataTableFactory $dataTableFactory
4546
},
4647
])
4748
->add('employer', TextColumn::class, ['field' => 'company.name'])
49+
->add('link', TwigStringColumn::class, [
50+
'template' => '<a href="{{ url(\'employee.edit\', {id: row.id}) }}">{{ row.firstName }} {{ row.lastName }}</a>',
51+
])
4852
->add('buttons', TwigColumn::class, [
4953
'template' => '@App/buttons.html.twig',
5054
'data' => '<button>Click me</button>',

‎tests/Fixtures/services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ services:
2121
arguments:
2222
- '@doctrine.system_cache_pool'
2323

24+
twig.extension.string_loader:
25+
class: Twig\Extension\StringLoaderExtension
26+
tags:
27+
- { name: twig.extension }
28+
2429
test.Omines\DataTablesBundle\Exporter\DataTableExporterCollection: '@Omines\DataTablesBundle\Exporter\DataTableExporterCollection'
2530

2631
Tests\Fixtures\AppBundle\DataTable\Exporter\TxtExporter:

‎tests/Functional/FunctionalTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testPlainDataTable()
6464
$this->assertSame('LastName94', $sample->lastName);
6565
$this->assertEmpty($sample->employedSince);
6666
$this->assertSame('FirstName94 &lt;img src=&quot;https://symfony.com/images/v5/logos/sf-positive.svg&quot;&gt; LastName94', $sample->fullName);
67+
$this->assertSame('<a href="http://localhost/employee/95">FirstName94 LastName94</a>', $sample->link);
6768
$this->assertRegExp('#href="/employee/[0-9]+"#', $sample->buttons);
6869

6970
$this->assertSame('04-07-2016', $json->data[6]->employedSince);

‎tests/Unit/ColumnTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
use Omines\DataTablesBundle\Column\NumberColumn;
1919
use Omines\DataTablesBundle\Column\TextColumn;
2020
use Omines\DataTablesBundle\Column\TwigColumn;
21+
use Omines\DataTablesBundle\Column\TwigStringColumn;
2122
use Omines\DataTablesBundle\DataTable;
2223
use Omines\DataTablesBundle\Exception\MissingDependencyException;
2324
use Omines\DataTablesBundle\Exporter\DataTableExporterManager;
2425
use PHPUnit\Framework\TestCase;
2526
use Symfony\Component\EventDispatcher\EventDispatcher;
27+
use Twig\Environment as Twig;
2628

2729
/**
2830
* ColumnTest.
@@ -142,6 +144,14 @@ public function testTwigDependencyDetection()
142144
new TwigColumn();
143145
}
144146

147+
public function testTwigStringColumnExtensionDetection()
148+
{
149+
$this->expectException(MissingDependencyException::class);
150+
$this->expectExceptionMessage('You must have StringLoaderExtension enabled to use');
151+
152+
new TwigStringColumn($this->createMock(Twig::class));
153+
}
154+
145155
private function createDataTable(): DataTable
146156
{
147157
return new DataTable($this->createMock(EventDispatcher::class), $this->createMock(DataTableExporterManager::class));

0 commit comments

Comments
 (0)