Skip to content

Commit d7a8977

Browse files
committed
Add support for default value bypassing
1 parent 2b43ef4 commit d7a8977

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ As you can see in the example above, the `class` is added twice. This is of cour
5555

5656
## Usage
5757

58-
### 1. Configure the Helper
58+
### Configure the Helper
5959

6060
In your `AppView.php`, specify the custom template class for any helper you want to use with default attributes:
6161

@@ -69,7 +69,7 @@ $this->loadHelper('Form', [
6969
]);
7070
```
7171

72-
### 2. Define Templates with defaults
72+
### Define Templates with defaults
7373

7474
Add default attributes to your templates. For example, to set a default class and a custom data attribute for all input fields:
7575

@@ -100,7 +100,7 @@ Renders to
100100
<input type="text" name="field" class="defaultClass" data-star="wars" />
101101
```
102102

103-
### 3. Merging default and runtime values
103+
### Merging default and runtime values
104104

105105
If you want to merge default values with those provided at runtime (e.g., for classes), use an array for the default value:
106106

@@ -127,7 +127,26 @@ Renders to
127127
<input type="text" name="field" class="firstClass secondClass thirdClass" />
128128
```
129129

130-
### 4. Advanced Example: Multiple Attributes
130+
#### Removing default values at runtime
131+
132+
There may be situations where you want to remove the default values. In that case, you can pass `false` as the attribute value, and the attribute will then be removed from the element.
133+
134+
Remove entire attribute:
135+
136+
```php
137+
<?= $this->Form->control('field', ['class' => 'false']); ?>
138+
// <input type="text" name="field" />
139+
```
140+
141+
You can also bypass default values and only add runtime values:
142+
143+
```php
144+
<?= $this->Form->control('field', ['class' => '!!there-can-be-only-one']); ?>
145+
// <input type="text" name="field" class="there-can-be-only-one" />
146+
```
147+
148+
149+
### Advanced Example: Multiple Attributes
131150

132151
You can set defaults for any attribute, including data attributes and ARIA properties:
133152

src/View/StringTemplate.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,18 @@ public function format(string $name, array $data): string
4747
foreach ($attrMatches as $match) {
4848
$attribute_key = $match[1];
4949
$attribute_value = $match[3];
50-
$defaults_value = $defaults[$attribute_key] ?? null;
5150

51+
if (str_starts_with($attribute_value, '!!')) {
52+
$attribute_value = substr($attribute_value, 2);
53+
unset($defaults[$attribute_key]);
54+
}
55+
56+
if ($attribute_value === 'false') {
57+
unset($defaults[$attribute_key]);
58+
continue;
59+
}
60+
61+
$defaults_value = $defaults[$attribute_key] ?? null;
5262
if (is_array($defaults_value)) {
5363
$attribute_value = (array)$attribute_value;
5464
}

tests/TestCase/View/StringTemplateTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ public function testDefault(): void
5252
$this->assertEquals('<input class="defaultClass" type="text" name="myName">', $formatted);
5353
}
5454

55+
public function testFalseValue(): void
56+
{
57+
$formatted = $this->templater->format('input', [
58+
'attrs' => $this->templater->formatAttributes([
59+
'class' => 'false',
60+
]),
61+
'type' => 'text',
62+
'name' => 'myName',
63+
]);
64+
65+
$this->assertEquals('<input type="text" name="myName">', $formatted);
66+
}
67+
68+
public function testByPassValue(): void
69+
{
70+
$formatted = $this->templater->format('input_extra_attribs', [
71+
'attrs' => $this->templater->formatAttributes([
72+
'class' => '!!my other class',
73+
]),
74+
'type' => 'text',
75+
'name' => 'myName',
76+
]);
77+
78+
$this->assertStringContainsString('class="my other class"', $formatted);
79+
}
80+
5581
public function testExtraAttribs(): void
5682
{
5783
$formatted = $this->templater->format('input_extra_attribs', [

0 commit comments

Comments
 (0)