Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration options to add HTML to the head and scripts sections #1542

Merged
merged 16 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 43 additions & 0 deletions docs/digging-deeper/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,49 @@ If you don't want to have a footer on your site, you can set the `'footer'` conf
'footer' => 'false',
```

### Head and script HTML hooks

>info Note: The configuration options `head` and `scripts` were added in HydePHP v1.5. If you are running an older version, you need to use the Blade options, or upgrade your project.

While the most robust way to add custom HTML to the head or body of your site is to publish the Blade layouts, or pushing to the `meta` or `scripts` stacks,
you can also add custom HTML directly in the configuration file. This works especially well to quickly add things like analytics widgets or similar in the `hyde.yml` file, though the possibilities are endless.

To add custom HTML to your layouts, you can use the `head` and `scripts` configuration options in the `config/hyde.php` file (or the `hyde.yml` file).
The HTML will be added to the `<head>` section, or just before the closing `</body>` tag, respectively.
Note that the HTML is added to all pages. If you need to add HTML to a specific page, you will need to override the layout for that page.

```php
// filepath: config/hyde.php
'head' => '<!-- Custom HTML in the head -->',
'scripts' => '<!-- Custom HTML in the body -->',
```

```yaml
# filepath: hyde.yml
hyde:
head: "<!-- Custom HTML in the head -->"
scripts: "<!-- Custom HTML in the body -->"
```

You can of course also add multiple lines of HTML:

```php
// filepath: config/hyde.php
'head' => <<<HTML
<!-- Custom HTML in the head -->
<link rel="stylesheet" href="https://example.com/styles.css">
HTML,
```

```yaml
# filepath: hyde.yml

hyde:
head: |
<!-- Custom HTML in the head -->
<link rel="stylesheet" href="https://example.com/styles.css">
```

### Navigation Menu & Sidebar

A great time-saving feature of HydePHP is the automatic navigation menu and documentation sidebar generation.
Expand Down
17 changes: 17 additions & 0 deletions packages/framework/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 packages/framework/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 packages/framework/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 packages/framework/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
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
Loading