Skip to content

Commit

Permalink
Merge pull request #597 from hydephp/add-configuration-options-to-add…
Browse files Browse the repository at this point in the history
…-html-to-the-head-and-scripts-sections

Add configuration options to add HTML to the head and scripts sections
  • Loading branch information
caendesilva authored Feb 13, 2024
2 parents 444873f + 17614df commit 2465936
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
17 changes: 17 additions & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@
Meta::property('site_name', env('SITE_NAME', 'HydePHP')),
],

/*
|--------------------------------------------------------------------------
| Custom head and script HTML hooks
|--------------------------------------------------------------------------
|
| While the best way to add custom `<head>` and `<body>` code is to use the
| Blade components, you can also add them here. This is useful for adding
| scripts like analytics codes, chat widgets, or even custom styles.
|
*/

// Add any extra HTML to include in the <head> tag
'head' => '',

// Add any extra HTML to include before the closing <body> tag
'scripts' => '',

/*
|--------------------------------------------------------------------------
| Features
Expand Down
5 changes: 4 additions & 1 deletion resources/views/layouts/head.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
{{-- Check the local storage for theme preference to avoid FOUC --}}
<meta id="meta-color-scheme" name="color-scheme" content="{{ config('hyde.default_color_scheme', 'light') }}">
<script>if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.documentElement.classList.add('dark'); document.getElementById('meta-color-scheme').setAttribute('content', 'dark');} else { document.documentElement.classList.remove('dark') } </script>
@endif
@endif

{{-- If the user has defined any custom head tags, render them here --}}
{!! config('hyde.head') !!}
5 changes: 4 additions & 1 deletion resources/views/layouts/scripts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ function toggleTheme() {
</script>

{{-- Add any extra scripts to include before the closing <body> tag --}}
@stack('scripts')
@stack('scripts')

{{-- If the user has defined any custom scripts, render them here --}}
{!! config('hyde.scripts') !!}
7 changes: 7 additions & 0 deletions tests/Unit/Views/HeadComponentViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public function testComponentIncludesStylesView()
$this->assertStringContainsString("@include('hyde::layouts.styles')", $this->renderTestView());
}

public function testCanAddHeadHtmlFromConfigHook()
{
config(['hyde.head' => '<meta name="custom-hook" content="foo">']);

$this->assertStringContainsString('<meta name="custom-hook" content="foo">', $this->renderTestView());
}

protected function escapeIncludes(string $contents): string
{
return str_replace('@include', '@@include', $contents);
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Views/ScriptsComponentViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public function test_component_uses_relative_path_to_app_js_file_for_nested_page
Filesystem::unlink('_media/app.js');
}

public function testCanAddHeadHtmlFromConfigHook()
{
config(['hyde.scripts' => '<script src="custom-hook.js"></script>']);

$this->assertStringContainsString('<script src="custom-hook.js"></script>', $this->renderTestView());
}

public function test_scripts_can_be_pushed_to_the_component_scripts_stack()
{
view()->share('routeKey', '');
Expand Down

0 comments on commit 2465936

Please sign in to comment.