Skip to content
Permalink
Browse files

Merge pull request #105 from l0gicgate/3.x

Port for Slim 4 Usage
  • Loading branch information...
l0gicgate committed Apr 28, 2019
2 parents 06ef39b + 80cc982 commit fed1bec854113fa0650d8126da9284f58e78688c
@@ -1,2 +1,5 @@
.idea
composer.lock
clover.xml
coverage
vendor
@@ -2,13 +2,25 @@ language: php

dist: trusty

php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm
matrix:
include:
- php: 7.1
env: ANALYSIS='true'
- php: 7.2
- php: 7.3
- php: nightly
allow_failures:
- php: nightly

before_script: composer install
before_script:
- composer require php-coveralls/php-coveralls:^2.1.0
- composer install -n

script: vendor/bin/phpunit --coverage-text --configuration phpunit.xml.dist
script:
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
- vendor/bin/phpunit
- vendor/bin/phpcs
- vendor/bin/phpstan analyse Slim

after_success:
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/php-coveralls --coverage_clover=clover.xml -v ; fi
@@ -9,33 +9,35 @@ This is a Slim Framework view helper built on top of the Twig templating compone
Via [Composer](https://getcomposer.org/)

```bash
$ composer require slim/twig-view
$ composer require slim/twig-view:3.0.0-alpha
```

Requires Slim Framework 3 and PHP 5.5.0 or newer.
Requires Slim Framework 4 and PHP 7.1 or newer.

## Usage

```php
// Create Slim app
$app = new \Slim\App();
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;
use Slim\Views\TwigMiddleware;
// Fetch DI Container
$container = $app->getContainer();
require __DIR__ . '/vendor/autoload.php';
// Register Twig View helper
$container['view'] = function ($c) {
$view = new \Slim\Views\Twig('path/to/templates', [
'cache' => 'path/to/cache'
]);
// Instantiate and add Slim specific extension
$router = $c->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$view->addExtension(new \Slim\Views\TwigExtension($router, $uri));
// Create Container
$container = new Container();
AppFactory::setContainer($container);
// Create App
$app = new AppFactory::create();
return $view;
};
// Add Twig-View Middleware
$basePath = '/base-path';
$routeParser = $app->getRouteCollector()->getRouteParser();
$twig = new Twig('path/to/templates', ['cache' => 'path/to/cache']);
$twigMiddleware = new TwigMiddleware($twig, $container, $routeParser, $basePath);
$app->add($twigMiddleware);
// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
@@ -61,29 +63,33 @@ $app->run();

`TwigExtension` provides these functions to your Twig templates:

* `path_for()` - returns the URL for a given route.
* `base_url()` - returns the `Uri` object's base URL.
* `is_current_path()` - returns true is the provided route name and parameters are valid for the current path.
* `current_path()` - renders the current path, with or without the query string.
* `url_for()` - returns the URL for a given route. e.g.: /hello/world
* `full_url_for()` - returns the URL for a given route. e.g.: http://www.example.com/hello/world
* `is_current_url()` - returns true is the provided route name and parameters are valid for the current path.
* `current_url()` - returns the current path, with or without the query string.
* `get_uri()` - returns the `UriInterface` object from the incoming `ServerRequestInterface` object


You can use `path_for` to generate complete URLs to any Slim application named route and use `is_current_path` to determine if you need to mark a link as active as shown in this example Twig template:
You can use `url_for` to generate complete URLs to any Slim application named route and use `is_current_url` to determine if you need to mark a link as active as shown in this example Twig template:

{% extends "layout.html" %}

{% block body %}
<h1>User List</h1>
<ul>
<li><a href="{{ path_for('profile', { 'name': 'josh' }) }}" {% if is_current_path('profile', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li>
<li><a href="{{ path_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li>
<li><a href="{{ url_for('profile', { 'name': 'josh' }) }}" {% if is_current_url('profile', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li>
<li><a href="{{ url_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li>
</ul>
{% endblock %}


## Testing
## Tests

To execute the test suite, you'll need to clone the repository and install the dependencies.

```bash
phpunit
$ git clone https://github.com/slimphp/Twig-View
$ composer install
$ composer test
```

## Contributing
@@ -97,6 +103,7 @@ If you discover any security related issues, please email security@slimframework
## Credits

- [Josh Lockhart](https://github.com/codeguy)
- [Pierre Bérubé](https://github.com/l0gicgate)

## License

@@ -1,7 +1,7 @@
{
"name": "slim/twig-view",
"type": "library",
"description": "Slim Framework 3 view helper built on top of the Twig 2 templating component",
"description": "Slim Framework 4 view helper built on top of the Twig 2 templating component",
"keywords": ["slim","framework","view","template","twig"],
"homepage": "http://slimframework.com",
"license": "MIT",
@@ -10,20 +10,28 @@
"name": "Josh Lockhart",
"email": "hello@joshlockhart.com",
"homepage": "http://joshlockhart.com"
},
{
"name": "Pierre Berube",
"email": "pierre@lgse.com",
"homepage": "http://www.lgse.com"
}
],
"require": {
"php": ">=5.5.0",
"twig/twig": "^1.38|^2.7",
"psr/http-message": "^1.0"
"php": "^7.1",
"psr/http-message": "^1.0",
"twig/twig": "^2.8"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.7",
"slim/slim": "^3.10"
"phpunit/phpunit": "^7.5",
"php-di/php-di": "^6.0",
"psr/http-factory": "^1.0",
"slim/slim": "^4.0.0-alpha"
},
"autoload": {
"psr-4": {
"Slim\\Views\\": "src"
"Slim\\Views\\": "src",
"Slim\\Tests\\": "tests"
}
}
}
@@ -1,24 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.1/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Slim Test Suite">
<testsuite name="Twig-View Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src/</directory>
</whitelist>
</filter>

<logging>
<log
type="coverage-html"
target="./coverage"
lowUpperBound="20"
highLowerBound="50"
/>
</logging>
</phpunit>

0 comments on commit fed1bec

Please sign in to comment.
You can’t perform that action at this time.