From af6646566f8d55e557ccd9447132f64b9c082314 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 12 Feb 2024 16:18:34 +0100 Subject: [PATCH 1/3] Add `Includes::html()` helper --- packages/framework/src/Support/Includes.php | 18 ++++++++++++++++ .../tests/Feature/IncludesFacadeTest.php | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/packages/framework/src/Support/Includes.php b/packages/framework/src/Support/Includes.php index 08ff8f003fa..c271475103e 100644 --- a/packages/framework/src/Support/Includes.php +++ b/packages/framework/src/Support/Includes.php @@ -60,6 +60,24 @@ public static function get(string $filename, ?string $default = null): ?string return file_get_contents($path); } + /** + * Get the HTML contents of a partial file in the includes directory. + * + * @param string $filename The name of the partial file, with or without the extension. + * @param string|null $default The default value to return if the partial is not found. + * @return string|null The raw contents of the partial file, or the default value if not found. + */ + public static function html(string $filename, ?string $default = null): ?string + { + $path = static::path(basename($filename, '.html').'.html'); + + if (! file_exists($path)) { + return $default === null ? null : $default; + } + + return file_get_contents($path); + } + /** * Get the rendered Markdown of a partial file in the includes directory. * diff --git a/packages/framework/tests/Feature/IncludesFacadeTest.php b/packages/framework/tests/Feature/IncludesFacadeTest.php index 3812567ce16..c77c5b51fe0 100644 --- a/packages/framework/tests/Feature/IncludesFacadeTest.php +++ b/packages/framework/tests/Feature/IncludesFacadeTest.php @@ -53,6 +53,27 @@ public function test_get_returns_default_value_when_not_found() $this->assertEquals('default', Includes::get('foo.txt', 'default')); } + public function test_html_returns_rendered_partial() + { + $expected = '

foo bar

'; + file_put_contents(Hyde::path('resources/includes/foo.html'), '

foo bar

'); + $this->assertEquals($expected, Includes::html('foo.html')); + Filesystem::unlink('resources/includes/foo.html'); + } + + public function test_html_returns_efault_value_when_not_found() + { + $this->assertNull(Includes::html('foo.html')); + $this->assertEquals('

default

', Includes::html('foo.html', '

default

')); + } + + public function test_html_with_and_without_extension() + { + file_put_contents(Hyde::path('resources/includes/foo.html'), '# foo bar'); + $this->assertEquals(Includes::html('foo.html'), Includes::html('foo')); + Filesystem::unlink('resources/includes/foo.html'); + } + public function test_markdown_returns_rendered_partial() { $expected = "

foo bar

\n"; From 7b2f71310db50dd334f0428b01686a059cfb8306 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 12 Feb 2024 18:42:15 +0100 Subject: [PATCH 2/3] Document HTML includes --- docs/digging-deeper/helpers.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/digging-deeper/helpers.md b/docs/digging-deeper/helpers.md index 8f18d425248..447dd723c8a 100644 --- a/docs/digging-deeper/helpers.md +++ b/docs/digging-deeper/helpers.md @@ -80,6 +80,20 @@ Includes::blade('banner.blade.php'); Includes::blade('banner', 'Default content'); ``` +### HTML Includes + +Gets the raw HTML of a partial file in the includes directory. Supplying the file extension is optional. + +```php +use Hyde\Support\Includes; + +Includes::html('footer'); +Includes::html('footer.html'); + +// With default value if the file does not exist +Includes::html('footer', 'Default content'); +``` + ### Directory Structure Example Here is an example of the directory structure for includes: From 7a09852be72f1db92b1dfd438016cb7cfe2354c5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 12 Feb 2024 18:47:51 +0100 Subject: [PATCH 3/3] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 84e1f53b946..3199efc7100 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -21,6 +21,7 @@ This serves two purposes: ### Added - Added the existing `media_extensions` option to the `hyde` configuration file in https://github.com/hydephp/develop/pull/1531 +- Added an `html` helper to the `Includes` facade in https://github.com/hydephp/develop/pull/1552 ### Changed - Renamed local template variable `$document` to `$article` to better match the usage in https://github.com/hydephp/develop/pull/1506