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

标签 symfony twig

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

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/

相关文章:

php - 如何获取 Google Charts 中的数据 [Symfony 4]

forms - Symfony 2 Form,字段集合不显示

forms - Symfony 表单创建新对象并创建第一个一对多对象

symfony - 将变量传递到模板名称中包含变量的包含的 Twig 模板中

symfony - 如何检查今天是否在嫩枝中的两个日期之间?

symfony - 关闭Symfony 2/ Twig 中的转义

php - Symfony2 中的本地化错误页面

html - 使用 Symfony 在 css 中的图像

mysql - Symfony2.2 Swiftmailer 无法在 AWS-EC2 服务器站点中工作

Symfony twig 如何将类添加到表单行