Add Context::addLink() and Context::getLinks() to register HTML header content such as <link rel="preconnect">

This commit is contained in:
Kijin Sung 2024-05-05 16:29:56 +09:00
parent ac895d64cb
commit b951d50841
2 changed files with 27 additions and 1 deletions

View file

@ -41,6 +41,7 @@ class Context
public $html_footer = ''; public $html_footer = '';
public $body_header = ''; public $body_header = '';
public $body_class = []; public $body_class = [];
public $link_rels = [];
/** /**
* The current page's SEO information. * The current page's SEO information.
@ -2494,7 +2495,7 @@ class Context
*/ */
public static function getHtmlHeader(): string public static function getHtmlHeader(): string
{ {
return self::$_instance->html_header; return self::$_instance->html_header === '' ? '' : (self::$_instance->html_header . "\n");
} }
/** /**
@ -2602,6 +2603,28 @@ class Context
return self::$_instance->html_footer; return self::$_instance->html_footer;
} }
/**
* Add <link> such as <link rel="preconnect" href="https://webfonts.example.com" />
*
* @param string $url
* @param string $rel
* @return void
*/
public static function addLink(string $url, string $rel): void
{
self::$_instance->link_rels[$url] = $rel;
}
/**
* Returns added <link> list
*
* @return array
*/
public static function getLinks(): array
{
return self::$_instance->link_rels;
}
/** /**
* Get config file * Get config file
* *

View file

@ -50,6 +50,9 @@
@if (!empty($mobicon_url)) @if (!empty($mobicon_url))
<link rel="apple-touch-icon" href="{{ $mobicon_url }}" /> <link rel="apple-touch-icon" href="{{ $mobicon_url }}" />
@endif @endif
@foreach (Context::getLinks() as $link_url => $link_rel)
<link rel="{{ $link_rel }}" href="{{ $link_url }}" />
@endforeach
<!-- OTHER HEADERS --> <!-- OTHER HEADERS -->
@foreach (Context::getOpenGraphData() as $og_metadata) @foreach (Context::getOpenGraphData() as $og_metadata)