diff --git a/src/DirectivesRepository.php b/src/DirectivesRepository.php index 8083762..b2d7842 100644 --- a/src/DirectivesRepository.php +++ b/src/DirectivesRepository.php @@ -6,6 +6,11 @@ class DirectivesRepository { + /** + * @var array + */ + protected static $views_registry = []; + /** * Register the directives. * @@ -42,4 +47,23 @@ public static function stripQuotes($expression) { return str_replace(["'", '"'], '', $expression); } + + /** + * Check if a view has not been included yet. + * @param string $view + * + * @return bool + */ + public static function viewWasNotIncluded($view) + { + return ! in_array($view, static::$views_registry); + } + + /** + * @param string $view + */ + public static function addIncludedView($view) + { + static::$views_registry[] = $view; + } } diff --git a/src/directives.php b/src/directives.php index e3fe454..97afedf 100644 --- a/src/directives.php +++ b/src/directives.php @@ -64,6 +64,16 @@ return ''; }, + /* + |--------------------------------------------------------------------- + | @includeOnce + |--------------------------------------------------------------------- + */ + + 'includeOnce' => function ($expression) { + return "make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render();\Appstract\BladeDirectives\DirectivesRepository::addIncludedView({$expression});} ?>"; + }, + /* |--------------------------------------------------------------------- | @mix diff --git a/tests/DirectivesTest.php b/tests/DirectivesTest.php index c0e6b02..fe62f4c 100644 --- a/tests/DirectivesTest.php +++ b/tests/DirectivesTest.php @@ -42,6 +42,13 @@ public function test_is_not_null() $this->assertBladeRenders('It is not null', $blade, ['variable' => 'not null']); } + public function test_include_once() + { + $blade = "@includeOnce('not-checkout')"; + $this->assertBladeRenders('Do something only if this is not the checkout', $blade); + $this->assertBladeRenders('', $blade); // The second time the view shouldn't be included + } + public function test_mix() { // See /tests/laravel/public/mix-manifest.json for CSS file name