diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1b021fc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - '5.5' + - '5.6' + - '7.0' + - '7.1' + - hhvm + - nightly + +before_script: composer install + +script: phpunit --configuration phpunit.xml.dist diff --git a/README.md b/README.md index 2d9db70..2f8ffeb 100644 --- a/README.md +++ b/README.md @@ -1 +1,69 @@ -# Slim-Blade-View \ No newline at end of file +# Slim-Blade-View + +[![Build Status](https://travis-ci.org/rubellum/Slim-Blade-View.svg?branch=master)](https://travis-ci.org/rubellum/Slim-Blade-View) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) + +This is a Slim Framework view helper built on top of the Blade component. + +You can use this component to create and render templates in your Slim Framework application. + +## Install + +Via [Composer](https://getcomposer.org/) + +```bash +$ composer require hiropeke/slim-blade-view +``` + +Requires Slim Framework 3 and PHP 5.5.0 or newer. + +## Usage + +```php +// Slim Settings +$config = [ + 'settings' => [ + 'displayErrorDetails' => true, // set to false in production + + // Renderer settings + 'renderer' => [ + 'blade_template_path' => 'path/to/views', // String or array of multiple paths + 'blade_cache_path' => 'path/to/cache', // Mandatory by default, though could probably turn caching off for development + ], + ], +]; + +// Create Slim app +$app = new \Slim\App($config); + +// Fetch DI Container +$container = $app->getContainer(); + +// Register Blade View helper +$container['view'] = function ($container) { + return new \Slim\Views\Blade( + $container['settings']['renderer']['blade_template_path'], + $container['settings']['renderer']['blade_cache_path'] + ); +}; + +// Define named route +$app->get('/hello/{name}/', function ($request, $response, $args) { + return $this->view->render($response, 'profile', [ + 'name' => $args['name'], + ]); +})->setName('profile'); + +// Run app +$app->run(); +``` + +## Testing + +```bash +$ phpunit +``` + +## License + +The MIT License (MIT). Please see [License File](LICENSE) for more information. diff --git a/composer.json b/composer.json index e30870b..c3d98ce 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "authors": [ { "name": "Hiroaki Matsuura", - "email": "hiropeke.jp@gmail.com" + "email": "lib2jp@gmail.com" } ], "require": { @@ -28,7 +28,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^5.0", + "phpunit/phpunit": "^4.0", "slim/slim": "^3.0" } } \ No newline at end of file diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/example/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/example/cache/.gitignore b/example/cache/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/example/cache/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/example/composer.json b/example/composer.json new file mode 100644 index 0000000..6a53655 --- /dev/null +++ b/example/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "slim/slim": "^3.0", + "hiropeke/slim-blade-view": "^0.1.1" + } +} diff --git a/example/public/index.php b/example/public/index.php new file mode 100644 index 0000000..f83f894 --- /dev/null +++ b/example/public/index.php @@ -0,0 +1,40 @@ + [ + 'displayErrorDetails' => true, // set to false in production + + // Renderer settings + 'renderer' => [ + 'blade_template_path' => __DIR__ . '/../views', // String or array of multiple paths + 'blade_cache_path' => __DIR__ . '/../cache', // Mandatory by default, though could probably turn caching off for development + ], + ], +]; + +// Create Slim app +$app = new \Slim\App($config); + +// Fetch DI Container +$container = $app->getContainer(); + +// Register Blade View helper +$container['view'] = function ($container) { + return new \Slim\Views\Blade( + $container['settings']['renderer']['blade_template_path'], + $container['settings']['renderer']['blade_cache_path'] + ); +}; + +// Define named route +$app->get('/hello/{name}/', function ($request, $response, $args) { + return $this->view->render($response, 'profile', [ + 'name' => $args['name'], + ]); +})->setName('profile'); + +// Run app +$app->run(); diff --git a/example/views/profile.blade.php b/example/views/profile.blade.php new file mode 100644 index 0000000..063cd81 --- /dev/null +++ b/example/views/profile.blade.php @@ -0,0 +1 @@ +Hello, {{ $name }} diff --git a/tests/BladeTest.php b/tests/BladeTest.php index a6bca4d..cdca93a 100644 --- a/tests/BladeTest.php +++ b/tests/BladeTest.php @@ -30,7 +30,7 @@ class BladeTest extends \PHPUnit_Framework_TestCase { $views = new Blade([ 'One' => __DIR__ . '/templates', - ]); + ], __DIR__ . '/../data/cache'); $output = $views->fetch('example', [ 'name' => 'Josh', ]); @@ -42,7 +42,7 @@ class BladeTest extends \PHPUnit_Framework_TestCase $views = new Blade([ 'One' => __DIR__ . '/templates', 'Two' => __DIR__ . '/another', - ]); + ], __DIR__ . '/../data/cache'); $outputOne = $views->fetch('example', [ 'name' => 'Peter', ]);