我用了Twig和Symfony,我真的很喜欢它。我现在有一个CodeIgniter项目,我想将Twig与它集成起来。
我通过Composer安装了CodeIgniter和Twig的最新版本,并遵循了本教程,但我相信本教程中的代码是针对CI v3的。
任何人谁已经将Twig与CI v4集成,请帮助我正确的代码。
更新
下面是解决方案!
发布于 2021-02-15 09:29:43
我在一段时间前找到了这个解决方案,现在我把它发了出来,以防有人发现这个问题。
BaseController;默认情况下,安装CodeIgniter 4时该控制器是可用的。[project-name]/appstarter/app/Helpers。重要
[name]_helper.php,否则它将无法工作!例如,我的名字叫custom_helper.php
备注
BaseController中找到一个名为$helpers的空数组。您必须将自定义助手的名称放入其中。我的代码名为custom_helper.php;所以对于我来说,代码如下所示:
受保护的$helpers =‘定制’;BaseController的构造函数,这里将初始化Twig库;通过调用您在自定义助手中创建的函数:
公共函数initController(RequestInterface $request,ResponseInterface $response,LoggerInterface $logger) {父级::initController($request,$response,$logger);$this->twig = twig_conf();}发布于 2020-07-30 19:33:37
试试这个,我希望它能帮到你
安装Composer并运行以下命令以获得最新版本:
composer require "twig/twig:^3.0"
然后在安装之后,将这一行代码添加到baseController initController方法的父级::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()和小树枝,如codeigniter 4默认视图函数,您可以通过添加下面的代码块来修改应用程序目录中的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);
}
}然后在控制器里这样叫它
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)的规则,您可以继续改进代码,我稍后会这样做。
发布于 2021-05-19 09:27:04
试试这个,我希望它能帮到你。
安装Composer并运行以下命令以获得最新版本:
作曲家要求“树枝/树枝:^3.0”
https://stackoverflow.com/questions/63174050
复制相似问题