【问题标题】:Laravel dynamic page title in navbar-brand导航栏品牌中的 Laravel 动态页面标题
【发布时间】:2016-04-02 12:48:11
【问题描述】:

我有 layouts.app.blade.php,其中有我的 <html><body> 标签以及 <nav>
<body> 中,我为每个页面生成内容,所以他们基本上扩展了这个 app.blade.php。
所有基本的 Laravel 东西,所以现在我有了这个:

 <div class="navbar-header">
    <!-- Collapsed Hamburger -->
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#spark-navbar-collapse">
        <span class="sr-only">Toggle Navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <!-- Branding Image -->
    <a class="navbar-brand" href="/">
        *Dynamic page title*
    </a>
</div>
// ...
@yield('content')

我想用这个&lt;a class="navbar-brand"&gt; 来显示我的页面标题。所以这意味着它必须为在这个'parent.blade.php'中加载的每个模板(使用@yield('content'))进行更改。

我将如何使用 Laravel 5.2 做到这一点?

非常感谢

【问题讨论】:

  • 在视图中使用变量增强上述解决方案,如果你正在扩展一个布局(你应该),那么如果你有一个标题部分,你可以像这样渲染它:@section("title ","$字母")。感谢Blade的力量。希望这有用。

标签: php jquery twitter-bootstrap laravel dynamic


【解决方案1】:

在主文件中以如下格式定义此标题,如 (master.blade.php)

<title>Vendor | @yield('mytitle') </title>

在页面上 这里 All Company 是我要在此页面上动态标题的刀片文件的新标题

@section('mytitle', 'All Company')

【讨论】:

    【解决方案2】:

    在您的控制器中声明一个名为 title 的变量并将其压缩如下

        public function aboutUs()
        {
            //page title
            $title = 'Project | About Us';
            return view('project.about_us', compact('title'));
        }
    

    在您的视图页面中调用如下变量

    <title>{{ $title }}</title>
    
    

    【讨论】:

      【解决方案3】:

      例如,您可以将其传递给视图

      控制器

      $title = 'Welcome';
      
      return view('welcome', compact('title'));
      

      查看

      isset($title) ? $title : 'title';
      

      或php7

      $title ?? 'title';
      

      Null coalescing operator

      【讨论】:

        【解决方案4】:

        如果这是您下面的母版页标题

        <html>
        <head>
            <title>App Name - @yield('title')</title>
        </head>
        <body>
            @section('sidebar')
                This is the master sidebar.
            @show
        
            <div class="container">
                @yield('content')
            </div>
        </body>
        

        然后您的页面标题可以在您的刀片页面中更改,如下所示

        @extends('layouts.master')
        
        @section('title', 'Page Title')
        
        @section('sidebar')
        @parent
        
        <p>This is appended to the master sidebar.</p>
        @endsection
        
        @section('content')
        <p>This is my body content.</p>
        @endsection
        

        更多信息可以在这里找到Laravel Docs

        【讨论】:

        • 哦,这很简单。像魅力一样工作。
        • 我一直试图从控制器中命名标题,但现在我发现你的更好。感谢您的提示!
        猜你喜欢
        • 2017-05-30
        • 2018-07-03
        • 1970-01-01
        • 2019-01-26
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 2015-08-11
        • 2021-12-05
        相关资源
        最近更新 更多