Skip to content

A Foundry VTT Module which registers a some additional handlebars helpers.

License

Notifications You must be signed in to change notification settings

kgar/foundry-vtt-more-handlebars-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foundry VTT: More Handlebars Helpers

GitHub issues Latest Version Latest Release Download Count Forge Installs Foundry Core Compatible Version GitHub all releases

Do you need more handlebars helpers in Foundry VTT?

This is the Foundry Module for you!

Helpers that are included in this module:

abs

Return the magnitude of a.

Params

  • num {number}

Examples

<!-- value: -1 -->
{{ more-handlebars-helpers-abs value }}
<!-- results in: "1" -->

add

Return the sum of a plus b.

Params

  • a {number}
  • b {number}

Examples

<!-- value: 2 -->
{{ more-handlebars-helpers-add value 2 }}
<!-- results in: "4" -->

avg

Returns the average of all numbers in the given array.

Params

  • ...args {numbers}: one or more number arguments

Examples

{{ more-handlebars-helpers-avg 1 2 3 4 }}
<!-- results in: "2.5" -->

ceil

Get the Math.ceil() of the given value.

Params

  • a {number}

Examples

<!-- value: 2.1 -->
{{ more-handlebars-helpers-ceil value }}
<!-- results in: "3" -->

divide

Divide a by b

Params

  • a {number}: numerator
  • b {number}: denominator

Examples

<!-- value: 2.1 -->
{{ more-handlebars-helpers-divide 4 2 }}
<!-- results in: "2" -->

floor

Get the Math.floor() of the given value.

Params

  • value {number}

Examples

<!-- value: 2.9 -->
{{ more-handlebars-helpers-floor value }}
<!-- results in: "2" -->

gt

Test whether a value is greater than another value.

Params

  • a {any}: the value to test
  • b {any}: the value to test against

Examples

