-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Introduce
@js()
directive (#39522)
* [8.x] Introduce `@js()` directive * Update CompilesJs.php * Update to match new name Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
0e9ff79
commit 31da25c
Showing
4 changed files
with
54 additions
and
1 deletion.
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,22 @@ | ||
<?php | ||
|
||
namespace Illuminate\View\Compilers\Concerns; | ||
|
||
use Illuminate\Support\Js; | ||
|
||
trait CompilesJs | ||
{ | ||
/** | ||
* Compile the "@js" directive into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileJs(string $expression) | ||
{ | ||
return sprintf( | ||
"<?php echo \%s::from(%s)->toHtml() ?>", | ||
Js::class, $this->stripParentheses($expression) | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\View\Blade; | ||
|
||
class BladeJsTest extends AbstractBladeTestCase | ||
{ | ||
public function testStatementIsCompiledWithoutAnyOptions() | ||
{ | ||
$string = '<div x-data="@js($data)"></div>'; | ||
$expected = '<div x-data="<?php echo \Illuminate\Support\Js::from($data)->toHtml() ?>"></div>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testJsonFlagsCanBeSet() | ||
{ | ||
$string = '<div x-data="@js($data, JSON_FORCE_OBJECT)"></div>'; | ||
$expected = '<div x-data="<?php echo \Illuminate\Support\Js::from($data, JSON_FORCE_OBJECT)->toHtml() ?>"></div>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testEncodingDepthCanBeSet() | ||
{ | ||
$string = '<div x-data="@js($data, JSON_FORCE_OBJECT, 256)"></div>'; | ||
$expected = '<div x-data="<?php echo \Illuminate\Support\Js::from($data, JSON_FORCE_OBJECT, 256)->toHtml() ?>"></div>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
} |