将 Twig 与 CodeIgniter 4 集成
Integrate Twig with CodeIgniter 4
我将 Twig 与 Symfony 一起使用,我真的很喜欢它。我现在有一个 CodeIgniter 项目,我想将 Twig 与其集成。
我通过 Composer 安装了最新版本的 CodeIgniter 和 Twig,并遵循 this tutorial 但我相信教程中的代码适用于 CI v3。
任何将 Twig 与 CI v4 集成的人都可以帮助我提供正确的代码。
更新
解决方法如下!
试试这个,希望对你有帮助
安装 Composer 和 运行 以下命令以获取最新版本:
composer require "twig/twig:^3.0"
然后在安装后将这行代码添加到baseController initController方法中parent::initController之后,就像下面的代码
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class BaseController extends Controller
{
protected $helpers = [];
protected $twig;
// protected $helper = [];
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$appPaths = new \Config\Paths();
$appViewPaths = $appPaths->viewDirectory;
$loader = new \Twig\Loader\FilesystemLoader($appViewPaths);
$this->twig = new \Twig\Environment($loader, [
'cache' => WRITEPATH.'/cache/twig',
]);
}
}
所以现在你可以调用其他控制器中的视图文件扩展到父控制器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
}
}
如果你仍然想使用 view()
和 twig 类似 codeigniter 4 默认视图功能,你可以修改 app 目录中的 Common.php
文件
通过在下面添加此代码块。
if (!function_exists('view'))
{
function view($tpl, $data = []) {
$appPaths = new \Config\Paths();
$appViewPaths = $appPaths->viewDirectory;
$loader = new \Twig\Loader\FilesystemLoader($appViewPaths);
$twig = new \Twig\Environment($loader, [
'cache' => WRITEPATH.'/cache/twig',
]);
if (!stripos($tpl, '.twig')) {
$tpl = $tpl . '.twig';
}
return $twig->render($tpl, $data);
}
}
然后在controller中这样调用
return view('index', ['name' => 'Chibueze Agwu'])
然后在查看文件index.twig
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>My Webpage</h1>
{{ name }}
</body>
</html>
这将输出
我的网页
赤布泽阿格乌
我还没有测试这段代码,但我希望它能工作。如果没有引起我的注意。
为了遵守 DRY
(DO NOT REPEAT YOURSELF
) 的规则,你可以继续改进我稍后会做的代码
我不久前找到了解决方案,我将其发布以防有人无意中遇到这个问题。
首先,你所有的控制器都必须扩展BaseController
;当您安装 CodeIgniter 4 时,此控制器默认可用。
创建自定义帮助程序文件并放入 [project-name]/appstarter/app/Helpers
.
重要
- 你的助手的名字必须是
[name]_helper.php
,否则将无法工作!
例如我的名字叫custom_helper.php
在您刚刚创建的自定义助手中创建以下函数:
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
if (!function_exists('twig_conf')) {
function twig_conf() {
// the follwing line of code is the only one needed to make Twig work
// the lines of code that follow are optional
$loader = new FilesystemLoader('Views', '../app/');
// to be able to use the 'dump' function in twig files
$twig = new Environment($loader, ['debug' => true]);
$twig->addExtension(new DebugExtension());
// twig lets you create custom filters
$filter = new TwigFilter('_base_url', function ($asset) {
return base_url() . '/' . $asset;
});
$twig->addFilter($filter);
return $twig;
}
}
注意
- 在创建任何自定义过滤器之前,sure Twig 还没有内置过滤器。
现在在 BaseController
中,您会发现一个名为 $helpers
的空数组。您必须在其中输入自定义助手的名称。我的叫custom_helper.php
;所以代码对我来说是这样的:
protected $helpers = ['custom'];
就在数组下方,您将找到 BaseController
的构造函数,这是初始化 Twig 库的地方;通过调用您在自定义助手中创建的函数:
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) {
parent::initController($request, $response, $logger);
$this->twig = twig_conf();
}
现在你可以开始了!在任何控制器中渲染你的树枝文件:
return $this->twig->render('twig_name', $dataArray);
试试这个,希望对你有帮助。
安装 Composer 和运行以下命令以获取最新版本:
composer require "twig/twig:^3.0"
我将 Twig 与 Symfony 一起使用,我真的很喜欢它。我现在有一个 CodeIgniter 项目,我想将 Twig 与其集成。
我通过 Composer 安装了最新版本的 CodeIgniter 和 Twig,并遵循 this tutorial 但我相信教程中的代码适用于 CI v3。
任何将 Twig 与 CI v4 集成的人都可以帮助我提供正确的代码。
更新
解决方法如下!
试试这个,希望对你有帮助
安装 Composer 和 运行 以下命令以获取最新版本:
composer require "twig/twig:^3.0"
然后在安装后将这行代码添加到baseController initController方法中parent::initController之后,就像下面的代码
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class BaseController extends Controller
{
protected $helpers = [];
protected $twig;
// protected $helper = [];
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$appPaths = new \Config\Paths();
$appViewPaths = $appPaths->viewDirectory;
$loader = new \Twig\Loader\FilesystemLoader($appViewPaths);
$this->twig = new \Twig\Environment($loader, [
'cache' => WRITEPATH.'/cache/twig',
]);
}
}
所以现在你可以调用其他控制器中的视图文件扩展到父控制器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
}
}
如果你仍然想使用 view()
和 twig 类似 codeigniter 4 默认视图功能,你可以修改 app 目录中的 Common.php
文件
通过在下面添加此代码块。
if (!function_exists('view'))
{
function view($tpl, $data = []) {
$appPaths = new \Config\Paths();
$appViewPaths = $appPaths->viewDirectory;
$loader = new \Twig\Loader\FilesystemLoader($appViewPaths);
$twig = new \Twig\Environment($loader, [
'cache' => WRITEPATH.'/cache/twig',
]);
if (!stripos($tpl, '.twig')) {
$tpl = $tpl . '.twig';
}
return $twig->render($tpl, $data);
}
}
然后在controller中这样调用
return view('index', ['name' => 'Chibueze Agwu'])
然后在查看文件index.twig
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>My Webpage</h1>
{{ name }}
</body>
</html>
这将输出
我的网页
赤布泽阿格乌
我还没有测试这段代码,但我希望它能工作。如果没有引起我的注意。
为了遵守 DRY
(DO NOT REPEAT YOURSELF
) 的规则,你可以继续改进我稍后会做的代码
我不久前找到了解决方案,我将其发布以防有人无意中遇到这个问题。
首先,你所有的控制器都必须扩展
BaseController
;当您安装 CodeIgniter 4 时,此控制器默认可用。创建自定义帮助程序文件并放入
[project-name]/appstarter/app/Helpers
.
重要
- 你的助手的名字必须是
[name]_helper.php
,否则将无法工作!
例如我的名字叫custom_helper.php
在您刚刚创建的自定义助手中创建以下函数:
use Twig\Environment; use Twig\Extension\DebugExtension; use Twig\Loader\FilesystemLoader; use Twig\TwigFilter; if (!function_exists('twig_conf')) { function twig_conf() { // the follwing line of code is the only one needed to make Twig work // the lines of code that follow are optional $loader = new FilesystemLoader('Views', '../app/'); // to be able to use the 'dump' function in twig files $twig = new Environment($loader, ['debug' => true]); $twig->addExtension(new DebugExtension()); // twig lets you create custom filters $filter = new TwigFilter('_base_url', function ($asset) { return base_url() . '/' . $asset; }); $twig->addFilter($filter); return $twig; } }
注意
- 在创建任何自定义过滤器之前,sure Twig 还没有内置过滤器。
现在在
BaseController
中,您会发现一个名为$helpers
的空数组。您必须在其中输入自定义助手的名称。我的叫custom_helper.php
;所以代码对我来说是这样的:protected $helpers = ['custom'];
就在数组下方,您将找到
BaseController
的构造函数,这是初始化 Twig 库的地方;通过调用您在自定义助手中创建的函数:public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); $this->twig = twig_conf(); }
现在你可以开始了!在任何控制器中渲染你的树枝文件:
return $this->twig->render('twig_name', $dataArray);
试试这个,希望对你有帮助。
安装 Composer 和运行以下命令以获取最新版本:
composer require "twig/twig:^3.0"