From 0304512b798b316e31c7a1cd6aa1424ec4124404 Mon Sep 17 00:00:00 2001 From: blat Date: Sun, 31 Jul 2022 10:16:39 +0200 Subject: [PATCH] first commit --- .gitignore | 2 + README.md | 80 ++++++++++++++++++++++++++ composer.json | 26 +++++++++ src/Plates.php | 51 +++++++++++++++++ src/PlatesExtension.php | 123 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Plates.php create mode 100644 src/PlatesExtension.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..987e2a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +vendor diff --git a/README.md b/README.md new file mode 100644 index 0000000..73e0a46 --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ +# Slim Framework Plates View + +This is a Slim Framework view helper built on top of the Plates templating 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 blat/slim-plates +``` + +Requires Slim Framework 4, Plates 3 and PHP 7.4 or newer. + +## Usage + +```php +use DI\Container; +use Slim\Factory\AppFactory; +use Slim\Views\Plates; +use Slim\Views\PlatesExtension; + +require __DIR__ . '/vendor/autoload.php'; + +// Create Container +$container = new Container(); +AppFactory::setContainer($container); + +// Set Plates View in Container +$container->set('view', function () { + return new Plates(__DIR__ . '/../templates'); +}); + +// Create App +$app = AppFactory::create(); + +// Add Plates Extension Middleware +$app->add(new PlatesExtension($app)); + +// Define named route +$app->get('/hello/{name}', function ($request, $response, $args) { + return $this->get('view')->render($response, 'profile', [ + 'name' => $args['name'] + ]); +})->setName('profile'); + +// Run app +$app->run(); +``` + +## Custom template functions + +`PlatesExtension` provides these functions to your Plates templates: + +* `urlFor()` - returns the URL for a given route. e.g.: /hello/world +* `fullUrFor()` - returns the URL for a given route. e.g.: http://www.example.com/hello/world +* `isCurrentUrl()` - returns true is the provided route name and parameters are valid for the current path. +* `currentUrl()` - returns the current path, with or without the query string. +* `basePath()` - returns the base path. + +You can use `urlFor` to generate complete URLs to any Slim application named route and use `isCurrentUrl` to determine if you need to mark a link as active as shown in this example Plates template: + +```php +layout('template') ?> + +

User List

+ +``` + +## The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e6c5792 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "blat/slim-plates", + "type": "library", + "description": "Slim Framework 4 view helper built on top of the Plates templating component", + "keywords": ["slim","framework","view","template","templating","plates"], + "license": "MIT", + "authors": [ + { + "name": "Mickael BLATIERE", + "email": "blatiere@gmail.com", + "homepage": "https://blizzart.net" + } + ], + "require": { + "league/plates": "^3.4", + "php": "^7.4 || ^8.0", + "psr/http-message": "^1.0", + "slim/slim": "^4.10" + }, + "autoload": { + "psr-4": { + "Slim\\Views\\": "src" + } + } +} + diff --git a/src/Plates.php b/src/Plates.php new file mode 100644 index 0000000..d8b90f3 --- /dev/null +++ b/src/Plates.php @@ -0,0 +1,51 @@ +engine = new Engine(...$settings); + } + + /** + * Access forward to Engine methods + * + * @param string $name + * @param array $args + * + * @return mixed + */ + public function __call(string $name, array $args): mixed + { + return call_user_func_array([$this->engine, $name], $args); + } + + /** + * Output rendered template + * + * @param ResponseInterface $response + * @param string $template Template pathname relative to templates directory + * @param array $data Associative array of template variables + * + * @return ResponseInterface + */ + public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface + { + $output = $this->engine->render($template, $data); + $response->getBody()->write($output); + return $response; + } + +} diff --git a/src/PlatesExtension.php b/src/PlatesExtension.php new file mode 100644 index 0000000..8c0f6f0 --- /dev/null +++ b/src/PlatesExtension.php @@ -0,0 +1,123 @@ +app = $app; + } + + /** + * Process an incoming server request. + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $this->uri = $request->getUri(); + $this->app->getContainer()->get('view')->loadExtension($this); + return $handler->handle($request); + } + + /** + * @param Engine $engine + */ + public function register(Engine $engine) + { + foreach (['urlFor', 'fullUrlFor', 'isCurrentUrl', 'getCurrentUrl', 'getBasePath'] as $name) { + $engine->registerFunction($name, [$this, $name]); + } + } + + /** + * Get the url for a named route + * + * @param string $routeName Route name + * @param array $data Route placeholders + * @param array $queryParams Query parameters + * + * @return string + */ + public function urlFor(string $routeName, array $data = [], array $queryParams = []): string + { + return $this->app->getRouteCollector()->getRouteParser()->urlFor($routeName, $data, $queryParams); + } + + /** + * Get the full url for a named route + * + * @param string $routeName Route name + * @param array $data Route placeholders + * @param array $queryParams Query parameters + * + * @return string + */ + public function fullUrlFor(string $routeName, array $data = [], array $queryParams = []): string + { + return $this->app->getRouteCollector()->getRouteParser()->fullUrlFor($this->uri, $routeName, $data, $queryParams); + } + + /** + * @param string $routeName Route name + * @param array $data Route placeholders + * + * @return bool + */ + public function isCurrentUrl(string $routeName, array $data = []): bool + { + $currentUrl = $this->getBasePath() . $this->uri->getPath(); + $result = $this->app->getRouteCollector()->getRouteParser()->urlFor($routeName, $data); + + return $result === $currentUrl; + } + + /** + * Get current path on given Uri + * + * @param bool $withQueryString + * + * @return string + */ + public function getCurrentUrl(bool $withQueryString = false): string + { + $currentUrl = $this->getBasePath() . $this->uri->getPath(); + $query = $this->uri->getQuery(); + + if ($withQueryString && !empty($query)) { + $currentUrl .= '?' . $query; + } + + return $currentUrl; + } + + /** + * Get the base path + * + * @return string + */ + public function getBasePath(): string + { + return $this->app->getBasePath(); + } + +}