Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Which handlebars helpers are available to statsheets #4185

Open
cwisniew opened this issue Jun 29, 2023 · 3 comments
Open

[Question]: Which handlebars helpers are available to statsheets #4185

cwisniew opened this issue Jun 29, 2023 · 3 comments
Labels

Comments

@cwisniew
Copy link
Member

cwisniew commented Jun 29, 2023

Your Question

In 1.14.0-rc.2

Standard

Conditional Helpers (from ConditionalHelpers.java)

eq - Test if two elements are equal

{{#eq this.shortName "STR"}}
   This is how strong you are
{{else}}
   This is not how strong you are
{{/eq} 

neq - Test if two elements are not equal

{{#neq this.shortName "STR"}}
   This is not how strong you are
{{else}}
   This is how strong you are
{{/neq} 

gt - Greater than Test

{{#gt val1 val2}}
   The first value is larger than the second
{{else}}
   The first value is not larger than the second
{{/gt} 

gte - Greater than or equal Test

{{#gte val1 val2}}
   The first value is larger than or equal to the second
{{else}}
   The first value is less than the second
{{/gt} 

lt - Less than Test

{{#lt val1 val2}}
   The first value is smaller than the second
{{else}}
   The first value is not smaller than the second
{{/gt} 

lte - Less than or equal Test

{{#lte val1 val2}}
   The first value is smaller than or equal to the second
{{else}}
   The first value is larger than the second
{{/gt} 

and - And operator

{{#and val1 val2}}
   All values are "true"
{{else}}
   Not all values are "true"
{{/and}}

You can have multiple values

{{#and val1 val2 val3 val4}}
   All values are "true"
{{else}}
   Not all values are true"
{{/and}}

or - And operator

{{#or val1 val2}}
   At least one of values is "true"
{{else}}
   None of the values are "true"
{{/and}}

You can have multiple values

{{#and val1 val2 val3 val4}}
   At least one of values is "true"
{{else}}
   None of the values are true"
{{/and}}

not - Not operator

{{#not val1}}
   Value is not "true"
{{else}}
   Value is "true"
{{/and}}

Rendering True or False

All of the above conditional operators can be used to display simple true/false
e.g.

{{eq val1 val2}}

Rendering Yes or No

All of the above conditional operators can be used to display Yes/No (or other simple string)

{{eq val1 val2 yes='Yes' no='No'}}

Logging

https://handlebarsjs.com/guide/builtin-helpers.html#lookup
Simple

{{log "the message to log}}

With logging level

{{log "error log message" level="error"}}

Multiline

{{#log}} 
   logging message {{message}}
{{/log}}

String functions (from [StringHelpers] (https://github.com/jknack/handlebars.java/blob/master/handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java))

Capitalize First Letter

{{capitalizeFirst value}}

Center text

{{center value size=20 pad=" "}}

Remove occurrences of argument

{{cut value " "}}

Default value if empty

{{defaultIfEmpty value "(empty)"}}

join values as a string

{{join val1 val2 val3 ", "}}
{{join val1 val2 val3 ", " prefix="values = " }}
{{join val1 val2 val3 ", " prefix="values = " suffix=";"}}

Left Justify

{{ljust value 20 pad=" "}}

Right Justify

{{rjust value 20 pad=" "}}

Substring

{{sub value 0 4}}

Convert to lowercase

{{lower value}}

Convert to uppercase

{{upper value}}

Convert to lowercase, remove non word characters and translate spaces to -

{{slugify value}}

Format string

{{stringFormat format val1 val2 .. valN}}

Format specifiers

%% – Inserts a “%” sign
%x/%X – Integer hexadecimal
%t/%T – Time and Date
%s/%S – String
%n – Inserts a newline character
%o – Octal integer
%f – Decimal floating-point
%e/%E – Scientific notation
%g – Causes Formatter to use either %f or %e, whichever is shorter
%h/%H – Hash code of the argument
%d – Decimal integer
%c – Character
%b/%B – Boolean
%a/%A – Floating-point hexadecimal

see Java String Format

Capitalizes all the whitespace separated words in a String.

{{capitalize value}}

Truncates a string if it is longer than the specified number of characters. (ends string with ...)

{{abbreviate value 10}} 

Wordwrap

{{wordWrap value 80}}

String Replace

{{replace "This is not a test" "not" ""}}

Map values for true, false and null (optional), to the strings "yes", "no", "maybe".

{{yesno yes="sure" no="nope" maybe="dunno"}}

Date Format

{{dateFormat date ["format"] [format="format"][locale="locale"][tz=timeZone|timeZoneId] time="format"]}}

Format parameters is one of:

  • "full": full date format. For example: Tuesday, June 19, 2012
  • "long": long date format. For example: June 19, 2012
  • "medium": medium date format. For example: Jun 19, 2012
  • "short": short date format. For example: 6/19/12
  • "pattern": a {see [ java.time.format.DateTimeFormatter](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/format/DateTimeFormatter.html)} pattern.
Otherwise, the default formatter will be used.

The format option can be specified as a parameter or hash (a.k.a named parameter).
The "locale" parameter can be use to select a locale, e.g. "de" or "en_GB".
It defaults to the system locale.

The "tz" parameter is the time zone to use, e.g. "Europe/Berlin" or "GMT-8:00".
It defaults to the system time zone.

The "time" parameter specifies the format of the time part, it can be "full", "long", "medium" or "short".
If you do not specify it only the date part will appear in the output string.

Format Numbers

{{numberFormat number ["format"] [locale=default]}}

Format parameters is one of:

  • "integer": the integer number format
  • "percent": the percent number format
  • "currency": the decimal number format
  • "pattern": a decimal pattern.

Otherwise, the default formatter will be used.

More options:

  • groupingUsed: Set whether or not grouping will be used in this format.
  • maximumFractionDigits: Sets the maximum number of digits allowed in the fraction portion of a number.
  • maximumIntegerDigits: Sets the maximum number of digits allowed in the integer portion of a number
  • minimumFractionDigits: Sets the minimum number of digits allowed in the fraction portion of a number
  • minimumIntegerDigits: Sets the minimum number of digits allowed in the integer portion of a number.
  • parseIntegerOnly: Sets whether or not numbers should be parsed as integers only.
  • roundingMode: Sets the {see java.math.RoundingMode} used in this NumberFormat.

see

Time Now

{{now}}
{{now ["format"] [tz=timeZone|timeZoneId]}}

Format parameters is one of:

  • "full": full date format. For example: Tuesday, June 19, 2012
  • "long": long date format. For example: June 19, 2012
  • "medium": medium date format. For example: Jun 19, 2012
  • "short": short date format. For example: 6/19/12
  • "pattern": a date pattern.

Otherwise, the default formatter will be used.

Numbers (from [NumberHelper.java] (https://github.com/jknack/handlebars.java/blob/master/handlebars-helpers/src/main/java/com/github/jknack/handlebars/helper/NumberHelper.java))

Check if Even

{{isEven value "this is even"}}

Check if Odd

{{isOdd value "this is odd"}}

Alternate values between even and odd

{{stripes value "row-even" "row-odd"}}

Variables (from [AssignHelper] (https://github.com/jknack/handlebars.java/blob/master/handlebars-helpers/src/main/java/com/github/jknack/handlebars/helper/AssignHelper.java))

{{#assign "var1"}}
  Value to be Assigned
{{/var}}

The value of var1 is {{var1}}
@dwian
Copy link

dwian commented Jun 30, 2023

That's great! I think your a bit of an overachiever ;-). I'm out for the weekend but I will start some testing next week. I'll report.
Thanks for the many new possibilities!

@bubblobill
Copy link
Collaborator

It's not enough, I want more :)
If anyone finds a string length helper in the wild I would appreciate it being pointed out,

@fishface60
Copy link
Contributor

fishface60 commented Aug 14, 2024

The substring operator appears to be {{substring value start end}} not {{sub value start end}}. (See https://github.com/jknack/handlebars.java/blob/42c1912f8555697664d3da1a539b2150709fb984/handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java#L240)

This is of limited use for a prefix match without a string length though because a substring longer than the string causes an exception and for the template to not load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants