将 Twig 与 CodeIgniter 4 集成

Jol*_*lan 4 codeigniter twig codeigniter-4

我将 Twig 与 Symfony 一起使用,我真的很喜欢它。我现在有一个 CodeIgniter 项目,我想将 Twig 与其集成。

我通过 Composer 安装了最新版本的 CodeIgniter 和 Twig,并遵循了本教程,但我相信教程中的代码适用于 CI v3。

任何将 Twig 与 CI v4 集成的人都可以帮我提供正确的代码吗?

更新

解决方案如下!

Chi*_*gwu 9

试试这个我希望它能帮助你

安装 Composer 并运行以下命令以获取最新版本:

composer require "twig/twig:*"

然后安装完后将这行代码添加到config文件夹下的Services.php文件中,即 ,app => Config => Services.php就像下面的代码

    ....

    public static function twig($viewDir = null, $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('twig', $viewDir);
        }

        $appPaths = new \Config\Paths();
        $appViewPaths = $viewDir ?? $appPaths->viewDirectory;

        $loader = new \Twig\Loader\FilesystemLoader($appViewPaths);

        return new \Twig\Environment($loader, [
            'cache' => WRITEPATH.'/cache/twig',
        ]);
    }
    ....
Run Code Online (Sandbox Code Playgroud)

**然后在你的控制器中调用你刚刚创建的服务方法,就像这样

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use Config\Services; // Add the services namespace 

class BaseController extends Controller
{
    protected $helpers = [];
    protected $twig;

    // protected $helper = [];
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);

        $this->twig = Services::twig(); // call the twig service you just created

    }
}

Run Code Online (Sandbox Code Playgroud)

因此,现在您可以调用其他控制器中的视图文件扩展至父控制器 BaseController 例如


namespace App\Controllers;


class Home extends BaseController
{
    public function index ()
    {
        // To load a template from a Twig environment, call the load() method which returns a \Twig\TemplateWrapper instance:

       $template = $this->twig->load('index.html');

       // To render the template with some variables, call the render() method:

       return $template->render(['the' => 'variables', 'go' => 'here']);

       // The display() method is a shortcut to output the rendered template.
       // OR You can also load and render the template in one fell swoop:

       return $this->twig->render('index.html', ['the' => 'variables', 'go' => 'here']);

       // If a template defines blocks, they can be rendered individually via the renderBlock() call:

       return $template->renderBlock('block_name', ['the' => 'variables', 'go' => 'here']);

       // Note any of them above will work
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您仍然想使用view()像 codeigniter 4 默认视图功能这样的 twig,您可以Common.php通过添加下面的代码块来修改 app 目录中的文件。


if (!function_exists('view'))
{
    function view($tpl, $data = []) {

        $twig = \Config\Services::twig(); // call the twig service you just created

        return $twig->render($tpl, $data);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在控制器中这样调用它


return view('index', ['name' => 'Chibueze Agwu'])

Run Code Online (Sandbox Code Playgroud)

然后在查看文件中index.twig

<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
  </head>
  <body>
    <h1>My Webpage</h1>
    {{ name }}
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这将输出

我的网页
Chibueze Agwu

我还没有测试过这段代码,但我希望它能起作用。如果没有请引起我的注意。为了遵守DRYDO NOT REPEAT YOURSELF)的规则,你可以继续改进代码,我稍后会这样做