borschphp/smarty
Composer 安装命令:
composer require borschphp/smarty
包简介
A smarty template engine wrapper for Borsch Framework applications.
README 文档
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.
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-01