Skip to content

Commit 8e9ad21

Browse files
authored
Add Twig extension for Haste formatter (#235)
1 parent fe59981 commit 8e9ad21

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ services:
4040
$entityManager: '@?doctrine.orm.entity_manager'
4141
public: true
4242

43+
Codefog\HasteBundle\Twig\HasteExtension: ~
44+
4345
# StringParser
4446
Codefog\HasteBundle\StringParser:
4547
public: true

docs/Formatter.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ $this->formatter->dcaLabel('tl_news', 'headline');
2121
// Display the tl_news.source formatted value
2222
$this->formatter->dcaValue('tl_news', 'source', $newsModel->source);
2323
```
24+
25+
26+
## Twig Usage
27+
28+
You can also use the formatting helper inside Twig templates.
29+
30+
```twig
31+
<table>
32+
<thead>
33+
<tr>
34+
<th>{{ dca_label('tl_news', 'headline') }}</th>
35+
<th>{{ dca_label('tl_news', 'published') }}</th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
{% for item in news %}
40+
<tr>
41+
<td>{{ dca_value('tl_news', 'headline', item.headline) }}</td>
42+
<td>{{ dca_value('tl_news', 'published', item.published) }}</td>
43+
</tr>
44+
{% endfor %}
45+
</tbody>
46+
</table>
47+
```

src/Twig/HasteExtension.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codefog\HasteBundle\Twig;
6+
7+
use Codefog\HasteBundle\Formatter;
8+
use Twig\Extension\AbstractExtension;
9+
use Twig\TwigFunction;
10+
11+
class HasteExtension extends AbstractExtension
12+
{
13+
public function __construct(private readonly Formatter $formatter)
14+
{
15+
}
16+
17+
public function getFunctions(): array
18+
{
19+
return [
20+
new TwigFunction('dca_label', $this->formatter->dcaLabel(...)),
21+
new TwigFunction('dca_value', $this->formatter->dcaValue(...)),
22+
];
23+
}
24+
}

0 commit comments

Comments
 (0)