├── LICENSE ├── README.md ├── composer.json └── src ├── Plates.php └── PlatesExtension.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Fery Wardiyanto 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Version](https://img.shields.io/packagist/v/projek-xyz/slim-plates?style=flat-square)](https://packagist.org/packages/projek-xyz/slim-plates) 2 | [![Lisence](https://img.shields.io/github/license/projek-xyz/slim-plates?style=flat-square)](https://github.com/projek-xyz/slim-plates/blob/main/LICENSE) 3 | [![Actions Status](https://img.shields.io/github/actions/workflow/status/projek-xyz/slim-plates/test.yml?branch=main&style=flat-square)](https://github.com/projek-xyz/slim-plates/actions) 4 | [![Coveralls](https://img.shields.io/coveralls/projek-xyz/slim-plates/master.svg?style=flat-square&logo=coveralls)](https://coveralls.io/github/projek-xyz/slim-plates) 5 | [![Code Climate](https://img.shields.io/codeclimate/coverage/projek-xyz/slim-plates.svg?style=flat-square&logo=code-climate)](https://codeclimate.com/github/projek-xyz/slim-plates/coverage) 6 | [![Maintainability](https://img.shields.io/codeclimate/maintainability/projek-xyz/slim-plates?style=flat-square&logo=code-climate)](https://codeclimate.com/github/projek-xyz/slim-plates/maintainability) 7 | [![SymfonyInsight Grade](https://img.shields.io/symfony/i/grade/69e58aaf-5d08-45f8-a9a7-8f246e031ec0?style=flat-square&logo=symfony)](https://insight.symfony.com/projects/69e58aaf-5d08-45f8-a9a7-8f246e031ec0) 8 | 9 | # Plates Template Integration for Slim micro framework 3 10 | 11 | Render your Slim 3 application views using Plates template engine. 12 | 13 | ## Install 14 | 15 | Via [Composer](https://getcomposer.org/) 16 | 17 | ```bash 18 | $ composer require projek-xyz/slim-plates --prefer-dist 19 | ``` 20 | 21 | Requires Slim micro framework 3 and PHP 5.5.0 or newer. 22 | 23 | ## Usage 24 | 25 | ```php 26 | // Create Slim app 27 | $app = new \Slim\App(); 28 | 29 | // Fetch DI Container 30 | $container = $app->getContainer(); 31 | 32 | // Register Plates View helper: 33 | // Option 1, using PlatesProvider 34 | $container->register(new \Projek\Slim\PlatesProvider); 35 | 36 | // Option 2, using Closure 37 | $container['view'] = function ($c) { 38 | $view = new \Projek\Slim\Plates([ 39 | // Path to view directory (default: null) 40 | 'directory' => 'path/to/views', 41 | // Path to asset directory (default: null) 42 | 'assetPath' => 'path/to/static/assets', 43 | // Template extension (default: 'php') 44 | 'fileExtension' => 'tpl', 45 | // Template extension (default: false) see: http://platesphp.com/extensions/asset/ 46 | 'timestampInFilename' => false, 47 | ]); 48 | 49 | // Set \Psr\Http\Message\ResponseInterface object 50 | // Or you can optionaly pass `$c->get('response')` in `__construct` second parameter 51 | $view->setResponse($c->get('response')); 52 | 53 | // Instantiate and add Slim specific extension 54 | $view->loadExtension(new Projek\Slim\PlatesExtension( 55 | $c->get('router'), 56 | $c->get('request')->getUri() 57 | )); 58 | 59 | return $view; 60 | }; 61 | 62 | // Define named route 63 | $app->get('/hello/{name}', function ($request, $response, $args) { 64 | return $this->view->render('profile', [ 65 | 'name' => $args['name'] 66 | ]); 67 | })->setName('profile'); 68 | 69 | // Run app 70 | $app->run(); 71 | ``` 72 | 73 | **NOTE**: 74 | * If you are using _option 1_ please make sure you already have `$container['settings']['view']` in your configuration file. 75 | * `Plates::setResponse()` is required to use `Plates::render()` otherwise `\LogicException` will thrown. 76 | 77 | ## Custom functions 78 | 79 | This component exposes some Slim functions to your Plates templates. 80 | 81 | ### `pathFor()` 82 | 83 | You can use this function to generate complete URLs to any Slim application named route. Example: 84 | 85 | ```php 86 | layout('base-template') ?> 87 | 88 | start('body') ?> 89 |

Hallo e($name)?>

90 | pathFor('profile', ['name'=> $name])?> 91 | stop() ?> 92 | ``` 93 | 94 | ### `baseUrl($permalink = '')` 95 | 96 | Retrieve the base url of your Slim application. Example: 97 | 98 | ```php 99 | Some Link 100 | ``` 101 | 102 | Or you can pass a permalink 103 | 104 | ```php 105 | Some Other Link 106 | ``` 107 | 108 | ### `basePath()` 109 | 110 | Retrieve the base url of your Slim application. Example: 111 | 112 | ```php 113 | 114 | ``` 115 | 116 | ### `uriFull()` 117 | 118 | Retrieve full request URI. 119 | 120 | ### `uriScheme()` 121 | 122 | Retrieve the [scheme](https://tools.ietf.org/html/rfc3986#section-3.1) component of the URI. 123 | 124 | ### `uriHost()` 125 | 126 | Retrieve the [host](http://tools.ietf.org/html/rfc3986#section-3.2.2) component of the URI 127 | 128 | ### `uriPort()` 129 | 130 | Retrieve the [port](https://tools.ietf.org/html/rfc3986#section-3.2.3) component of the URI. 131 | 132 | ### `uriPath()` 133 | 134 | Retrieve the [path](https://tools.ietf.org/html/rfc3986#section-3.3) component of the URI. 135 | 136 | ### `uriQuery()` 137 | 138 | Retrieve the [query string](https://tools.ietf.org/html/rfc3986#section-3.4) component of the URI. 139 | 140 | ### `uriFragment()` 141 | 142 | Retrieve the [fragment](https://tools.ietf.org/html/rfc3986#section-3.5) component of the URI. 143 | 144 | ## Contributing 145 | 146 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CONDUCT](.github/CONDUCT.md) for details. 147 | 148 | ## License 149 | 150 | This library is open-sourced software licensed under [MIT license](LICENSE.md). 151 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "projek-xyz/slim-plates", 3 | "description": "Render your Slim 3 application views using Plates template engine.", 4 | "keywords": ["slim", "microframework", "framework", "view", "template", "plates", "library"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Fery Wardiyanto", 10 | "email": "feryardiyanto@gmail.com" 11 | } 12 | ], 13 | "support": { 14 | "issues": "https://github.com/projek-xyz/slim-plates/issues", 15 | "source": "https://github.com/projek-xyz/slim-plates" 16 | }, 17 | "scripts": { 18 | "spec": [ 19 | "@lint", 20 | "@test" 21 | ], 22 | "test": "kahlan --config=test/config.php", 23 | "lint": "phpcs --standard=PSR2 -n -p src" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Projek\\Slim\\": "src/" 28 | } 29 | }, 30 | "require": { 31 | "league/plates": "^3.1", 32 | "psr/container": "^1.0 || ^2.0", 33 | "psr/http-factory": "^1.0", 34 | "psr/http-message": "^1.0 || ^2.0", 35 | "slim/slim": "^4.8" 36 | }, 37 | "require-dev": { 38 | "kahlan/kahlan": "^5.1", 39 | "projek-xyz/container": "^0.7", 40 | "slim/psr7": "^1.1", 41 | "squizlabs/php_codesniffer": "^3.6" 42 | }, 43 | "config": { 44 | "optimize-autoloader": true, 45 | "preferred-install": "dist", 46 | "sort-packages": true 47 | }, 48 | "minimum-stability": "dev", 49 | "prefer-stable": true 50 | } 51 | -------------------------------------------------------------------------------- /src/Plates.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | private $settings = [ 18 | 'directory' => null, 19 | 'assetPath' => null, 20 | 'fileExtension' => 'php', 21 | 'timestampInFilename' => false, 22 | ]; 23 | 24 | /** 25 | * Plates Engine instance. 26 | * 27 | * @var Engine 28 | */ 29 | private $plates; 30 | 31 | /** 32 | * @var StreamFactoryInterface 33 | */ 34 | private $streamFactory; 35 | 36 | /** 37 | * Create new Projek\Slim\Plates instance. 38 | * 39 | * @param array $settings 40 | * @param StreamFactoryInterface $streamFactory 41 | */ 42 | public function __construct(array $settings, StreamFactoryInterface $streamFactory) 43 | { 44 | $this->settings = array_merge($this->settings, $settings); 45 | $this->plates = new Engine($this->settings['directory'], $this->settings['fileExtension']); 46 | $this->streamFactory = $streamFactory; 47 | 48 | if (null !== $this->settings['assetPath']) { 49 | $this->setAssetPath($this->settings['assetPath']); 50 | } 51 | } 52 | 53 | /** 54 | * Get the Plate Engine. 55 | * 56 | * @return Engine 57 | */ 58 | public function getPlates() : Engine 59 | { 60 | return $this->plates; 61 | } 62 | 63 | /** 64 | * Set Asset path from Plates Asset Extension. 65 | * 66 | * @param string $assetPath 67 | * 68 | * @return Engine 69 | */ 70 | public function setAssetPath($assetPath) : Engine 71 | { 72 | return $this->plates->loadExtension( 73 | new Asset($assetPath, $this->settings['timestampInFilename']) 74 | ); 75 | } 76 | 77 | /** 78 | * Set Asset path from Plates Asset Extension. 79 | * 80 | * @param ExtensionInterface $extension 81 | * 82 | * @return Engine 83 | */ 84 | public function loadExtension(ExtensionInterface $extension) : Engine 85 | { 86 | $extension->register($this->plates); 87 | 88 | return $this->plates; 89 | } 90 | 91 | /** 92 | * Add a new template folder for grouping templates under different namespaces. 93 | * 94 | * @param string $name 95 | * @param string $directory 96 | * @param bool $fallback 97 | * 98 | * @throws \LogicException 99 | * 100 | * @return Engine 101 | */ 102 | public function addFolder($name, $directory, $fallback = false) : Engine 103 | { 104 | return $this->plates->addFolder($name, $directory, $fallback); 105 | } 106 | 107 | /** 108 | * Add preassigned template data. 109 | * 110 | * @param array $data 111 | * @param null|string[] $templates 112 | * 113 | * @throws \LogicException 114 | * 115 | * @return Engine 116 | */ 117 | public function addData(array $data, $templates = null) : Engine 118 | { 119 | return $this->plates->addData($data, $templates); 120 | } 121 | 122 | /** 123 | * Register a new template function. 124 | * 125 | * @param string $name 126 | * @param callable $callback 127 | * 128 | * @throws \LogicException 129 | * 130 | * @return Engine 131 | */ 132 | public function registerFunction($name, $callback) : Engine 133 | { 134 | return $this->plates->registerFunction($name, $callback); 135 | } 136 | 137 | /** 138 | * Render the template. 139 | * 140 | * @param ResponseInterface $response 141 | * @param string $name 142 | * @param array $data 143 | * 144 | * @throws \LogicException 145 | * 146 | * @return ResponseInterface 147 | */ 148 | public function render(ResponseInterface $response, $name, array $data = []) : ResponseInterface 149 | { 150 | $rendered = $this->streamFactory->createStream( 151 | $this->plates->render($name, $data) 152 | ); 153 | 154 | return $response->withBody($rendered); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/PlatesExtension.php: -------------------------------------------------------------------------------- 1 | context = RouteContext::fromRequest($request); 31 | $this->uri = $request->getUri(); 32 | } 33 | 34 | /** 35 | * Register extension function. 36 | * 37 | * @param Engine $engine Plates instance 38 | */ 39 | public function register(Engine $engine) : void 40 | { 41 | $engine->registerFunction('baseUrl', [$this, 'baseUrl']); 42 | $engine->registerFunction('uriFull', [$this, 'uriFull']); 43 | 44 | $engine->registerFunction('route', [$this->context, 'getRoute']); 45 | $engine->registerFunction('basePath', [$this->context, 'getBasePath']); 46 | 47 | $engine->registerFunction('urlFor', [$this->context->getRouteParser(), 'urlFor']); 48 | $engine->registerFunction('fullUrlFor', [$this->context->getRouteParser(), 'fullUrlFor']); 49 | $engine->registerFunction('relativeUrlFor', [$this->context->getRouteParser(), 'relativeUrlFor']); 50 | 51 | $engine->registerFunction('uriScheme', [$this->uri, 'getScheme']); 52 | $engine->registerFunction('uriHost', [$this->uri, 'getHost']); 53 | $engine->registerFunction('uriPort', [$this->uri, 'getPort']); 54 | $engine->registerFunction('uriPath', [$this->uri, 'getPath']); 55 | $engine->registerFunction('uriQuery', [$this->uri, 'getQuery']); 56 | $engine->registerFunction('uriFragment', [$this->uri, 'getFragment']); 57 | } 58 | 59 | /** 60 | * Retrieve slim baseUrl. 61 | * 62 | * @param string $permalink You can add optional permalink 63 | * 64 | * @return string 65 | */ 66 | public function baseUrl($permalink = '') : string 67 | { 68 | return $this->context->getBasePath().'/'.ltrim($permalink, '/'); 69 | } 70 | 71 | /** 72 | * Retrieve slim uri (string). 73 | * 74 | * @return string 75 | */ 76 | public function uriFull() : string 77 | { 78 | return (string) $this->uri; 79 | } 80 | 81 | /** 82 | * Retrieve slim uri (string). 83 | * 84 | * @return RouteInterface|null 85 | */ 86 | public function getRoute() : ?RouteInterface 87 | { 88 | return $this->context->getRoute(); 89 | } 90 | } 91 | --------------------------------------------------------------------------------