borschphp / smarty
A smarty template engine wrapper for Borsch Framework applications.
0.1.1
2022-02-19 17:45 UTC
Requires
- borschphp/template: ^0
- smarty/smarty: ^v4
Requires (Dev)
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-10-20 00:06:10 UTC
README
An implementation of Smarty for the Borsch's TemplateRendererInterface.
This package is part of the Borsch Framework.
Installation
Via composer :
composer require borschphp/smarty
Integration in Borsch Framework
Open the file config/container.php
then add your TemplateRendererInterface definition.
use Borsch\Smarty\Smarty; use Borsch\Template\TemplateRendererInterface; /* * Template renderer definitions * ----------------------------- * * Defining the TemplateRendererInterface here so our HomeHandler handler (see upper) can use it when instantiated. * The default Borsch Smarty renderer is used. * We cache it so that it is not re-created when fetched a second time. */ $container->set(TemplateRendererInterface::class, function () { $smarty = new Smarty(); $smarty->setTemplateDir(__DIR__.'/../resources/templates'); $smarty->setCompileDir(__DIR__.'/../storage/smarty/templates_c'); $smarty->setCacheDir(__DIR__.'/../storage/smarty/cache'); return $smarty; })->cache(true);
To load it in your handler, add it as a parameter of the constructor :
class HomeHandler implements RequestHandlerInterface { /** @var TemplateRendererInterface */ protected $renderer; /** * HomeHandler constructor. * @param TemplateRendererInterface $renderer */ public function __construct(TemplateRendererInterface $renderer) { $this->renderer = $renderer; } /** * @inheritDoc */ public function handle(ServerRequestInterface $request): ResponseInterface { return new HtmlResponse($this->renderer->render('home')); } }
Usage
$smarty = new \Borsch\Smarty\Smarty(); // Adding some directories where templates are located $smarty->addPath(__DIR__.'/templates'); $smarty->addPath(__DIR__.'/templates/error', 'error'); $smarty->addPath(__DIR__.'/templates/success', 'success'); // Assign some variables $smarty->assign([ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ]); // Display the template (with or without .tpl extension) echo $smarty->render('template_file_name');
License
The package is licensed under the MIT license. See License File for more information.