Last active
December 31, 2015 02:39
-
-
Save localdisk/7922335 to your computer and use it in GitHub Desktop.
Laravel4 HTML macros.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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