模板仅在 html 中显示

Posted

技术标签:

【中文标题】模板仅在 html 中显示【英文标题】:Template is showing only in html 【发布时间】:2022-01-03 16:02:40 【问题描述】:

我在渲染一个在基本模板中扩展的页面时遇到问题,它只显示在没有 css 或 js 的 html 中。 这是显示的内容: img 我试图理解这个问题,这似乎不合逻辑,因为它是相同的暗示

这是我的树枝页面:

% extends 'base-back.html.twig'%

% block title %Edit DossierEmprunteur% endblock %

% block body %
    <h1>Edit DossierEmprunteur</h1>

     include('dossier_emprunteur/_form.html.twig', 'button_label': 'Update') 

    <a href=" path('dossier_emprunteur_index') ">back to list</a>


     include('dossier_emprunteur/_delete_form.html.twig') 
% endblock %

这是我的模板库:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="/docs/4.1/assets/img/favicons/favicon.ico">

    <title>Dashboard Template for Bootstrap</title>

    <link rel="canonical" href="https://getbootstrap.com/docs/4.1/examples/dashboard/">

    <!-- Bootstrap core CSS -->
    <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="dashboard.css" rel="stylesheet">
  </head>

  <body>
   include('inc/navbar.html.twig')



  <div class="container-fluid" style="margin-top: 50px">
      <div class="row">
        <nav class="col-md-2 d-none d-md-block bg-light sidebar">
          <div class="sidebar-sticky">
            <ul class="nav flex-column">
              <li class="nav-item">
                <a class="nav-link active" href="#">
                  <span data-feather="home"></span>
                  Dashboard <span class="sr-only">(current)</span>
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="file"></span>
                  Orders
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="shopping-cart"></span>
                  Products
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="users"></span>
                  Customers
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="bar-chart-2"></span>
                  Reports
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="layers"></span>
                  Integrations
                </a>
              </li>
            </ul>

            <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
              <span>Saved reports</span>
              <a class="d-flex align-items-center text-muted" href="#">
                <span data-feather="plus-circle"></span>
              </a>
            </h6>
            <ul class="nav flex-column mb-2">
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="file-text"></span>
                  Current month
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="file-text"></span>
                  Last quarter
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="file-text"></span>
                  Social engagement
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span data-feather="file-text"></span>
                  Year-end sale
                </a>
              </li>
            </ul>
          </div>
        </nav>


        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
          <div class="row">
            % block body %% endblock %

                  </div>




        </main>
      </div>
    </div>


    <!-- Bootstrap core javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery-slim.min.js"><\/script>')</script>
    <script src="../../assets/js/vendor/popper.min.js"></script>
    <script src="../../dist/js/bootstrap.min.js"></script>

    <!-- Icons -->
    <script src="https://unpkg.com/feather-icons/dist/feather.min.js"></script>
    <script>
      feather.replace()
    </script>


  </body>
</html>

在其他页面中它可以正确呈现,但在编辑页面中没有

【问题讨论】:

您尝试过什么解决问题的方法?你被困在哪里了?此模板与其他模板有何不同? 另外,您的浏览器的网络控制台是否告诉您有关此的任何信息?我认为这是 CSS 文件的加载问题,因为您使用 relative 路径来加载 CSS 文件 它是同一个模板,它在其他页面中正确呈现,但在“编辑”页面中它只显示没有 css 或 js 的 html。浏览器控制台显示没有问题 您尝试解决问题的方法是什么? Symfony 为资产提供组件是有充分理由的,请参阅 symfony.com/doc/current/… 【参考方案1】:

因为您使用的是相对路径,所以当您的 URL 获得额外级别时,文件将不会加载。

替换

<link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
<link href="dashboard.css" rel="stylesheet">

<script src="../../assets/js/vendor/popper.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>

# File location: "public/dist/css/bootstrap.min.css" #
<link href=" asset('dist/css/bootstrap.min.css') " rel="stylesheet">
# File location: "public/dashboard.css" #
<link href=" asset('dashboard.css') " rel="stylesheet">

# File location: "public/assets/js/vendor/popper.min.js" #
<script src=" asset('assets/js/vendor/popper.min.js') "></script>
# File location: "public/dist/js/bootstrap.min.js" #
<script src=" asset('dist/js/bootstrap.min.js') "></script>

Symfony 4.4 的文档:Linking to CSS, JavaScript and Image Assets

【讨论】:

以上是关于模板仅在 html 中显示的主要内容,如果未能解决你的问题,请参考以下文章

Drupal-仅在节点模板中显示主体值。

使用带有实际模板的 Joomla 仅在弹出窗口中显示文章

仅在特定文章中显示 Joomla 模块?

Kendo-TreeList :仅在 TreeList 的根级别的页脚模板

试图以 HTML 格式显示所有记录,但它仅在 HTML 中显示数据库中的最后一项

KnockoutJS:模板未在可观察数组更改时更新(仅在添加时,在删除时有效)