Skip to content

Commit 14cbd0b

Browse files
committed
Initial first release
0 parents  commit 14cbd0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4245
-0
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
5+
end_of_line = lf
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
insert_final_newline = false
11+
12+
[*.php]
13+
14+
end_of_line = lf
15+
charset = utf-8
16+
indent_style = space
17+
indent_size = 4
18+
insert_final_newline = true
19+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/tests export-ignore
2+
/examples export-ignore
3+
/docker export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/phpcs.xml.dist export-ignore
8+
/captainhook.json export-ignore
9+
/.travis.yml export-ignore
10+
/ruleset.xml export-ignore
11+
/.editorconfig export-ignore
12+
/phpstan.neon export-ignore
13+
/phpstan_base.neon export-ignore
14+
/phpstan-baseline.neon export-ignore
15+
/psalm.xml export-ignore
16+
/psalm-baseline.xml export-ignore
17+
/vendor-bin export-ignore

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.idea
2+
/composer.lock
3+
/vendor
4+
/vendor-bin/**/vendor
5+
/.phpunit*
6+
/tests/_output
7+
/tests/_reports
8+
/build

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Andreas Leathley
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
PHP Syntax for Twig
2+
===================
3+
4+
[![Build Status](https://img.shields.io/travis/com/squirrelphp/twig-php-syntax.svg)](https://travis-ci.com/squirrelphp/twig-php-syntax) ![PHPStan](https://img.shields.io/badge/style-level%207-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)
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.

captainhook.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"commit-msg": {
3+
"enabled": true,
4+
"actions": [
5+
{
6+
"action": "\\CaptainHook\\App\\Hook\\Message\\Action\\Beams",
7+
"options": {
8+
"subjectLength": 50,
9+
"bodyLineLength": 72
10+
},
11+
"conditions": []
12+
}
13+
]
14+
},
15+
"pre-push": {
16+
"enabled": false,
17+
"actions": []
18+
},
19+
"pre-commit": {
20+
"enabled": true,
21+
"actions": [
22+
{
23+
"action": "\\CaptainHook\\App\\Hook\\PHP\\Action\\Linting",
24+
"options": [],
25+
"conditions": []
26+
},
27+
{
28+
"action": "composer phpunit",
29+
"options": [],
30+
"conditions": []
31+
},
32+
{
33+
"action": "composer phpstan",
34+
"options": [],
35+
"conditions": []
36+
},
37+
{
38+
"action": "composer psalm",
39+
"options": [],
40+
"conditions": []
41+
},
42+
{
43+
"action": "composer phpcs",
44+
"options": [],
45+
"conditions": []
46+
}
47+
]
48+
},
49+
"prepare-commit-msg": {
50+
"enabled": false,
51+
"actions": []
52+
},
53+
"post-commit": {
54+
"enabled": false,
55+
"actions": []
56+
},
57+
"post-merge": {
58+
"enabled": false,
59+
"actions": []
60+
},
61+
"post-checkout": {
62+
"enabled": false,
63+
"actions": []
64+
}
65+
}

composer.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "squirrelphp/twig-php-syntax",
3+
"type": "library",
4+
"description": "Adds common PHP syntax to twig templates, like ===, foreach and continue/break.",
5+
"keywords": [
6+
"php",
7+
"twig",
8+
"syntax",
9+
"foreach"
10+
],
11+
"homepage": "https://github.com/squirrelphp/twig-php-syntax",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Andreas Leathley",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"php": "^7.4",
21+
"twig/twig": "^3.0"
22+
},
23+
"require-dev": {
24+
"bamarni/composer-bin-plugin": "^1.3",
25+
"captainhook/plugin-composer": "^4.0"
26+
},
27+
"config": {
28+
"sort-packages": false,
29+
"platform": {
30+
"php": "7.4"
31+
}
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Squirrel\\TwigPhpSyntax\\": "src/"
36+
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Squirrel\\TwigPhpSyntax\\Tests\\": "tests/"
41+
}
42+
},
43+
"scripts": {
44+
"post-install-cmd": ["@composer bin all install --ansi"],
45+
"post-update-cmd": ["@composer bin all update --ansi"],
46+
"phpstan": "vendor/bin/phpstan analyse",
47+
"phpstan_base": "vendor/bin/phpstan analyse --configuration phpstan_base.neon --error-format baselineNeon > phpstan-baseline.neon",
48+
"psalm": "vendor/bin/psalm --show-info=false --diff",
49+
"psalm_full": "vendor/bin/psalm --show-info=false",
50+
"psalm_base": "vendor/bin/psalm --set-baseline=psalm-baseline.xml",
51+
"phpunit": "vendor/bin/phpunit --colors=always",
52+
"phpunit_clover": "vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml",
53+
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src tests",
54+
"phpcsfix": "vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --cache src tests",
55+
"codecoverage": "vendor/bin/phpunit --coverage-html tests/_reports",
56+
"binupdate": "@composer bin all update --ansi",
57+
"bininstall": "@composer bin all install --ansi"
58+
}
59+
}

phpstan-baseline.neon

Whitespace-only changes.

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
4+
parameters:
5+
level: 8
6+
paths:
7+
- src
8+
checkMissingIterableValueType: false

phpstan_base.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- src
5+
checkMissingIterableValueType: false

0 commit comments

Comments
 (0)