如何在codeigniter中解决“ Twig \ Error \ LoaderError”

时间:2019-05-17 06:44:44

标签: codeigniter twig

我在我的codeigniter文件夹中设置了树枝。

在运行codeigniter应用程序时,它显示以下错误,

An uncaught Exception was encountered
Type: Twig\Error\LoaderError

Message: Unable to find template "forms/admission.php" (looked into: C:\wamp\www\openApplication\application\views).

Filename: C:\wamp\www\openApplication\application\vendor\twig\twig\src\Loader\FilesystemLoader.php

Line Number: 266

Backtrace:

File: C:\wamp\www\openApplication\application\vendor\twig\twig\src\Loader\FilesystemLoader.php
Line: 161
Function: findTemplate

File: C:\wamp\www\openApplication\application\vendor\twig\twig\src\Environment.php
Line: 351
Function: getCacheKey

File: C:\wamp\www\openApplication\application\vendor\twig\twig\src\Environment.php
Line: 445
Function: getTemplateClass

File: C:\wamp\www\openApplication\application\vendor\twig\twig\src\Environment.php
Line: 423
Function: loadTemplate

File: C:\wamp\www\openApplication\application\vendor\twig\twig\src\Environment.php
Line: 384
Function: load

File: C:\wamp\www\openApplication\application\libraries\Twig.php
Line: 33
Function: render

File: C:\wamp\www\openApplication\application\controllers\Admission.php
Line: 27
Function: render

File: C:\wamp\www\openApplication\index.php
Line: 315
Function: require_once

我要运行twig文件格式(.twig)。

我的项目代码在下面,

控制器页面

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admission extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */


    public function index()
    {

        $output = "";
        $output .= $this->twig->render('forms/admission');
        $this->output->set_output($output);
    }

}

twig.config

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$config['twig']['template_dir'] = VIEWPATH;
$config['twig']['template_ext'] = 'php';
$config['twig']['environment']['autoescape'] = TRUE;
$config['twig']['environment']['cache'] = FALSE;
$config['twig']['environment']['debug'] = FALSE;

MY_Loder.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader {
    public function view($template, $data = array(), $return = FALSE) {
        $CI =& get_instance();

        try {
            $output = $CI->twig->render($template, $data);
        } catch (Exception $e) {
            show_error(htmlspecialchars_decode($e->getMessage()), 500, 'Twig Exception');
        }

        // Return the output if the return value is TRUE.
        if ($return === TRUE) {
            return $output;
        }

        // Otherwise append to output just like a view.
        $CI->output->append_output($output);
    }
}

config / autoload.php

  

$ autoload ['libraries'] = array('twig');

     

$ autoload ['config'] = array('twig');

config / config.php

  

$ config ['composer_autoload'] = TRUE;

如果我的编码中有任何错误。

1 个答案:

答案 0 :(得分:0)

在下面的配置文件中,我使用php作为文件扩展名。喜欢

  

$ config ['twig'] ['template_ext'] ='php';

我建议使用“ twig”选项来解决此问题。

  

$ config ['twig'] ['template_ext'] ='twig';

现在我的.twig扩展文件可以正常工作了。