php 导航栏品牌中的 Laravel 动态页面标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34497428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:03:00  来源:igfitidea点击:

Laravel dynamic page title in navbar-brand

phpjquerytwitter-bootstraplaraveldynamic

提问by nclsvh

I have layouts.app.blade.php where I have my <html>and <body>tags and also the <nav>.
In the <body>I yield content for every page, so they basically extend this app.blade.php.
All basic Laravel stuff so now I have this:

我有 layouts.app.blade.php,其中有我的<html><body>标签以及<nav>.
<body>I yield 每个页面的内容,所以他们基本上扩展了这个 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')

And I would like to use this <a class="navbar-brand">to display my pagetitle. So this means it has to change for each template that is loaded (with @yield('content')) in this 'parent.blade.php'.

我想用它<a class="navbar-brand">来显示我的页面标题。因此,这意味着它必须针对此“parent.blade.php”中加载的每个模板(使用@yield('content'))进行更改。

How would I do this using Laravel 5.2?

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

Many thanks

非常感谢

回答by PhillipMwaniki

If this is your master page title below

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

<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>

then your page title can be changed in your blade page like below

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

@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

More information can be found here Laravel Docs

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

回答by Erik

You can pass it to a view for example

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

Controller

控制器

$title = 'Welcome';

return view('welcome', compact('title'));

View

看法

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

or php7

或 php7

$title ?? 'title';

Null coalescing operator

空合并运算符