From 9d1134b8b967214fd73162860b66bc2a89079056 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 19:30:06 +0200 Subject: [PATCH] Create a helper for fluent metadata configuration --- config/hyde.php | 25 +++++++-------- resources/views/layouts/meta.blade.php | 9 +----- src/Meta.php | 43 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/Meta.php diff --git a/config/hyde.php b/config/hyde.php index 85f7c20a..b6bfa784 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -18,6 +18,7 @@ */ use Hyde\Framework\Features; +use Hyde\Framework\Meta; return [ @@ -70,24 +71,22 @@ | Global Site Meta Tags |-------------------------------------------------------------------------- | - | While you can add any number of meta tags in the meta.blade.php component, - | these settings allow you to easily add tags for the meta component. + | While you can add any number of meta tags in the meta.blade.php component + | using standard HTML, you can also use the Meta helper. To add a regular + | meta tag, use Meta::name() helper. To add an Open Graph property, use + | Meta::property() helper which also adds the `og:` prefix for you. | - | The `meta` array is for standard meta tags. See the examples below. - | The `og` array is for Open Graph properties. Do not include the `og:` prefix. + | Note that these global tags may be overridden by a page's meta tags. | */ 'meta' => [ - // 'author' => 'Mr. Hyde', - // 'twitter:creator' => '@hyde_php', - // 'description' => 'My Hyde Blog', - // 'keywords' => 'Static Sites, Blogs, Documentation', - 'generator' => 'HydePHP '.Hyde\Framework\Hyde::version(), - ], - - 'ogProperties' => [ - 'site_name' => $siteName, + Meta::name('author', 'Mr. Hyde'), + Meta::name('twitter:creator', '@hyde_php'), + Meta::name('description', 'My Hyde Blog'), + Meta::name('keywords', 'Static Sites, Blogs, Documentation'), + Meta::name('generator', 'HydePHP '.Hyde\Framework\Hyde::version()), + Meta::property('site_name', $siteName), ], /* diff --git a/resources/views/layouts/meta.blade.php b/resources/views/layouts/meta.blade.php index 15cf6d2b..6795640c 100644 --- a/resources/views/layouts/meta.blade.php +++ b/resources/views/layouts/meta.blade.php @@ -1,12 +1,5 @@ {{-- Config Defined Meta Tags --}} -@foreach (config('hyde.meta', []) as $name => $content) - -@endforeach - -@foreach (config('hyde.ogProperties', []) as $property => $content) - -@endforeach +{!! Hyde\Framework\Meta::render() !!} {{-- Add any extra tags to include in the section --}} @stack('meta') - diff --git a/src/Meta.php b/src/Meta.php new file mode 100644 index 00000000..49b124e2 --- /dev/null +++ b/src/Meta.php @@ -0,0 +1,43 @@ +'; + } + return null; + } + + public static function property(string $property, string $content, bool $ifConditionIsMet = true): ?string + { + if ($ifConditionIsMet) { + $property = static::formatOpenGraphProperty($property); + return ''; + } + return null; + } + + public static function render(array $overridesGlobalMeta = []): string + { + return implode("\n", + array_merge( + static::getGlobalMeta(), + $overridesGlobalMeta + ) + ); + } + + public static function getGlobalMeta(): array + { + return config('hyde.meta', []); + } + + protected static function formatOpenGraphProperty(string $property): string + { + return str_starts_with('og:', $property) ? $property : 'og:' . $property; + } +} \ No newline at end of file