扩展名为.twig的文件不起作用

时间:2018-03-07 08:25:14

标签: twig

我是twig的新手,问题是当我想使用.twig文件时,它与.html.twig的工作方式不同。但是当我切换到.html文件工作正常。问题在哪里?

编辑:

的index.php:

<?php

use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

require __DIR__.'/../vendor/autoload.php';

$loader = new Twig_Loader_Filesystem('../templates');

$twig = new Twig_Environment($loader, array(
'cache' => '../compilation_cache',
));

$collection = new RouteCollection();
$collection->add('index', new Route('/', array(

)));

// Twig

if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
    throw new \RuntimeException('APP_ENV environment variable is not 
defined. You need to define environment variables for configuration or 
add "symfony/dotenv" as a Composer dependency to load variables from a 
.env file.');
}
(new Dotenv())->load(__DIR__.'/../.env'); 
}

$env = $_SERVER['APP_ENV'] ?? 'dev';
$debug = $_SERVER['APP_DEBUG'] ?? ('prod' !== $env);

if ($debug) {
umask(0000);

Debug::enable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), 
Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts(explode(',', $trustedHosts));
}

$kernel = new Kernel($env, $debug);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);


//Product list TEST TWIG->Delete if not using!
$products = 'products';
$products = array(
array('name' => 'Notebook', 'description' => 'Core i7', 'value' =>  
800.00, 'date_register' => '2017-06-22',),
array('name' => 'Mouse', 'description'=> 'Razer', 'value' =>  125.00, 
'date_register' => '2017-10-25',),
array('name' => 'Keyboard', 'description' => 'Mechanical Keyboard', 
'value'=>  250.00, 'date_register' => '2017-06-23',)
);

$template = $twig->load('index.twig');
echo $template->display(array('products' => $products));

index.twig:

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
<title>Twig Example</title>
</head>
<body>
<table border="1" style="width: 80%;">
<thead>
<tr>
    <td>Product</td>
    <td>Description</td>
    <td>Value</td>
    <td>Date</td>
</tr>
</thead>
<tbody>
{% for product in products %}
    <tr>
        <td>{{ product.name }}</td>
        <td>{{ product.description }}</td>
        <td>{{ product.value }}</td>
        <td>{{ product.date_register|date("m/d/Y") }}</td>
    </tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

我想从index.php文件中加载数组,但它让我错误:

无法在第1行的index.twig中找到模板“layout.html”(查看:../ template)。

我不再使用文件layout.html文件被删除。当我将index.twig文件重构为index.html时,它就会消失。

EDIT(2):

twig.yaml(/config/packages/twig.yaml):

twig:
paths: ['%kernel.project_dir%/templates']
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'

1 个答案:

答案 0 :(得分:0)

问题出在index.php文件中。当我评论/删除部分时:

if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
  throw new \RuntimeException('APP_ENV environment variable is not 
defined. You need to define environment variables for configuration or 
add "symfony/dotenv" as a Composer dependency to load variables from a 
.env file.');
  }
  (new Dotenv())->load(__DIR__.'/../.env');
}

$env = $_SERVER['APP_ENV'] ?? 'dev';
$debug = $_SERVER['APP_DEBUG'] ?? ('prod' !== $env);

if ($debug) {
  umask(0000);

Debug::enable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
  Request::setTrustedProxies(explode(',', $trustedProxies), 
Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
  Request::setTrustedHosts(explode(',', $trustedHosts));
}

$kernel = new Kernel($env, $debug);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

一切正常......