如何在Laravel中添加动态页面标题

问题描述 投票:1回答:2

[我想添加一个动态页面标题,就像我有一个帖子列表,并且当我单击打开的单个帖子详细信息页面时,在这种情况下,我希望该动态标题显示在标题文件中。

@extends('layouts.app')
@section('title', 'This is {{$post->title}} Post Page')
@section('contents')
<h3>{{$post->title}}</h3>
<p>{{$post->body}}</p>
@endsection

您可以在本节中看到。@section('title','这是{{$ post-> title}}帖子页'){{$ post-> title}},但无效。它显示出这样的内容。

这是标题); ?>帖子页

在app.blade.php上,我也有类似的东西。

<title>Laravel Practice - @yield('title')</title>
php laravel oop laravel-5
2个回答
5
投票

刀片的变量语法在@section调用中无效,因此您将要使用普通的旧PHP:

@section('title', 'This is ' . $post->title . ' Post Page')

如果$post->title是用户输入,您将希望逃避某些事情,以防XSS漏洞:

@section('title', 'This is ' . e($post->title) . ' Post Page')

-1
投票

您不能将{{$ post-> title}}留在''中,只需将其放出并与'。'连接即可。像这样:

@section('title', 'This is' . {{$post->title}} . 'Post Page')
© www.soinside.com 2019 - 2024. All rights reserved.