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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21804973/
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-25 04:27:26  来源:igfitidea点击:

Laravel 4 how to apply title and meta information to each page with blade master page

phphtmllaravellaravel-4blade

提问by Mitch

Trying to apply an individual title and meta description to my websites pages, but I'm not sure if the way I'm attempting is very clean.

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

master.blade.php

master.blade.php

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

individual page

个人页面

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

@section('content')

I feel like this is a quick and dirty way to get the job done, is there a cleaner way?

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

回答by zeckdude

This works as well:

这也有效:

master.blade.php

master.blade.php

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

individual page

个人页面

@extends('layouts.master')

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

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

@section('content')

or if you want to shorten that some more, alternately do this:

或者,如果您想进一步缩短它,请交替执行以下操作:

individual page

个人页面

@extends('layouts.master')

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

@section('content')

回答by Antonio Carlos Ribeiro

This should work:

这应该有效:

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

...

You can also do this:

你也可以这样做:

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

回答by Paolo Resteghini

Really recommend this:

真的推荐这个:

https://github.com/artesaos/seotools

https://github.com/artesaos/seotools

You pass the information to the the view require the content

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

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

回答by user2020432

no one think that the best way is to create your own class with facade (Site::title(), Site::description etc) and mutators (via Str::macro) that automatically check if title, description etc is in right format (max length, adding categories, defaults, separators, etc) and clone data to others fields (title => og:title, description => og:description) if necessary?

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

回答by Jquestions

If you want to use a variable in your title so it's dynamically generated from your DB, I do it like this:

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

master.blade.php

master.blade.php

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

article.blade.php

文章.blade.php

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

It uses https://laravel.com/docs/5.7/helpers#method-e

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