php render模板,easyswoole实现模板渲染

easyswoole框架使用模板直接使用模板引擎,是会有问题的,所以增加了渲染驱动

渲染驱动

EasySwoole引入模板渲染驱动的形式,把需要渲染的数据,通过协程客户端投递到自定义的同步进程中进行渲染并返回结果。为何要如此处理,原因在于,市面上的一些模板引擎在Swoole协程下存在变量安全问题。例如以下流程:request A reached, static A assign requestA-data

compiled template

write compiled template (yiled current coroutine)

request B reached,static A assign requestB-data

render static A data into complied template file

以上流程我们可以发现,A请求的数据,被B给污染了。为了解决该问题,EasySwoole引入模板渲染驱动模式。

安装

composer require easyswoole/template

实现渲染引擎use EasySwoole\Template\Config;

use EasySwoole\Template\Render;

use EasySwoole\Template\RenderInterface;

class R implements RenderInterface{

public function render(string $template, array $data = [], array $options = []):?string

{

return 'asas';

}

public function afterRender(?string $result, string $template, array $data = [], array $options = [])

{

// TODO: Implement afterRender() method.

}

public function onException(Throwable $throwable):string

{

return $throwable->getMessage();

}

}

在http中调用://在全局的主服务中创建事件中,实例化该Render,并注入你的驱动配置

Render::getInstance()->getConfig()>setRender(new R());

$http = new swoole_http_server("0.0.0.0", 9501);

$http->on("request", function ($request, $response)use($render) {

//调用渲染器,此时会通过携程客户端,把数据发往自定义的同步进程中处理,并得到渲染结果

$response->end(Render::getInstance()->render('a.html'));

});

$render->attachServer($http);$http->start();

Smarty 渲染

引入:composer require smarty/smarty

实现渲染引擎实现渲染引擎

use EasySwoole\Template\RenderInterface;use EasySwoole\Template\RenderInterface;

class Smarty implements RenderInterface{

private $smarty;

function __construct()

{

$temp = sys_get_temp_dir();

$this->smarty = new \Smarty();

$this->smarty->setTemplateDir(__DIR__.'/');

$this->smarty->setCacheDir("{$temp}/smarty/cache/");

$this->smarty->setCompileDir("{$temp}/smarty/compile/");

}

public function render(string $template, array $data = [], array $options = []): ?string    {

foreach ($data as $key => $item){

$this->smarty->assign($key,$item);

}

return $this->smarty->fetch($template,$cache_id = null, $compile_id = null, $parent = null, $display = false,

$merge_tpl_vars = true, $no_output_filter = false);

}

public function afterRender(?string $result, string $template, array $data = [], array $options = [])

{

}

public function onException(\Throwable $throwable): string    {

$msg = "{$throwable->getMessage()} at file:{$throwable->getFile()} line:{$throwable->getLine()}";

trigger_error($msg);

return $msg;

}

}

在http中调用smarty://在全局的主服务中创建事件中,实例化该Render,并注入你的驱动配置

Render::getInstance()->getConfig()>setRender(new Smarty());

Render::getInstance()->getConfig()->setTempDir(EASYSWOOLE_TEMP_DIR);

Render::getInstance()->attachServer(ServerManager::getInstance()->getSwooleServer());

//在控制器action中实现响应

Render::getInstance()->render('a.html');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值