Skip to content

Commit bd93fb4

Browse files
committedNov 13, 2022
Only add scalar meta data to image dataset
Prevents attributes containing arrays or maps from breaking rendering with an "array to string" conversion error, see xp-forge/handlebars#24 This will permit us to refactor meta data more freely in the future, e.g. as see in the "Extract colors" pull request, #36
1 parent 144a6bb commit bd93fb4

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
 

‎src/main/handlebars/partials/images.handlebars

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</video>
88
{{else}}
99
<a href="/image/{{in.slug}}/full-{{name}}.webp" data-lightbox>
10-
<img alt="{{title}}, {{date meta.dateTime format='d.m.Y H:i'}}" {{#unless first}}loading="lazy"{{/unless}} {{#each meta}}data-{{@key}}="{{.}}"{{/each}} src="/image/{{in.slug}}/thumb-{{name}}.webp" width="1024">
10+
<img alt="{{title}}, {{date meta.dateTime format='d.m.Y H:i'}}" {{#unless first}}loading="lazy"{{/unless}} {{&dataset meta}} src="/image/{{in.slug}}/thumb-{{name}}.webp" width="1024">
1111
</a>
1212
{{/if}}
1313
</div>

‎src/main/php/de/thekid/dialog/Helpers.php

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
* - range <from> <until>
99
* - route <entry>
10+
* - dataset <meta-inf>
1011
*
1112
* @test de.thekid.dialog.unittest.HelpersTest
1213
*/
@@ -29,5 +30,12 @@ public function helpers() {
2930
return 'content/'.$entry['slug'];
3031
}
3132
};
33+
yield 'dataset' => function($node, $context, $options) {
34+
$r= '';
35+
foreach ($options[0] as $key => $value) {
36+
is_scalar($value) && $r.= ' data-'.htmlspecialchars($key).'="'.htmlspecialchars($value).'"';
37+
}
38+
return $r;
39+
};
3240
}
3341
}

‎src/test/php/de/thekid/dialog/unittest/HelpersTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,20 @@ public function range($options, $expected) {
2424
public function route($entry, $expected) {
2525
Assert::equals($expected, $this->helpers['route'](null, null, [$entry]));
2626
}
27+
28+
#[Test]
29+
public function dataset() {
30+
Assert::equals(
31+
' data-string="value" data-yes="1" data-no="" data-number="2022"',
32+
$this->helpers['dataset'](null, null, [[
33+
'string' => 'value',
34+
'yes' => true,
35+
'no' => false,
36+
'number' => 2022,
37+
'array' => ['ignored'],
38+
'map' => ['also' => 'ignored'],
39+
'nulls' => null,
40+
]])
41+
);
42+
}
2743
}

0 commit comments

Comments
 (0)
Please sign in to comment.