如何在Laravel中为类别添加特定标题和描述

时间:2014-03-17 10:07:42

标签: php laravel

我正在尝试为基于Laravel的网站中的每个帖子和类别添加特定的标题和元描述。

到目前为止,我已经完成了帖子的标题和说明,但我也无法弄清楚如何为类别做这些。

这是我用于帖子的代码,它起作用了:

@if(isset($post->title))
  <title>{{ $post->title }}</title>
@else
  <title>{{ $settings->website_name }} - {{ $settings->website_description }}</title>
@endif

@if(isset($post->description))
<meta name="description" content="{{ $post->description }}" />
@else
<meta name="description" content="Example Example Example" />
@endif

我也尝试为类别做类似的事情:

@if(isset($categories->name) && ($categories->description))
<title>{{ categories->name }}</title>
<meta name="description" content="{{ categories->description }}">
@else
<title>Whatever</title>
<meta name="description" content="Whatever">
@endif

在此代码中,“categories”是我的类别表的名称,而“name”和“description”是它的列。

这就是我在error_log上的内容:

#0 /home/example/public_html/example/app/storage/views

/cba078e6a3a6e27c09d6787a6da4672d(22): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', '/home/example/...', 22, Array)
#1 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(38): include('/home/example/...')
#2 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(45): Illuminate\View\Engines\PhpEngine->evaluatePath('/home/example/...', Array)
#3 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/View.php(100): Illuminate\View\Engines\CompilerEngine->get('/home/example/...', Array)
#4 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/View.php(81): Illuminate\View\View->getContents()
#5 /home/example/public_html/fu in /home/gametops/public_html/fun/app/storage/views/cba078e6a3a6e27c09d6787a6da4672d on line 22

这就是我获取类别的方式:

<a href="#" class="dropdown-toggle categories" data-toggle="dropdown"><i class="fa fa-folder-open"></i> {{ Lang::get('category.categories') }} <b class="caret"></b><div class="nav-border-bottom"></div></a>

<ul class="dropdown-menu">
                <li>
                    @foreach ($categories as $category)
                      <a href="{{ URL::to('category') . '/' . slugify($category->name) }}">{{ $category->name }}</a>
                    @endforeach
                </li>
              </ul>

1 个答案:

答案 0 :(得分:2)

嗨,如果你使用的是刀片,那么简单只需要简单,如果你想把唯一的标题和描述放在那么修改刀片布局就像这样

<title>@yield('meta_title', 'Default Title')</title>
<meta name="description" content="@yield('meta_description', 'Default Description')" />

现在在您可以调用的任何视图中

@section('meta_title')
    Your title text will go here
@stop

@section('meta_description')
    Your description text will go here
@stop