首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Twig与CodeIgniter 4集成

将Twig与CodeIgniter 4集成
EN

Stack Overflow用户
提问于 2020-07-30 13:17:01
回答 3查看 3.2K关注 0票数 3

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

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

任何人谁已经将Twig与CI v4集成,请帮助我正确的代码。

更新

下面是解决方案!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-15 09:29:43

我在一段时间前找到了这个解决方案,现在我把它发了出来,以防有人发现这个问题。

  1. 首先,所有控制器都必须扩展BaseController;默认情况下,安装CodeIgniter 4时该控制器是可用的。
  2. 创建一个自定义帮助文件并放入[project-name]/appstarter/app/Helpers

重要

  • 您的助手的名字必须是[name]_helper.php,否则它将无法工作!

例如,我的名字叫custom_helper.php

  1. 在您刚才创建的自定义助手中创建以下函数: 使用Twig\Environment;使用Twig\Extension\DebugExtension;使用Twig\Loader\FilesystemLoader;使用Twig\TwigFilter;if (!function_exists('twig_conf')) { function twig_conf() { //折叠代码行是使Twig工作/后面的代码行是可选的$loader =新的FilesystemLoader(‘视图’,‘./app/’);//能够在小枝文件$twig = new ($loader,调试‘=>真)中使用’DebugExtension‘函数;$ twig ->addExtension(新DebugExtension());// twig允许您创建自定义筛选器$filter =新TwigFilter('_base_url',函数($asset) {返回base_url() )。“/”。( $asset;});$twig->addFilter($filter);返回$twig;}

备注

  • 在创建任何自定义过滤器之前,make 好的 Twig还没有内置一个。
  1. 现在,您将在BaseController中找到一个名为$helpers的空数组。您必须将自定义助手的名称放入其中。我的代码名为custom_helper.php;所以对于我来说,代码如下所示: 受保护的$helpers =‘定制’;
  2. 就在数组下面,您将找到BaseController的构造函数,这里将初始化Twig库;通过调用您在自定义助手中创建的函数: 公共函数initController(RequestInterface $request,ResponseInterface $response,LoggerInterface $logger) {父级::initController($request,$response,$logger);$this->twig = twig_conf();}
  3. 现在你可以走了!若要在任何控制器中呈现树枝文件,请执行以下操作: 返回$this->twig->呈现(‘twig_name’,$dataArray);
票数 2
EN

Stack Overflow用户

发布于 2020-07-30 19:33:37

试试这个,我希望它能帮到你

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

composer require "twig/twig:^3.0"

然后在安装之后,将这一行代码添加到baseController initController方法的父级::initController之后,如下所示

代码语言:javascript
运行
复制
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的视图文件,例如

代码语言:javascript
运行
复制
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文件。

代码语言:javascript
运行
复制
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);
    }
}

然后在控制器里这样叫它

代码语言:javascript
运行
复制
return view('index', ['name' => 'Chibueze Agwu'])

然后在视图文件index.twig

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <h1>My Webpage</h1>
        {{ name }}
    </body>
</html>

这将输出

我的网页

芝麻阿格武

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

票数 8
EN

Stack Overflow用户

发布于 2021-05-19 09:27:04

试试这个,我希望它能帮到你。

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

作曲家要求“树枝/树枝:^3.0”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63174050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档