Skip to content

Commit

Permalink
4.13
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Mar 26, 2024
1 parent fc9549e commit a24ce56
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/BladeOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
* @copyright Copyright (c) 2016-2023 Jorge Patricio Castro Castillo MIT License.
* Don't delete this comment, its part of the license.
* Part of this code is based in the work of Laravel PHP Components.
* @version 4.12
* @version 4.13
* @link https://github.com/EFTEC/BladeOne
*/
class BladeOne
{
//<editor-fold desc="fields">
public const VERSION = '4.12';
public const VERSION = '4.13';
/** @var int BladeOne reads if the compiled file has changed. If it has changed,then the file is replaced. */
public const MODE_AUTO = 0;
/** @var int Then compiled file is always replaced. It's slow and it's useful for development. */
Expand Down Expand Up @@ -4160,6 +4160,30 @@ protected function compileChecked($expression): string
{
return $this->phpTag . "if$expression echo 'checked'; ?>";
}
protected function compileStyle($expression) {
return $this->phpTag . "echo 'class=\"'.\$this->runtimeStyle($expression).'\"' ?>";
}
protected function compileClass($expression) {
return $this->phpTag . "echo 'class=\"'.\$this->runtimeStyle($expression).'\"'; ?>";
}
protected function runtimeStyle($expression=null,$separator=' '): string
{
if($expression===null) {
return '';
}
if(!is_array($expression)) {
$expression=[$expression];
}
$result='';
foreach($expression as $k=>$v) {
if(is_numeric($k)) {
$result.=$v.$separator;
} elseif($v) {
$result.=$k.$separator;
}
}
return trim($result);
}

/**
* Compile the selected statements into valid PHP.
Expand Down Expand Up @@ -4195,7 +4219,7 @@ protected function compileReadonly($expression): string
{
return $this->phpTag . "if$expression echo 'readonly'; ?>";
}

/**
* Compile the required statements into valid PHP.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/OtherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@ public function test2()
$compare=['a1'=>'1','a2'=>'function(1 2 3)'];
$this->assertEquals($compare, $arr);
}
public function testClassStyle()
{
$this->assertEquals('<span class="p-4 text-gray-500 bg-red"></span>
<span class="p-4 text-gray-500 bg-red"></span>
<span class="background-color: red font-weight: bold"></span>
<span style="background-color: red; font-weight: bold;"></span>
', $this->blade->run("v11.test", ["list" => []]));
}

}
19 changes: 19 additions & 0 deletions tests/resources/templates/v11/test.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@php
$isActive = false;
$hasError = true;
@endphp
<span @class([
'p-4',
'font-bold' => $isActive,
'text-gray-500' => ! $isActive,
'bg-red' => $hasError,
])></span>
<span class="p-4 text-gray-500 bg-red"></span>
@php
$isActive = true;
@endphp
<span @style([
'background-color: red',
'font-weight: bold' => $isActive,
])></span>
<span style="background-color: red; font-weight: bold;"></span>

0 comments on commit a24ce56

Please sign in to comment.