-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] Adds a
@antlers @endantlers
Blade Directive Pair (#8692)
* Adds a new antlers/endantlers Blade directive pair * Removes the dedicated Blade parser (PHP 8.1+ requirement)
- Loading branch information
1 parent
5404acc
commit 21a4e59
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Statamic\View\Blade; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
class AntlersBladePrecompiler | ||
{ | ||
public static function compile(string $content): string | ||
{ | ||
if (! Str::contains($content, ['@antlers', '@endantlers'])) { | ||
return $content; | ||
} | ||
|
||
$pattern = '/@antlers(.*?)@endantlers/s'; | ||
|
||
preg_match_all($pattern, $content, $matches); | ||
|
||
if (! $matches || count($matches) != 2) { | ||
return $content; | ||
} | ||
|
||
for ($i = 0; $i < count($matches[0]); $i++) { | ||
$original = $matches[0][$i]; | ||
$innerContent = $matches[1][$i]; | ||
|
||
$contentHash = sha1($innerContent); | ||
$fileName = 'antlers_'.$contentHash; | ||
|
||
file_put_contents(storage_path('framework/views/'.$fileName.'.antlers.html'), $innerContent); | ||
|
||
$content = str_replace($original, '@include(\'compiled__views::'.$fileName.'\')', $content); | ||
} | ||
|
||
return $content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Tests\View\Blade; | ||
|
||
use Illuminate\Support\Facades\Blade; | ||
use Tests\TestCase; | ||
|
||
class AntlersDirectiveTest extends TestCase | ||
{ | ||
public function test_blade_antlers_directive_is_compiled() | ||
{ | ||
$template = <<<'EOT' | ||
<?php $myCustomVariable = ['foo' => 'bar']; ?> | ||
@php($data = range(1, 10)) | ||
@if ($test) | ||
@antlers | ||
{{ myCustomVariable }} | ||
{{ foo }} | ||
{{ /myCustomVariable }} | ||
{{ data }} | ||
{{ value }} | ||
{{ /data }} | ||
@endantlers | ||
@endif | ||
@php($data = range('a', 'e')) | ||
@antlers {{ data }}{{ value }}{{ /data }} @endantlers | ||
EOT; | ||
|
||
$expected = <<<'EXPECTED' | ||
bar | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
abcde | ||
EXPECTED; | ||
|
||
$this->assertSame($expected, trim(Blade::render($template, ['test' => true]))); | ||
} | ||
} |