【问题标题】:Laravel 5 Blade Templating Not WorkingLaravel 5 刀片模板不起作用
【发布时间】:2015-06-16 10:31:42
【问题描述】:

我试图了解 laravel 的基本刀片模板引擎,但我似乎无法通过一个基本示例,并且文档似乎没有太大帮助。有什么建议吗?

   -views
     --landing.blade.php
     --navbar.blade.php

landing.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

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

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    @yield('navbar')
    <h1>Hello, world!</h1>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

navbar.blade.php

@extends('landing')
@section('navbar')
    <div class="container">
        <nav class="navbar navbar-default">
          <div class="container-fluid">
            <div class="navbar-header">
              <a class="navbar-brand" href="#">
              </a>
            </div>
          </div>
        </nav>
    </div>
@stop

当我访问landing.blade.php 时唯一呈现的是“Hello World”。

【问题讨论】:

  • 可能你想使用@include而不是@yield
  • 这不会导致任何渲染。
  • 你的代码现在怎么样了?

标签: php laravel blade


【解决方案1】:

navbar.blade.php

<div class="container">
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">
          </a>
        </div>
      </div>
    </nav>
</div>

landing.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

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

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    @include('navbar')
    <h1>Hello, world!</h1>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

请阅读Laravel documentation,了解如何使用 Blade,尤其是如何生成内容。

您还可以使用section 和extends 使其更加灵活。

【讨论】:

  • 你是对的,当然,但你可能应该添加一个关于为什么的解释,以便 OP 可以更容易地了解问题所在。
  • 谢谢,不过文档中没有。 laravel.com/docs/5.0/templates 不给你答案,因为粗鲁。祝你有美好的一天。
  • 当然它在文档中,Note that views which extend a Blade layout simply override sections from the layout...,你覆盖部分,但是你没有在任何地方渲染导航栏
猜你喜欢
  • 2013-02-03
  • 2014-11-15
  • 2016-03-11
  • 2013-06-04
  • 2018-05-28
  • 2019-01-31
  • 2017-07-31
  • 2019-06-26
  • 2019-05-15
相关资源
最近更新 更多