gpt4 book ai didi

symfony - 无法加载 Twig 模板 (Symfony 2.8.8)

转载 作者:行者123 更新时间:2023-12-02 08:19:30 25 4
gpt4 key购买 nike

所以我的文件夹结构如下:

enter image description here

根据 Symfony 手册(click here),我应该能够加载我的 Twig 模板,但 Symfony 找不到它。

enter image description here

知道我可能做错了什么吗?谢谢。


我试过的代码

    return $this->render(
"AppBundle:DisplayForm.html.twig"
, array(
"form" => $form->createView()
);

return $this->render(
"DisplayForm.html.twig"
, array(
"form" => $form->createView()
)
);

我的应用/AppKernel.php 的内容

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),

new Symfony\Bundle\AsseticBundle\AsseticBundle(),
);

if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}

return $bundles;
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}

app/config/config.yml 的内容

imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en

framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true

# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"

# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"

orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }

assetic:
debug: '%kernel.debug%'
use_controller: '%kernel.debug%'
filters:
cssrewrite: ~

最佳答案

因为您将模板存储在包中,所以您还必须在 View 中引用目录 - 在本例中为“空”目录。

AppBundle::DisplayForm.html.twig

注意目录的 ::

如果您将整个 AppBundle/Resources/views/目录移动到 app/Resources/views/那么您将能够像普通子目录一样引用它们:

引用模板存储在:

app/Resources/views/           | AcmeDemoBundle/Resources/views
----------------------------- | -------------------------------
index.html.twig | AcmeDemoBundle::index.html.twig
Default/subdir/index.html.twig | AcmeDemoBundle:Default:subdir/index.html.twig
Default/subdir/index.html.twig | AcmeDemoBundle:Default/subdir:index.html.twig

关于symfony - 无法加载 Twig 模板 (Symfony 2.8.8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38561235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com