【问题标题】:how to solve "Twig\Error\LoaderError" in codeigniter如何解决 codeigniter 中的“Twig\Error\LoaderError”
【发布时间】:2019-10-04 11:08:49
【问题描述】:

我正在我的 codeigniter 文件夹中设置 twig。

在运行 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;

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

【问题讨论】:

  • VIEWPATH 的值是多少?这是您定义的常量吗?这应该指向您存储模板的文件夹
  • 我没有在 VIEWPATH 中设置任何值。如何设置这个值以及在哪里?
  • 只需将 VIEWPATH 更改为您的文件夹的位置,例如$config['twig']['template_dir'] = __DIR__ . '/../path/to/folder';
  • viewpath 是 codeigniter 中的一个常量,指向 application/views
  • 现在我的问题解决了。感谢您的关注。

标签: codeigniter twig


【解决方案1】:

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

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

代替 'php' 我使用 'twig' 选项来解决这个问题。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2020-05-24
    • 2014-06-15
    • 1970-01-01
    相关资源
    最近更新 更多