Skip to content

Commit a24ce56

Browse files
4.13
1 parent fc9549e commit a24ce56

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

lib/BladeOne.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
* @copyright Copyright (c) 2016-2023 Jorge Patricio Castro Castillo MIT License.
3535
* Don't delete this comment, its part of the license.
3636
* Part of this code is based in the work of Laravel PHP Components.
37-
* @version 4.12
37+
* @version 4.13
3838
* @link https://github.com/EFTEC/BladeOne
3939
*/
4040
class BladeOne
4141
{
4242
//<editor-fold desc="fields">
43-
public const VERSION = '4.12';
43+
public const VERSION = '4.13';
4444
/** @var int BladeOne reads if the compiled file has changed. If it has changed,then the file is replaced. */
4545
public const MODE_AUTO = 0;
4646
/** @var int Then compiled file is always replaced. It's slow and it's useful for development. */
@@ -4160,6 +4160,30 @@ protected function compileChecked($expression): string
41604160
{
41614161
return $this->phpTag . "if$expression echo 'checked'; ?>";
41624162
}
4163+
protected function compileStyle($expression) {
4164+
return $this->phpTag . "echo 'class=\"'.\$this->runtimeStyle($expression).'\"' ?>";
4165+
}
4166+
protected function compileClass($expression) {
4167+
return $this->phpTag . "echo 'class=\"'.\$this->runtimeStyle($expression).'\"'; ?>";
4168+
}
4169+
protected function runtimeStyle($expression=null,$separator=' '): string
4170+
{
4171+
if($expression===null) {
4172+
return '';
4173+
}
4174+
if(!is_array($expression)) {
4175+
$expression=[$expression];
4176+
}
4177+
$result='';
4178+
foreach($expression as $k=>$v) {
4179+
if(is_numeric($k)) {
4180+
$result.=$v.$separator;
4181+
} elseif($v) {
4182+
$result.=$k.$separator;
4183+
}
4184+
}
4185+
return trim($result);
4186+
}
41634187

41644188
/**
41654189
* Compile the selected statements into valid PHP.
@@ -4195,7 +4219,7 @@ protected function compileReadonly($expression): string
41954219
{
41964220
return $this->phpTag . "if$expression echo 'readonly'; ?>";
41974221
}
4198-
4222+
41994223
/**
42004224
* Compile the required statements into valid PHP.
42014225
*

tests/OtherTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,13 @@ public function test2()
127127
$compare=['a1'=>'1','a2'=>'function(1 2 3)'];
128128
$this->assertEquals($compare, $arr);
129129
}
130+
public function testClassStyle()
131+
{
132+
$this->assertEquals('<span class="p-4 text-gray-500 bg-red"></span>
133+
<span class="p-4 text-gray-500 bg-red"></span>
134+
<span class="background-color: red font-weight: bold"></span>
135+
<span style="background-color: red; font-weight: bold;"></span>
136+
', $this->blade->run("v11.test", ["list" => []]));
137+
}
138+
130139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@php
2+
$isActive = false;
3+
$hasError = true;
4+
@endphp
5+
<span @class([
6+
'p-4',
7+
'font-bold' => $isActive,
8+
'text-gray-500' => ! $isActive,
9+
'bg-red' => $hasError,
10+
])></span>
11+
<span class="p-4 text-gray-500 bg-red"></span>
12+
@php
13+
$isActive = true;
14+
@endphp
15+
<span @style([
16+
'background-color: red',
17+
'font-weight: bold' => $isActive,
18+
])></span>
19+
<span style="background-color: red; font-weight: bold;"></span>

0 commit comments

Comments
 (0)