davidB / handlebars_misc_helpers

A collection of helpers for handlebars (rust) to manage string, json, yaml, toml, path, file, http request.

Home Page:https://crates.io/crates/handlebars_misc_helpers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

helpers to do comparison

iilyak opened this issue · comments

I am very new to handlebars and might be missing something. I am trying to do a conditional inclusion:

{{#if var == 123}}
...
{{/if}}

I couldn't find a way to do comparisons using #if. It looks like the #if works only with boolean values.
This gist (for javascript based implementation of handlebars) suggest registering helper.

compare helper from the above gist might solve the issue:

{{#compare "Test" "Test"}}
  Default comparison of "==="
{{/compare}}

{{#compare Database.Tables.Count ">" 5}}
  There are more than 5 tables
{{/compare}}

There are some built-in comparison helpers in handlebars-rust:

https://github.com/sunng87/handlebars-rust/blob/master/src/helpers/helper_boolean.rs

And you can create these kind of helpers easily with macro: handlebars_helper!

You can do

{{#if (eq var 123)}}
...
{{/if}}

{{#if (gt var 123)}}
...
{{/if}}

{{#if (lt var 123)}}
...
{{/if}}

list of fonction (copy from the link in the comment of sunng87)

handlebars_helper!(eq: |x: Json, y: Json| x == y);
handlebars_helper!(ne: |x: Json, y: Json| x != y);
handlebars_helper!(gt: |x: i64, y: i64| x > y);
handlebars_helper!(gte: |x: i64, y: i64| x >= y);
handlebars_helper!(lt: |x: i64, y: i64| x < y);
handlebars_helper!(lte: |x: i64, y: i64| x <= y);
handlebars_helper!(and: |x: Json, y: Json| x.is_truthy(false) && y.is_truthy(false));
handlebars_helper!(or: |x: Json, y: Json| x.is_truthy(false) || y.is_truthy(false));
handlebars_helper!(not: |x: Json| !x.is_truthy(false));

feel free to close. Thank you for pointing me to the alternative ways of doing it.

Sorry I made a mistake (syntax error), I updated my comment:

(var eq 123) => (eq var 123) call the function eq with args and return a boolean