You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
7
7
8
8
Installation
9
9
------------
@@ -35,7 +35,7 @@ This will be shown
35
35
{% endif %}
36
36
37
37
{% if 1 is same as(1) %}
38
-
Same as above but with standard twig syntax
38
+
Same as above but with standard Twig syntax
39
39
{% endif %}
40
40
41
41
@@ -53,9 +53,29 @@ somevariable either is not a string or does not equal "test"
53
53
{% endif %}
54
54
```
55
55
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
+
56
76
### foreach loops
57
77
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:
59
79
60
80
```twig
61
81
{% foreach list as sublist %}
@@ -68,7 +88,7 @@ Internally it behaves the exact same way as `for`: it actually creates ForNode e
68
88
69
89
### break and continue
70
90
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:
72
92
73
93
```twig
74
94
{% 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
90
110
{% endforeach %}
91
111
```
92
112
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.
94
114
95
115
### Variable type tests (string, array, true, callable, etc.)
96
116
@@ -116,38 +136,38 @@ Adds tests known from PHP, so you can test a value for being:
116
136
{% if someflag is false %} {# instead of {% if someflag is same as(false) %} #}
117
137
{% endif %}
118
138
119
-
{% if somevar is string %} {# no equivalent in twig %} #}
139
+
{% if somevar is string %} {# no equivalent in Twig %} #}
120
140
{% endif %}
121
141
122
-
{% if somevar is scalar %} {# no equivalent in twig %} #}
142
+
{% if somevar is scalar %} {# no equivalent in Twig %} #}
123
143
{% endif %}
124
144
125
-
{% if somevar is object %} {# no equivalent in twig %} #}
145
+
{% if somevar is object %} {# no equivalent in Twig %} #}
126
146
{% endif %}
127
147
128
-
{% if somevar is integer %} {# no equivalent in twig %} #}
148
+
{% if somevar is integer %} {# no equivalent in Twig %} #}
129
149
{% endif %}
130
150
{% if somevar is int %} {# same as integer test above, alternate way to write it %} #}
131
151
{% endif %}
132
152
133
-
{% if somevar is float %} {# no equivalent in twig %} #}
153
+
{% if somevar is float %} {# no equivalent in Twig %} #}
134
154
{% endif %}
135
155
136
-
{% if somevar is callable %} {# no equivalent in twig %} #}
156
+
{% if somevar is callable %} {# no equivalent in Twig %} #}
137
157
{% endif %}
138
158
139
-
{% if somevar is boolean %} {# no equivalent in twig %} #}
159
+
{% if somevar is boolean %} {# no equivalent in Twig %} #}
140
160
{% endif %}
141
161
{% if somevar is bool %} {# same as boolean test above, alternate way to write it %} #}
142
162
{% endif %}
143
163
144
-
{% if somevar is array %} {# no equivalent in twig %} #}
164
+
{% if somevar is array %} {# no equivalent in Twig %} #}
145
165
{% endif %}
146
166
```
147
167
148
168
### && and ||
149
169
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`.
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