简体   繁体   English

navbar-brand 中的 Laravel 动态页面标题

[英]Laravel dynamic page title in navbar-brand

I have layouts.app.blade.php where I have my <html> and <body> tags and also the <nav> .我有 layouts.app.blade.php ,其中有我的<html><body>标签以及<nav>
In the <body> I yield content for every page, so they basically extend this app.blade.php.<body>中,我为每个页面生成内容,所以他们基本上扩展了这个 app.blade.php。
All basic Laravel stuff so now I have this:所有基本的 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.我想用这个<a class="navbar-brand">来显示我的页面标题。 So this means it has to change for each template that is loaded (with @yield('content')) in this 'parent.blade.php'.所以这意味着它必须为这个'parent.blade.php'中加载的每个模板(使用@yield('content'))进行更改。

How would I do this using Laravel 5.2?我将如何使用 Laravel 5.2 执行此操作?

Many thanks非常感谢

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

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 空合并运算符

Blade view like this- 刀片视图如下 -

return view('front.list',compact('artists','letter'));

instead of- 代替-

return view('front.list',compact('artists'));

And now in your view you can use: 现在在您的视图中,您可以使用:

<title>Artists begging with {{ $letter }}</title>

in your controller declare a variable called title & compact it as below在您的控制器中声明一个名为 title 的变量并将其压缩如下

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

in your view page call the variable as below在您的视图页面中调用如下变量

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

Define this title in this format as given below in the main file like as (master.blade.php)在主文件中以如下格式定义此标题,如 (master.blade.php)

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

on page here All Company is my new title for the blade file you want to dynamic title on this page在此页面上所有公司是我要在此页面上动态标题的刀片文件的新标题

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

Another alternative that has worked for me in Laravel 9 and PHP 8 is to use a prop in a master layout.在 Laravel 9 和 PHP 8 中对我有用的另一种选择是在主布局中使用道具。 In my project I have only one layout so it's simple:在我的项目中,我只有一种布局,所以很简单:

<x-layout :title="' - Page Title'">
  <!-- content -->
</x-layout>

Then in the layout.blade.php file:然后在layout.blade.php文件中:

@props(['title'])
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Static Title{{$title ?? ""}}</title>
</head>
<body>
  <main>
    {{$slot}}
  </main>
</body>
</html>
Your can try this In you header.blade.php file put `@stack('title')`
instant of title tag
and 
in your `page.blade.php` file us put this
`@extends('view.layouts.main')
@push('title')
    <title> Home Page</title>
@endpush
@section('main-container')
Your content
@endsection
`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM