Skip to content

Commit df21663

Browse files
authored
Merge pull request #24 from dipcode-software/feat/retrocompability
Added retrocompability between Twig v1 and v2
2 parents 953502c + 2f1a757 commit df21663

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
- `data` and `files` were wrongly exposed on `Form` class. Changed visibility to private and added `getData` and `getFiles` methods.
1010
- Allow non-required file fields
1111
- Fixed choice field values check
12+
- Added retrocompability between Twig v1 and v2
1213

1314
## [2.0.3] - 2017-12-13
1415
### Added

src/Renderers/TwigRenderer.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,33 @@ public function __construct(array $templates_dirs, array $options = array())
2727
{
2828
$loaders = new ChainLoader();
2929

30+
/* To maintain compatibility with non-PSR4 versions of Twig */
31+
if (class_exists('Twig_Loader_Filesystem')) {
32+
$class = 'Twig_Loader_Filesystem';
33+
$envClass = 'Twig_Environment';
34+
} else {
35+
$class = 'FilesystemLoader';
36+
$envClass = 'Environment';
37+
}
38+
3039
foreach ($templates_dirs as $template_dir) {
31-
$loaders->addLoader(new FilesystemLoader($template_dir));
40+
$loaders->addLoader(new $class($template_dir));
3241
}
3342

34-
$this->twig = new Environment($loaders, $options);
43+
$this->twig = new $envClass($loaders, $options);
3544
$this->setFilters();
3645
}
3746

3847
public function setFilters()
3948
{
40-
$filter_merge_str = new TwigFilter('merge_str', function ($attrs, array $options = array()) {
49+
/* To maintain compatibility with non-PSR4 versions of Twig */
50+
if (class_exists('Twig_SimpleFilter')) {
51+
$class = 'Twig_SimpleFilter';
52+
} else {
53+
$class = 'TwigFilter';
54+
}
55+
56+
$filter_merge_str = new $class('merge_str', function ($attrs, array $options = array()) {
4157
$key = $options[0];
4258
$value = $options[1];
4359

0 commit comments

Comments
 (0)