<!-- value: 1 -->
{{#if (more-handlebars-helpers-gt value 0)}} 
    GREAT 
{{else}}
    NOPE
{{/if}}
<!-- results in: "GREAT" -->

gte

Test whether a value is greater than or equal to another value.

Params

  • a {any}: the value to test
  • b {any}: the value to test against

Examples

<!-- value: 0 -->
{{#if (more-handlebars-helpers-gte value 0)}} 
    STILL GREAT
{{else}}
    NOPE
{{/if}}
<!-- results in: "STILL GREAT" -->

lt

Test whether a value is less than another value.

Params

  • a {any}: the value to test
  • b {any}: the value to test against

Examples

<!-- value: 1 -->
{{#if (more-handlebars-helpers-lt value 0)}} 
    IS IT REALLY
{{else}}
    NOPE
{{/if}}
<!-- results in: "NOPE" -->

lte

Test whether a value is less than or equal to another value.

Params

  • a {any}: the value to test
  • b {any}: the value to test against

Examples

<!-- value: 0 -->
{{#if (more-handlebars-helpers-lte value 0)}} 
    GOOD TO GO
{{else}}
    NOPE
{{/if}}
<!-- results in: "GOOD TO GO" -->

modulo

Get the remainder of a division operation.

Params

  • a {number}: numerator
  • b {number}: denominator

Examples

{{ more-handlebars-helpers-modulo 9 2 }}
<!-- results in: "1" -->

multiply

Return the product of a times b.

Params

  • a {number}: factor
  • b {number}: multiplier

Examples

{{ more-handlebars-helpers-multiply 3 3 }}
<!-- results in: "9" -->

random

Generate a random integer between two values, including the min and max values.

Params

  • min {number}
  • max {number}

Examples

{{ more-handlebars-helpers-random 1 10 }}
<!-- results in: random integer between 1 and 10 -->

regexp

Converts text into a RegExp instance to be fed into other helpers.

Params

  • text {string}
  • flags (optional, default "g") {string}: flags to use when creating the instance of RegExp.

Examples

<!-- value: "Hello,\n<hr />there,\n<hr />world!" -->
{{ more-handlebars-helpers-replace value (more-handlebars-helpers-regexp '\n<hr />') ' ' }}
<!-- results in: "Hello, there, world!" -->
<!-- value: "Hello,\n<hr />there,\n<HR />world!" -->
{{ more-handlebars-helpers-replace value (more-handlebars-helpers-regexp '\n<hr />' 'gi') ' ' }}
<!-- results in: "Hello, there, world!" -->

replace

Replace first instance of searchTextOrRegExp with replacementText in text.

Params

  • text {string}
  • searchTextOrRegExp {string}: the text to be replaced, or an instance of RegExp (see regexp)
  • replacementText {string}: the text to replace searchTextOrRegExp; it follows the rules of String.prototype.replace(), so RegExp replacements can replace multiple results while a regular string of text will be replaced once

Example

<!-- value: "One and Two and Three" -->
{{ more-handlebars-helpers-replace value 'and ', '' }}
<!-- results in: "One Two and Three" -->

replaceAll

Replace all instances of searchTextOrRegExp with replacementText in text.

Params

  • text {string}
  • searchTextOrRegExp {string}: the text to be replaced, or an instance of RegExp (see regexp)
  • replacementText {string}: the text to replace searchTextOrRegExp throughout text

Example

<!-- value: "One and Two and Three" -->
{{ more-handlebars-helpers-replaceAll value 'and ', '' }}
<!-- results in: "One Two Three" -->

round

Round the given number.

Params

  • number {number}

Examples

{{ more-handlebars-helpers-round 2.55 }}
<!-- results in: "3" -->

sanitize

Strip HTML tags from a string, so that only the text is preserved.

Params

  • htmlString {string}: The string of HTML to sanitize.

Example

<!-- value: "<span>hello</span>" -->
{{ more-handlebars-helpers-sanitize value }}
<!-- results in : "hello" -->

split

Split string by the given character.

Params

  • text {string}
  • separator {string}: The pattern describing where each split should occur

Example

<!-- value: "a,b,c" -->
<ul>{{#each (more-handlebars-helpers-split value ',')}}<li>{{this}}</li>{{/each}}</ul>
<!-- results in (newlines and indents added for documentation): 
    <ul>
        <li>a</li>
        <li>b</li>
        <li>c</li>
    </ul> 
-->

subtract

Return the difference of a minus b.

Params

  • a {number}: left term
  • b {number}: right right

Examples

{{more-handlebars-helpers-subtract 10 9 }}
<!-- results in : "1" -->

sum

Returns the sum of all numbers in the given array.

Params

  • ...args {numbers}: two or more number arguments

Examples

{{ more-handlebars-helpers-sum 1 2 3 4 }}
<!-- results in: "10" -->

table

Look up a value in a static look-up table and replace it with another value.

Params

  • lookup {string}: the text to use as a lookup key
  • key1 {string}: the key for result1; can contain a regular expression
  • result1 {string}: the value to return when lookup matches key1; can contain capturing groups (e.g. $1) to copy information from the matching regex key
  • key2, result2, key3, result3, etc.

Example

We want to look up what "blue" means, according to a static series of keys and results.

<!-- value: "blue" -->
{{ more-handlebars-helpers-table value "red" "angry" "blue" "sad" "yellow" "envious" "green" "happy" }}
<!-- results in: "sad" -->

Example with Regex

We want to look up what "blue" means, according to a static series of keys and results. The blue key uses regex to capture the key, and its value references the key via $1.

<!-- value: "What does blue mean?" -->
{{ more-handlebars-helpers-table value "red" "angry" "(blue)" "$1 means sad" "yellow" "envious" "green" "happy" }}
<!-- results in: "blue means sad" -->

Need More Helpers?

  • Open an issue.
  • Optional: submit a pull request to resolve the issue if you know how to do it and want to contribute.

Supporting this Module

I plan to keep building up this collection of helpers over time. If you like what I'm doing, feel free to send some support my way.

ko-fi "Buy Me A Coffee"

About

A Foundry VTT Module which registers a some additional handlebars helpers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published