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

[5.6] Add Blade::include for include aliases #23172

Merged
merged 3 commits into from
Feb 19, 2018
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
18 changes: 18 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,24 @@ public function directive($name, callable $handler)
$this->customDirectives[$name] = $handler;
}

/**
* Register an include alias directive.
*
* @param string $path
* @param string $alias
* @return void
*/
public function include($path, $alias = null)
{
$alias = $alias ?: array_last(explode('.', $path));

$this->directive($alias, function ($expression) use ($path) {
$expression = $this->stripParentheses($expression) ?: '[]';

return "<?php echo \$__env->make('{$path}', {$expression}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
});
}

/**
* Get the list of custom directives.
*
Expand Down
47 changes: 47 additions & 0 deletions tests/View/Blade/BladeCustomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,51 @@ public function testCustomComponentsDefaultAlias()
<?php echo $__env->renderComponent(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomComponentsWithExistingDirective()
{
$this->compiler->component('app.components.foreach');

$string = '@foreach
@endforeach';
$expected = '<?php $__env->startComponent(\'app.components.foreach\'); ?>
<?php echo $__env->renderComponent(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomIncludes()
{
$this->compiler->include('app.includes.input', 'input');

$string = '@input';
$expected = '<?php echo $__env->make(\'app.includes.input\', [], array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomIncludesWithData()
{
$this->compiler->include('app.includes.input', 'input');

$string = '@input([\'type\' => \'email\'])';
$expected = '<?php echo $__env->make(\'app.includes.input\', [\'type\' => \'email\'], array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomIncludesDefaultAlias()
{
$this->compiler->include('app.includes.input');

$string = '@input';
$expected = '<?php echo $__env->make(\'app.includes.input\', [], array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomIncludesWithExistingDirective()
{
$this->compiler->include('app.includes.foreach');

$string = '@foreach';
$expected = '<?php echo $__env->make(\'app.includes.foreach\', [], array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}