From baec3d2c20a691900ccd94071c946b222ced0a69 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 12 Feb 2024 17:49:59 +0000 Subject: [PATCH] Merge pull request #1552 from hydephp/improved-includes Add a new `Includes::html()` helper https://github.com/hydephp/develop/commit/56242aba0b34c7a7fd7e5f669f9b9aebc28046d5 --- src/Support/Includes.php | 18 ++++++++++++++++++ tests/Feature/IncludesFacadeTest.php | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Support/Includes.php b/src/Support/Includes.php index 08ff8f00..c2714751 100644 --- a/src/Support/Includes.php +++ b/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/tests/Feature/IncludesFacadeTest.php b/tests/Feature/IncludesFacadeTest.php index 3812567c..c77c5b51 100644 --- a/tests/Feature/IncludesFacadeTest.php +++ b/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";