Skip to content

Instantly share code, notes, and snippets.

@tillsanders
Last active August 29, 2015 14:03
Show Gist options
  • Save tillsanders/9db91983ce802027e197 to your computer and use it in GitHub Desktop.
Save tillsanders/9db91983ce802027e197 to your computer and use it in GitHub Desktop.
Automatically get page meta from language files with Laravel

Automatically get page meta from language files with Laravel

Use this in your blade layout:

<title>{{ trans('pages.'.Route::currentRouteName().'.title').trans('pages.title-suffix') }}</title>
<meta name="description" content="{{ trans('pages.'.Route::currentRouteName().'.description') }}" />
<meta name="keywords" content="{{ trans('pages.obligatory-keywords').trans('pages.'.Route::currentRouteName().'.keywords') }}" />

An example for lang//pages.php:

<?php

return array(
	'title-suffix' => ' | The Website',
	'obligatory-keywords' => 'these,are,obligatory,keywords,',
	'home' => array(
		'title' => 'Home',
		'keywords' => 'home',
		'description' => 'This is an example website.',
	),
);
@gpluess
Copy link

gpluess commented Mar 16, 2015

This is nice! Is it possible to use this when parts of the title are dynamic?

@tillsanders
Copy link
Author

Sure, why not!
<title>{{ $somethingDynamic.trans('pages.title-suffix') }}</title>
or, when using multiple template files:

@if(isset($somethingDynamic) && !empty($somethingDynamic))
    <title>{{ $somethingDynamic.trans('pages.title-suffix') }}</title>
@else
    <title>{{ trans('pages.'.Route::currentRouteName().'.title').trans('pages.title-suffix') }}</title>
@endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment