Skip to content

Instantly share code, notes, and snippets.

@localdisk
Last active December 31, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save localdisk/7922335 to your computer and use it in GitHub Desktop.
Save localdisk/7922335 to your computer and use it in GitHub Desktop.
Laravel4 HTML macros.
<?php
// env == local の場合 noindex, nofollow する
HTML::macro('follow', function() {
if (App::environment() === 'local') {
return PHP_EOL . '<meta name="robots" content="noindex,nofollow" />';
}
return '';
});
// 配列を分解して keywords にセット
HTML::macro('keywords', function($keywords) {
if (!is_array($keywords)) {
$keywords = (array) $keywords;
}
if (count($keywords) === 0) {
return '';
}
$content = implode(',', $keywords);
return PHP_EOL . '<meta name="keywords" content="' . e($content) . '" />';
});
// description にセットする
HTML::macro('description', function($description) {
if (empty($description)) {
return '';
}
return PHP_EOL . '<meta name="description" content="' . e($description) . '" />';
});
// これを app の下において app/start/global.php で require する
// 以下使い方 View ではこう書く
<?= HTML::follow() ?>
<?= HTML::keywords(['SEO', 'あーだ', 'こーだ']) ?>
<?= HTML::description('説明とか') ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment