|
| 1 | +PHP Syntax for Twig |
| 2 | +=================== |
| 3 | + |
| 4 | +[](https://travis-ci.com/squirrelphp/twig-php-syntax)  [](https://packagist.org/packages/squirrelphp/twig-php-syntax) [](https://packagist.org/packages/squirrelphp/twig-php-syntax) [](LICENSE) |
| 5 | + |
| 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 | + |
| 8 | +Installation |
| 9 | +------------ |
| 10 | + |
| 11 | + composer require squirrelphp/twig-php-syntax |
| 12 | + |
| 13 | +Configuration |
| 14 | +------------- |
| 15 | + |
| 16 | +Add PhpSyntaxExtension to Twig: |
| 17 | + |
| 18 | +```php |
| 19 | +$twig = new \Twig\Environment($loader); |
| 20 | +$twig->addExtension(new \Squirrel\TwigPhpSyntax\PhpSyntaxExtension()); |
| 21 | +``` |
| 22 | + |
| 23 | +You can also have a look at the extension definition and create your own extension class to only include some of the features, if you do not like all of them. |
| 24 | + |
| 25 | +Features |
| 26 | +-------- |
| 27 | + |
| 28 | +### === / !== strict comparison operators |
| 29 | + |
| 30 | +Twig has the `same as` operator, or `==` which actually is not the same as `==` in PHP (it is a bit stricter). After being used to `===` and `!==` in PHP it is handy to just them for clarity. |
| 31 | + |
| 32 | +```twig |
| 33 | +{% if 1 === 1 %} |
| 34 | +{% endif %} |
| 35 | +
|
| 36 | +{% if somevariable === "test" %} |
| 37 | +{% endif %} |
| 38 | +
|
| 39 | +{% if somevariable !== "test" %} |
| 40 | +{% endif %} |
| 41 | +``` |
| 42 | + |
| 43 | +### foreach loops |
| 44 | + |
| 45 | +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: |
| 46 | + |
| 47 | +```twig |
| 48 | +{% foreach list as sublist %} |
| 49 | + {% foreach sublist as key => value %} |
| 50 | + {% endforeach %} |
| 51 | +{% endforeach %} |
| 52 | +``` |
| 53 | + |
| 54 | +Internally it behaves the exact same way as `for`: it actually creates ForNode elements, so you have the same functionality like in `for` loops, including the `loop` variable. |
| 55 | + |
| 56 | +### break and continue |
| 57 | + |
| 58 | +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: |
| 59 | + |
| 60 | +```twig |
| 61 | +{% foreach list as entry %} |
| 62 | + {% if loop.index > 10 %} |
| 63 | + {% break %} |
| 64 | + {% endif %} |
| 65 | +{% endforeach %} |
| 66 | +``` |
| 67 | + |
| 68 | +You can use `break` with a number to break out of multiple loops, just like in PHP: (`continue` does not support this) |
| 69 | + |
| 70 | +```twig |
| 71 | +{% foreach list as sublist %} |
| 72 | + {% foreach sublist as entry %} |
| 73 | + {% if loop.index > 10 %} |
| 74 | + {% break 2 %} {# breaks out of both loops #} |
| 75 | + {% endif %} |
| 76 | + {% endforeach %} |
| 77 | +{% endforeach %} |
| 78 | +``` |
| 79 | + |
| 80 | +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. |
| 81 | + |
| 82 | +### is true / is false tests |
| 83 | + |
| 84 | +Adds a strict true/false test, so expressions become a bit more readable: |
| 85 | + |
| 86 | +```twig |
| 87 | +{% if someflag is true %} {# instead of {% if someflag is same as(true) %} #} |
| 88 | +{% endif %} |
| 89 | +
|
| 90 | +{% if someflag is false %} {# instead of {% if someflag is same as(false) %} #} |
| 91 | +{% endif %} |
| 92 | +``` |
| 93 | + |
| 94 | +### && and || |
| 95 | + |
| 96 | +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. |
0 commit comments