-
Notifications
You must be signed in to change notification settings - Fork 0
array counting
Alex Oliver edited this page May 21, 2026
·
1 revision
Brace can count the number of items in an array using the COUNT() function inside {{ }} expressions.
<?php
use Brace\Parser;
$brace = new Parser();
echo $brace->parseInputString(
'<p>Total items: {{ COUNT(products) }}</p>',
[
'products' => [
['name' => 'Widget'],
['name' => 'Gadget'],
['name' => 'Doohickey'],
],
],
false
)->return();
// Output: <p>Total items: 3</p>COUNT() can be used anywhere a value is expected in an {{ if }} condition.
{{ if COUNT(cart) === 3 }}
<p>There are exactly three items.</p>
{{ end }}
All standard condition operators work with COUNT().
{{ if COUNT(cart) > 0 }}
<p>You have {{ COUNT(cart) }} item(s) in your cart.</p>
{{ else }}
<p>Your cart is empty.</p>
{{ end }}
{{ if COUNT(results) >= 10 }}
<p>Showing first 10 of {{ COUNT(results) }} results.</p>
{{ end }}
COUNT() works with nested paths using the -> operator.
<?php
use Brace\Parser;
$brace = new Parser();
echo $brace->parseInputString(
'{{ COUNT(order->items) }} item(s) ordered.',
[
'order' => [
'items' => ['Book', 'Pen', 'Ruler'],
],
],
false
)->return();
// Output: 3 item(s) ordered.