Skip to content

Commit

Permalink
Create a helper for fluent metadata configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 17, 2022
1 parent 10ee751 commit 9d1134b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
25 changes: 12 additions & 13 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

use Hyde\Framework\Features;
use Hyde\Framework\Meta;

return [

Expand Down Expand Up @@ -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),
],

/*
Expand Down
9 changes: 1 addition & 8 deletions resources/views/layouts/meta.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{{-- Config Defined Meta Tags --}}
@foreach (config('hyde.meta', []) as $name => $content)
<meta name="{{ $name }}" content="{{ $content }}">
@endforeach

@foreach (config('hyde.ogProperties', []) as $property => $content)
<meta property="og:{{ $property }}" content="{{ $content }}">
@endforeach
{!! Hyde\Framework\Meta::render() !!}

{{-- Add any extra tags to include in the <head> section --}}
@stack('meta')

43 changes: 43 additions & 0 deletions src/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Hyde\Framework;

class Meta
{
public static function name(string $name, string $content, bool $ifConditionIsMet = true): ?string
{
if ($ifConditionIsMet) {
return '<meta name="' . e($name) . '" content="' . e($content) . '">';
}
return null;
}

public static function property(string $property, string $content, bool $ifConditionIsMet = true): ?string
{
if ($ifConditionIsMet) {
$property = static::formatOpenGraphProperty($property);
return '<meta property="' . e($property) . '" content="' . e($content) . '">';
}
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;
}
}

0 comments on commit 9d1134b

Please sign in to comment.