Skip to content

Commit 8c20d25

Browse files
committed
feat: add 'select' handlebars helper
1 parent 0d821e2 commit 8c20d25

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

include/mrdocs/Support/Handlebars.hpp

+23
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,17 @@ MRDOCS_DECL
10441044
void
10451045
registerAntoraHelpers(Handlebars& hbs);
10461046

1047+
/** Register logical helpers into a Handlebars instance
1048+
1049+
This function registers a number of common helpers that perform
1050+
logical operations.
1051+
1052+
@param hbs The Handlebars instance to register the helpers into
1053+
*/
1054+
MRDOCS_DECL
1055+
void
1056+
registerLogicalHelpers(Handlebars& hbs);
1057+
10471058
/** Register string helpers into a Handlebars instance
10481059
10491060
This function registers a number of common helpers that operate on
@@ -1130,6 +1141,18 @@ MRDOCS_DECL
11301141
bool
11311142
not_fn(dom::Array const& arg);
11321143

1144+
/** "select" helper function
1145+
*
1146+
* The "select" helper returns the second argument if the first argument is
1147+
* truthy, and the third argument otherwise.
1148+
*/
1149+
MRDOCS_DECL
1150+
dom::Value
1151+
select_fn(
1152+
dom::Value condition,
1153+
dom::Value result_true,
1154+
dom::Value result_false);
1155+
11331156
/** "increment" helper function
11341157
*
11351158
* The "increment" helper adds 1 to the value if it's an integer and converts

src/lib/Support/Handlebars.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,17 @@ registerAntoraHelpers(Handlebars& hbs)
36743674
hbs.registerHelper("year", dom::makeInvocable(year_fn));
36753675
}
36763676

3677+
void
3678+
registerLogicalHelpers(Handlebars& hbs)
3679+
{
3680+
hbs.registerHelper("and", dom::makeVariadicInvocable(and_fn));
3681+
hbs.registerHelper("eq", dom::makeVariadicInvocable(eq_fn));
3682+
hbs.registerHelper("ne", dom::makeVariadicInvocable(ne_fn));
3683+
hbs.registerHelper("not", dom::makeVariadicInvocable(not_fn));
3684+
hbs.registerHelper("or", dom::makeVariadicInvocable(or_fn));
3685+
hbs.registerHelper("select", dom::makeInvocable(select_fn));
3686+
}
3687+
36773688
bool
36783689
and_fn(dom::Array const& args)
36793690
{
@@ -3749,6 +3760,16 @@ increment_fn(dom::Value const& value)
37493760
return 1;
37503761
}
37513762

3763+
dom::Value
3764+
select_fn(
3765+
dom::Value condition,
3766+
dom::Value result_true,
3767+
dom::Value result_false)
3768+
{
3769+
return isEmpty(condition) ?
3770+
result_false : result_true;
3771+
}
3772+
37523773
dom::Value
37533774
detag_fn(dom::Value html)
37543775
{

0 commit comments

Comments
 (0)