title: EasySwoole Template engine meta:

  • name: description content: EasySwoole Template engine|swoole Template engine|swoole Template rendering
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole Template engine|swoole Template engine|swoole Template rendering

Template engine

Rendering driver

EasySwoole introduces the form of the template rendering driver, and the data that needs to be rendered is delivered to the custom synchronization process through the coroutine client for rendering and returning the result. The reason for this is that some of the templating engines on the market have variable security issues under the Swoole coroutine. For example, the following process:

  • 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

    The above process we can find that the data requested by A is polluted by B. To solve this problem, EasySwoole introduced a template rendering driver mode.

Installation

  1. composer require easyswoole/template

Basic implementation

Implementing the rendering engine

  1. use EasySwoole\Template\Config;
  2. use EasySwoole\Template\Render;
  3. use EasySwoole\Template\RenderInterface;
  4. class R implements RenderInterface
  5. {
  6. public function render(string $template, array $data = [], array $options = []):?string
  7. {
  8. return 'asas';
  9. }
  10. public function afterRender(?string $result, string $template, array $data = [], array $options = [])
  11. {
  12. // TODO: Implement afterRender() method.
  13. }
  14. public function onException(Throwable $throwable):string
  15. {
  16. return $throwable->getMessage();
  17. }
  18. }

Called in HTTP service

  1. //In the event created in the global main service, instantiate the Render and inject your driver configuration
  2. Render::getInstance()->getConfig()->setRender(new R());
  3. $http = new swoole_http_server("0.0.0.0", 9501);
  4. $http->on("request", function ($request, $response)use($render) {
  5. //Call the renderer, and then send the data to the custom synchronization process through the Ctrip client, and get the rendering result.
  6. $response->end(Render::getInstance()->render('a.html'));
  7. });
  8. $render->attachServer($http);
  9. $http->start();

Restart the rendering engine

Because some template engines cache template files This may lead to the following situations:

  • User A requests 1.tpl to return ‘a’
  • The developer modified the 1.tpl data and changed it to ‘b’
  • User B, C, D may have two different values of ‘a’ and ‘b’ in subsequent requests.

That’s because the template engine has already cached the file of the process in which A is located, causing subsequent requests to be cached if they are also assigned to A’s process.

The solution is as follows: 1: Restart easyswoole, you can solve 2: The template rendering engine implements the restart method restartWorker, which can be called directly.

  1. Render::getInstance()->restartWorker();

Users can call the restartWorker method to restart according to their own logic. For example, add a reload method to the controller:

  1. <?php
  2. namespace App\HttpController;
  3. use EasySwoole\Http\AbstractInterface\Controller;
  4. use EasySwoole\Template\Render;
  5. class Index extends Controller
  6. {
  7. function index()
  8. {
  9. $this->response()->write(Render::getInstance()->render('index.tpl',[
  10. 'user'=>'easyswoole',
  11. 'time'=>time()
  12. ]));
  13. }
  14. function reload(){
  15. Render::getInstance()->restartWorker();
  16. $this->response()->write(1);
  17. }
  18. }

Smarty Rendering

Introducing Smarty

  1. composer require smarty/smarty - request A reached, static A assign requestA-data
  2. - compiled template
  3. - write compiled template (yiled current coroutine)
  4. - request B reachedstatic A assign requestB-data
  5. - render static A data into complied template file

Implement the rendering engine

  1. use EasySwoole\Template\RenderInterface;
  2. use EasySwoole\Template\RenderInterface;
  3. class Smarty implements RenderInterface
  4. {
  5. private $smarty;
  6. function __construct()
  7. {
  8. $temp = sys_get_temp_dir();
  9. $this->smarty = new \Smarty();
  10. $this->smarty->setTemplateDir(__DIR__.'/');
  11. $this->smarty->setCacheDir("{$temp}/smarty/cache/");
  12. $this->smarty->setCompileDir("{$temp}/smarty/compile/");
  13. }
  14. public function render(string $template, array $data = [], array $options = []): ?string
  15. {
  16. foreach ($data as $key => $item){
  17. $this->smarty->assign($key,$item);
  18. }
  19. return $this->smarty->fetch($template,$cache_id = null, $compile_id = null, $parent = null, $display = false,
  20. $merge_tpl_vars = true, $no_output_filter = false);
  21. }
  22. public function afterRender(?string $result, string $template, array $data = [], array $options = [])
  23. {
  24. }
  25. public function onException(\Throwable $throwable): string
  26. {
  27. $msg = "{$throwable->getMessage()} at file:{$throwable->getFile()} line:{$throwable->getLine()}";
  28. trigger_error($msg);
  29. return $msg;
  30. }
  31. }

Calling in HTTP service

  1. // Create an event in the global main service, instantiate the Render, and inject your driver configuration
  2. Render::getInstance()->getConfig()->setRender(new Smarty());
  3. Render::getInstance()->getConfig()->setTempDir(EASYSWOOLE_TEMP_DIR);
  4. Render::getInstance()->attachServer(ServerManager::getInstance()->getSwooleServer());
  5. // Implement the response in the action
  6. Render::getInstance()->render('a.html');

Support for common template engines

Here are some common template engine packages for easy introduction:

smarty/smarty

Smarty is a template engine written in PHP and is one of the most famous PHP template engines in the industry.

::: warning composer require smarty/smarty=~3.1 :::

league/plates

Lower cost of learning and higher freedom with a non-compiled template engine using native PHP syntax

::: warning Composer require league/plates=3.* :::

duncan3dc/blade

Template engine used by the Laravel framework

::: warning Composer require duncan3dc/blade=^4.5 :::

topthink/think-template

Template engine used by the ThinkPHP framework

::: warning composer require topthink/think-template :::