jxy918 / swoft-smarty
基于swoft的smarty视图
v2.0.2
2019-10-24 03:22 UTC
Requires
- smarty/smarty: ^3.1
- swoft/framework: ^2.0.2
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-24 19:32:17 UTC
README
swof框架smarty模板组件
为swoft提供的smarty模板组件。
Swoft-smarty 组件
安装,安装
- 使用composer install命令
composer require jxy918/swoft-smarty
- smarty默认配置
- swoft框架中smarty的默认配置如下,默认无需添加,如需修改,可以将以下配置放入bean.php中进行相应的修改
'smarty' => [
'debugging'=>true,
'caching'=>true,
'cacheLifetime'=>120,
'leftDelimiter' => '<!--{',
'rightDelimiter' => '}-->',
'templateDir' => '@base/resource/template',
'compileDir' => '@base/runtime/template_c',
'cacheDir' => '@base/runtime/cache'
]
在控制器中使用如下
- app/Http/Controlle/SmartyController.php
- 资源模板/smarty.html
app/Http/Controlle/SmartyController.php
<?php declare(strict_types=1);
namespace App\Http\Controller;
use Swoft;
use ReflectionException;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
/**
* Class SmartyController
*
* @since 2.0
*
* @Controller(prefix="smarty")
*/
class SmartyController
{
/**
* @RequestMapping("index")
* @return Response
* @throws ContainerException
* @throws ReflectionException
* @throws \Swoft\Exception\SwoftException
*/
public function assign(): Response
{
$tpl = Swoft::getBean('smarty')->initView();
$data = ['nickname'=>'jxy918', 'sex'=>'男', 'msg'=>' hello smarty'];
$tpl->assign('data', $data);
$content = $tpl->fetch('smarty.html');
return context()->getResponse()->withContentType(ContentType::HTML)->withContent($content);
}
}
模板文件 resource/template/smarty.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test smayrt</title>
</head>
<body>
<div>
<div>
<h1>Hello Smarty</h1>
</div>
<ul>
<!--{foreach key=k item=v from=$data}-->
<li><!--{$k}-->:<!--{$v}--></li>
<!--{/foreach}-->
</ul>
</div>
</body>
</html>