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 @includeOnce directive #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/DirectivesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class DirectivesRepository
{
/**
* @var array
*/
protected static $views_registry = [];

/**
* Register the directives.
*
Expand Down Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions src/directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
return '<?php endif; ?>';
},

/*
|---------------------------------------------------------------------
| @includeOnce
|---------------------------------------------------------------------
*/

'includeOnce' => function ($expression) {
return "<?php if (\Appstract\BladeDirectives\DirectivesRepository::viewWasNotIncluded({$expression})) {echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render();\Appstract\BladeDirectives\DirectivesRepository::addIncludedView({$expression});} ?>";
},

/*
|---------------------------------------------------------------------
| @mix
Expand Down
7 changes: 7 additions & 0 deletions tests/DirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down