Laravel 4如何使用刀片母版页面将标题和元信息应用于每个页面

问题描述 投票:28回答:5

试图将个人标题和元描述应用到我的网站页面,但我不确定我尝试的方式是否非常干净。

master.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{ $title }}</title>
    <meta name="description" content="{{ $description }}">
</head>

个人页面

@extends('layouts.master')
<?php $title = "This is an individual page title"; ?>
<?php $description = "This is a description"; ?>

@section('content')

我觉得这是一种快速而肮脏的方式来完成工作,是否有更简洁的方法?

php html laravel laravel-4 blade
5个回答
86
投票

这也有效:

master.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>@yield('title')</title>
    <meta name="description" content="@yield('description')">
</head>

个人页面

@extends('layouts.master')

@section('title')
    This is an individual page title
@stop

@section('description')
    This is a description
@stop

@section('content')

或者如果你想缩短那些,可以这样做:

个人页面

@extends('layouts.master')

@section('title', 'This is an individual page title')
@section('description', 'This is a description')

@section('content')

8
投票

这应该工作:

@extends('layouts.master')
<?php View::share('title', 'title'); ?>

...

你也可以这样做:

@extends('views.coming-soon.layout', ['title' => 'This is an individual page title'])

2
投票

真的推荐这个:

https://github.com/artesaos/seotools

您将信息传递给视图需要的内容

SEOTools::setTitle($page->seotitle);
SEOTools::setDescription($page->seodescription);

1
投票

没有人认为最好的方法是使用facade(Site :: title(),Site :: description等)和mutators(通过Str :: macro)创建自己的类,自动检查标题,描述等是否格式正确(最大长度,添加类别,默认值,分隔符等)并在必要时将数据克隆到其他字段(title => og:title,description => og:description)?


0
投票

如果你想在你的标题中使用一个变量,所以它是从你的数据库动态生成的,我这样做:

master.blade.php

<title>@yield('title')</title>

article.blade.php

@section( 'title', '' . e($article->title) )

它使用https://laravel.com/docs/5.7/helpers#method-e

© www.soinside.com 2019 - 2024. All rights reserved.