Skip to content

Commit 84e948f

Browse files
committed
Add strtotime filter
1 parent 93920b7 commit 84e948f

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed

README.md

+34-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PHP Syntax for Twig
33

44
[![Build Status](https://img.shields.io/travis/com/squirrelphp/twig-php-syntax.svg)](https://travis-ci.com/squirrelphp/twig-php-syntax) [![Test Coverage](https://api.codeclimate.com/v1/badges/56ed1e15544f2bb7609e/test_coverage)](https://codeclimate.com/github/squirrelphp/twig-php-syntax/test_coverage) ![PHPStan](https://img.shields.io/badge/style-level%208-success.svg?style=flat-round&label=phpstan) [![Packagist Version](https://img.shields.io/packagist/v/squirrelphp/twig-php-syntax.svg?style=flat-round)](https://packagist.org/packages/squirrelphp/twig-php-syntax) [![PHP Version](https://img.shields.io/packagist/php-v/squirrelphp/twig-php-syntax.svg)](https://packagist.org/packages/squirrelphp/twig-php-syntax) [![Software License](https://img.shields.io/badge/license-MIT-success.svg?style=flat-round)](LICENSE)
55

6-
Enables syntax known from PHP in Twig, so PHP developers can more easily create and edit twig templates. This is especially useful for small projects, where the PHP developers end up writing twig templates and it is not worth it to have a slightly different syntax in your templates.
6+
Enables syntax known from PHP in Twig, so PHP developers can more easily create and edit Twig templates. This is especially useful for small projects, where the PHP developers end up writing Twig templates and it is not worth it to have a slightly different syntax in your templates.
77

88
Installation
99
------------
@@ -35,7 +35,7 @@ This will be shown
3535
{% endif %}
3636
3737
{% if 1 is same as(1) %}
38-
Same as above but with standard twig syntax
38+
Same as above but with standard Twig syntax
3939
{% endif %}
4040
4141
@@ -53,9 +53,29 @@ somevariable either is not a string or does not equal "test"
5353
{% endif %}
5454
```
5555

56+
### strtotime filter
57+
58+
Comparing timestamps in templates when the data only has (date) strings is a bit cumbersome in Twig, as there is no `strtotime` filter - this library adds it exactly as it is in PHP:
59+
60+
```twig
61+
{% if "2018-05-05"|strtotime > "2017-05-05"|strtotime %}
62+
This is always true, as 2018 results in a larger timestamp integer than 2017
63+
{% endif %}
64+
65+
{% if post.date|strtotime > otherpost.date|strtotime %}
66+
Compares the dates of post and otherpost. strtotime returns an integer
67+
or throws an InvalidArgumentException if strtotime returns false
68+
{% endif %}
69+
70+
{# Sets next thursday as a timestamp variable, but also sets "now"
71+
like in strtotime in PHP to define from where the timestamp is
72+
calculated if it is a relative date and not an absolute date #}
73+
{% set nextThusday = "next Thursday"|strtotime(now=sometimestamp) %}
74+
```
75+
5676
### foreach loops
5777

58-
Twig uses `for` to create loops, with a slightly different syntax compared to `foreach` in PHP. With this library `foreach` becomes available in twig with the same syntax as in PHP:
78+
Twig uses `for` to create loops, with a slightly different syntax compared to `foreach` in PHP. With this library `foreach` becomes available in Twig with the same syntax as in PHP:
5979

6080
```twig
6181
{% foreach list as sublist %}
@@ -68,7 +88,7 @@ Internally it behaves the exact same way as `for`: it actually creates ForNode e
6888

6989
### break and continue
7090

71-
Sometimes it can be convenient to break loops in twig, yet there is no native support for it. This library adds `break` and `continue` and they work exactly as in PHP:
91+
Sometimes it can be convenient to break loops in Twig, yet there is no native support for it. This library adds `break` and `continue` and they work exactly as in PHP:
7292

7393
```twig
7494
{% foreach list as entry %}
@@ -90,7 +110,7 @@ You can use `break` with a number to break out of multiple loops, just like in P
90110
{% endforeach %}
91111
```
92112

93-
While you can often circumvent the usage of `break` and `continue` in twig, it sometimes leads to additional nesting and more complicated code. Just one `break` or `continue` can clarify behavior and intent in these instances. Yet I would advise to use `break` and `continue` sparingly.
113+
While you can often circumvent the usage of `break` and `continue` in Twig, it sometimes leads to additional nesting and more complicated code. Just one `break` or `continue` can clarify behavior and intent in these instances. Yet I would advise to use `break` and `continue` sparingly.
94114

95115
### Variable type tests (string, array, true, callable, etc.)
96116

@@ -116,38 +136,38 @@ Adds tests known from PHP, so you can test a value for being:
116136
{% if someflag is false %} {# instead of {% if someflag is same as(false) %} #}
117137
{% endif %}
118138
119-
{% if somevar is string %} {# no equivalent in twig %} #}
139+
{% if somevar is string %} {# no equivalent in Twig %} #}
120140
{% endif %}
121141
122-
{% if somevar is scalar %} {# no equivalent in twig %} #}
142+
{% if somevar is scalar %} {# no equivalent in Twig %} #}
123143
{% endif %}
124144
125-
{% if somevar is object %} {# no equivalent in twig %} #}
145+
{% if somevar is object %} {# no equivalent in Twig %} #}
126146
{% endif %}
127147
128-
{% if somevar is integer %} {# no equivalent in twig %} #}
148+
{% if somevar is integer %} {# no equivalent in Twig %} #}
129149
{% endif %}
130150
{% if somevar is int %} {# same as integer test above, alternate way to write it %} #}
131151
{% endif %}
132152
133-
{% if somevar is float %} {# no equivalent in twig %} #}
153+
{% if somevar is float %} {# no equivalent in Twig %} #}
134154
{% endif %}
135155
136-
{% if somevar is callable %} {# no equivalent in twig %} #}
156+
{% if somevar is callable %} {# no equivalent in Twig %} #}
137157
{% endif %}
138158
139-
{% if somevar is boolean %} {# no equivalent in twig %} #}
159+
{% if somevar is boolean %} {# no equivalent in Twig %} #}
140160
{% endif %}
141161
{% if somevar is bool %} {# same as boolean test above, alternate way to write it %} #}
142162
{% endif %}
143163
144-
{% if somevar is array %} {# no equivalent in twig %} #}
164+
{% if somevar is array %} {# no equivalent in Twig %} #}
145165
{% endif %}
146166
```
147167

148168
### && and ||
149169

150-
If you want to make expressions even more like PHP, you can use `&&` instead of `and` and `||` instead of `or`. This might be the least useful part of this library, as `and` and `or` are already short and clear, yet it is another easily remedied difference between twig and PHP, and `&&` and `||` can be easier to spot in comparison to `and` and `or`.
170+
If you want to make expressions even more like PHP, you can use `&&` instead of `and` and `||` instead of `or`. This might be the least useful part of this library, as `and` and `or` are already short and clear, yet it is another easily remedied difference between Twig and PHP, and `&&` and `||` can be easier to spot in comparison to `and` and `or`.
151171

152172
```twig
153173
{% if someflag === true && otherflag === false %}

src/PhpSyntaxExtension.php

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Twig\Extension\AbstractExtension;
2222
use Twig\Node\Expression\Binary\AndBinary;
2323
use Twig\Node\Expression\Binary\OrBinary;
24+
use Twig\TwigFilter;
2425
use Twig\TwigTest;
2526

2627
class PhpSyntaxExtension extends AbstractExtension
@@ -34,6 +35,23 @@ public function getTokenParsers(): array
3435
];
3536
}
3637

38+
public function getFilters()
39+
{
40+
return [
41+
new TwigFilter('strtotime', function (string $time, ?int $now = null): int {
42+
$timestamp = \strtotime($time, $now ?? time());
43+
44+
if ($timestamp === false) {
45+
throw new \InvalidArgumentException(
46+
'Given time string for strtotime seems to be invalid: ' . $time
47+
);
48+
}
49+
50+
return $timestamp;
51+
}),
52+
];
53+
}
54+
3755
public function getTests(): array
3856
{
3957
return [

tests/Fixtures/strtotime.test

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
"strtotime" filter test
3+
--TEMPLATE--
4+
{% if "2020-06-06T18:38:14+00:00"|strtotime is same as(1591468694) %}
5+
1
6+
{% endif %}
7+
{% if "2020-06-06T18:38:14+00:00"|strtotime|date('Y-m-d') is same as("2020-06-06") %}
8+
2
9+
{% endif %}
10+
{% if "2020-06-06T18:38:14+00:00"|strtotime is integer %}
11+
3
12+
{% endif %}
13+
{% if "2020-06-06T18:38:14+00:00"|strtotime is string %}
14+
4
15+
{% endif %}
16+
--DATA--
17+
return [];
18+
--EXPECT--
19+
1
20+
2
21+
3
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
"strtotime" filter test
3+
--TEMPLATE--
4+
{% if "invalid"|strtotime is integer %}
5+
3
6+
{% endif %}
7+
--DATA--
8+
return [];
9+
--EXCEPTION--
10+
Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("Given time string for strtotime seems to be invalid: invalid") in "index.twig" at line 2.

0 commit comments

Comments
 (0)