【问题标题】:Symfony 4 - Unable to find templateSymfony 4 - 找不到模板
【发布时间】:2018-08-05 02:09:55
【问题描述】:

我将我的项目更新到 Symfony4。 当我使用表单转到某个页面时,出现此错误:

Unable to find template "StarRatingBundle::rating.html.twig" 
(looked into: /templates, /templates, \vendor\symfony\twig-bridge/Resources/views/Form).

我使用blackknight467/star-rating-bundle 一个捆绑包,它提供了一个树枝扩展来拥有一个星级评分系统。错误中的模板位于此捆绑包Resources/views 文件夹中。

Twig.yaml

twig:
    paths: ['%kernel.project_dir%/templates']
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes:
        - 'Form/fields.html.twig'

【问题讨论】:

  • 你的 Symfony 是哪个版本的?
  • 我最近更新到 Symfony4
  • 不鼓励在树枝模板路径中使用冒号。您可以将它们与适当的配置更改一起使用,但最好使用所谓的 twig 命名空间。类似 '@StarRating/rating.html.twig' 如果您不确定 @StarRating 指向的确切位置,请使用 "bin/console debug:twig" More Details
  • 真正的问题是在出现错误的页面中,我没有使用这个模板,我找不到错误来自哪里。 StarRatingBundle::rating.html.twig 在我的代码中的唯一用途是在包源中。我什至尝试用你所说的在供应商中更改它,但错误仍然存​​在。
  • @KristenJoseph-Delaffon 在这种情况下,向下滚动到链接答案的底部并调整框架的 config.yaml 文件。您基本上需要启用 Symfony 模板组件才能使包名称映射工作。请注意,StarRatingBundle 仍使用旧样式格式这一事实意味着它尚未针对 4.0 进行更新。可能还会遇到其他问题。

标签: php symfony templates twig


【解决方案1】:

Symfony 4+ 不支持“x:x:x.html.twig”表示法。你需要安装composer require templating

然后通过添加在你的 framework.yaml 中激活它

templating: engines: ['twig'].

文件 > 使缓存无效并重新启动。这应该允许 Symfony 找到您的模板。

【讨论】:

    【解决方案2】:

    我在创建 symfony 5 捆绑包时遇到了这个问题。确保将树枝模板保存在 <project-folder>/Resources/views 文件夹中。在你的 Controller 中,以HelloworldController 为例:

    `<?php
        namespace Example\HelloBundle\Controller;
        use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
        use Symfony\Component\HttpFoundation\Response;
        use Symfony\Component\Routing\Annotation\Route;
    
        class HelloController extends AbstractController
        {
            public function index(): Response
            {
                return $this->render('@<BundleName>/hello/index.html.twig',           [
                'controller_name' => 'HelloController',
                ]);
            }
        }
    

    是您的捆绑包的名称,不带捆绑包后缀。例如,如果您在 Bundles.php 中将您的捆绑包注册为 Example\HelloBundle\ExampleHelloBundle::class =&gt; ['all' =&gt; true],,那么 &lt;BundleName&gt; 就是 ExampleHello

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多