前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Easyswoole中实现think-template模板引擎使用

Easyswoole中实现think-template模板引擎使用

作者头像
北溟有鱼QAQ
发布2019-12-19 13:32:51
1.1K0
发布2019-12-19 13:32:51
举报
文章被收录于专栏:北溟有鱼QAQ
在EasySwoole中实现Tp框架中think-template模板引擎的使用
  1. 安装EasySwoole模板引擎驱动 composer require easyswoole/template (EasySwoole引入模板渲染驱动的形式,把需要渲染的数据,通过协程客户端投递到自定义的同步进程中进行渲染并返回结果。为何要如此处理,原因在于,市面上的一些模板引擎在Swoole协程下存在变量安全问题)
  2. 安装ThinkPHP框架使用的模板引擎 composer require topthink/think-template
  3. 实现渲染引擎,代码如下
代码语言:javascript
复制
<?php
namespace App;
use EasySwoole\Template\RenderInterface;
class Template implements RenderInterface
{
    protected $template;
    function __construct()
    {
        $config = [
            'view_path'	=>	EASYSWOOLE_ROOT.'/App/Views/',
            'cache_path'	=>	EASYSWOOLE_ROOT.'/Temp/runtime/',
        ];
        $this->template = new \think\Template($config);
    }
    public function render(string $template, array $data = [], array $options = []): ?string
    {
        // TODO: Implement render() method.
        ob_start();
        $this->template->assign($data);
        $this->template->fetch($template);
        $content = ob_get_contents() ;
        return $content;
    }
    public function afterRender(?string $result, string $template, array $data = [], array $options = [])
    {
        // TODO: Implement afterRender() method.
    }
    public function onException(\Throwable $throwable): string
    {
        // TODO: Implement onException() method.
        $msg = "{$throwable->getMessage()} at file:{$throwable->getFile()} line:{$throwable->getLine()}";
        trigger_error($msg);
        return $msg;
    }
}
  1. HTTP服务中调用
代码语言:javascript
复制
<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Template\Render;
use App\Template;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        //在全局的主服务中创建事件中,实例化该Render,并注入你的驱动配置
        Render::getInstance()->getConfig()->setRender(new Template());
        Render::getInstance()->attachServer(ServerManager::getInstance()->getSwooleServer());
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}

5.调用模板并输出

代码语言:javascript
复制
<?php
namespace App\HttpController\Admin;

use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Template\Render;

class Index extends Controller
{
    public function index()
    {
        $this->response()->write(Render::getInstance()->render('admin/index',['row'=> time()]));

    }

    public function console()
    {
        $this->response()->write(Render::getInstance()->render('admin/console'));
    }
}
这样就可以在我们的模板中,就像用Tp框架开发项目一样,使用熟悉的模板来开发项目了

本文为北溟有鱼QAQ原创文章,转载无需和我联系,但请注明来自北溟有鱼QAQ https://www.umdzz.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在EasySwoole中实现Tp框架中think-template模板引擎的使用
  • 这样就可以在我们的模板中,就像用Tp框架开发项目一样,使用熟悉的模板来开发项目了
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档