Skip to content

Commit 09c240e

Browse files
Added "snippet" blade feature
1 parent a57dca1 commit 09c240e

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
3131
Concerns\CompilesLoops,
3232
Concerns\CompilesRawPhp,
3333
Concerns\CompilesSessions,
34+
Concerns\CompilesSnippets,
3435
Concerns\CompilesStacks,
3536
Concerns\CompilesStyles,
3637
Concerns\CompilesTranslations,
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Illuminate\View\Compilers\Concerns;
4+
5+
trait CompilesSnippets
6+
{
7+
/**
8+
* Compile the snippet statements into valid PHP.
9+
*
10+
* @param string $expression
11+
* @return string
12+
*/
13+
protected function compileSnippet($expression)
14+
{
15+
[$function, $args] = $this->extractSnippetParts($expression);
16+
17+
return implode("\n", [
18+
"<?php if (! isset(\${$function})):",
19+
'$'.$function.' = static function('.$args.') {',
20+
'?>',
21+
]);
22+
}
23+
24+
/**
25+
* Compile the endsnippet statements into valid PHP.
26+
*
27+
* @param string $expression
28+
* @return string
29+
*/
30+
protected function compileEndSnippet($expression)
31+
{
32+
return implode("\n", [
33+
'<?php } ?>',
34+
'<?php endif; ?>',
35+
]);
36+
}
37+
38+
/**
39+
* Compile the renderSnippet statements into valid PHP.
40+
*
41+
* @param string $expression
42+
* @return string
43+
*/
44+
protected function compileRenderSnippet($expression)
45+
{
46+
[$function, $args] = $this->extractSnippetParts($expression);
47+
48+
return '<?php echo $'.$function.'('.$args.'); ?>';
49+
}
50+
51+
/**
52+
* Analyse the snippet expression and extract the function and optional arguments.
53+
*
54+
* @param string $expression
55+
* @return array
56+
*/
57+
protected function extractSnippetParts($expression)
58+
{
59+
$functionParts = explode(',', $this->stripParentheses($expression));
60+
61+
$function = trim(array_shift($functionParts), "'\" ");
62+
63+
if (empty($function)) {
64+
$function = 'function';
65+
}
66+
67+
$function = "__snippet_{$function}";
68+
69+
$args = trim(implode(',', $functionParts));
70+
71+
return [$function, $args];
72+
}
73+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\View\Blade;
4+
5+
class BladeSnippetStatementsTest extends AbstractBladeTestCase
6+
{
7+
public function testSnippetStatementsAreCompiled()
8+
{
9+
$string = '@snippet
10+
default breeze
11+
@endsnippet
12+
13+
@snippet ("bar")
14+
breeze with double quotes
15+
@endsnippet
16+
17+
@snippet (\'foo\', $bar)
18+
breeze with single quotes {{ $bar }}
19+
@endsnippet
20+
21+
@snippet (foobar, string $barfoo)
22+
breeze without quotes {{ $barfoo }}
23+
@endsnippet';
24+
$expected = '<?php if (! isset($__snippet_function)):
25+
$__snippet_function = static function() {
26+
?>
27+
default breeze
28+
<?php } ?>
29+
<?php endif; ?>
30+
31+
<?php if (! isset($__snippet_bar)):
32+
$__snippet_bar = static function() {
33+
?>
34+
breeze with double quotes
35+
<?php } ?>
36+
<?php endif; ?>
37+
38+
<?php if (! isset($__snippet_foo)):
39+
$__snippet_foo = static function($bar) {
40+
?>
41+
breeze with single quotes <?php echo e($bar); ?>
42+
43+
<?php } ?>
44+
<?php endif; ?>
45+
46+
<?php if (! isset($__snippet_foobar)):
47+
$__snippet_foobar = static function(string $barfoo) {
48+
?>
49+
breeze without quotes <?php echo e($barfoo); ?>
50+
51+
<?php } ?>
52+
<?php endif; ?>';
53+
$this->assertEquals($expected, $this->compiler->compileString($string));
54+
}
55+
56+
public function testRenderSnippetStatementsAreCompiled()
57+
{
58+
$string = '@renderSnippet
59+
60+
@renderSnippet ("bar")
61+
62+
@renderSnippet (\'foo\', $bar)
63+
64+
@renderSnippet (foobar, $barfoo)';
65+
$expected = '<?php echo $__snippet_function(); ?>
66+
67+
<?php echo $__snippet_bar(); ?>
68+
69+
<?php echo $__snippet_foo($bar); ?>
70+
71+
<?php echo $__snippet_foobar($barfoo); ?>';
72+
$this->assertEquals($expected, $this->compiler->compileString($string));
73+
}
74+
}

0 commit comments

Comments
 (0)