feat: add Liquid filter 'find_by'

This commit is contained in:
Benjamin Nussbaum 2025-08-25 14:33:26 +02:00
parent 6cd00943a1
commit f4f8ab5181
4 changed files with 158 additions and 0 deletions

View file

@ -172,4 +172,60 @@ LIQUID
$this->assertStringContainsString('This is a test', $result);
$this->assertStringContainsString('class="simple"', $result);
}
public function test_plugin_with_find_by_filter(): void
{
$plugin = Plugin::factory()->create([
'markup_language' => 'liquid',
'render_markup' => <<<'LIQUID'
{% template user_info %}
<div class="user">
<h2>{{ user.name }}</h2>
<p>Age: {{ user.age }}</p>
</div>
{% endtemplate %}
{% assign found_user = collection | find_by: 'name', 'Ryan' %}
{% render "user_info", user: found_user %}
LIQUID
,
'data_payload' => [
'collection' => [
['name' => 'Ryan', 'age' => 35],
['name' => 'Sara', 'age' => 29],
['name' => 'Jimbob', 'age' => 29],
],
],
]);
$result = $plugin->render('full');
// Should render the user info for Ryan
$this->assertStringContainsString('Ryan', $result);
$this->assertStringContainsString('Age: 35', $result);
$this->assertStringContainsString('class="user"', $result);
}
public function test_plugin_with_find_by_filter_and_fallback(): void
{
$plugin = Plugin::factory()->create([
'markup_language' => 'liquid',
'render_markup' => <<<'LIQUID'
{{ collection | find_by: 'name', 'ronak', 'Not Found' }}
LIQUID
,
'data_payload' => [
'collection' => [
['name' => 'Ryan', 'age' => 35],
['name' => 'Sara', 'age' => 29],
['name' => 'Jimbob', 'age' => 29],
],
],
]);
$result = $plugin->render('full');
// Should return the fallback value
$this->assertStringContainsString('Not Found', $result);
}
}