This npm package provides a collection of useful Handlebars.js helpers to perform common operations in your templates. These helpers can be used to simplify and enhance your Handlebars templates.
You can install the package using npm:
npm install smart-handlebars
To use the provided helpers in your Handlebars templates, first, import the package:
Description: Adds the value to the variable in the template context.
Syntax:
{{addition key val}}
Example:
{{addition "total" 10}}
This will add 10 to the "total" variable in the template context.
Description: Subtracts the value from the variable in the template context.
Syntax:
{{subtraction key val}}
Example:
{{subtraction "balance" 5}}
This will subtract 5 from the "balance" variable in the template context.
Description: Rounds a number to the nearest integer.
Syntax:
{{round val}}
Example:
{{round 3.7}}
This will round 3.7 to 4
Description: Rounds a number up to the nearest integer.
Syntax:
{{ceil val}}
Example:
{{ceil 2.3}}
This will round 2.3 up to 3.
Description: Rounds a number down to the nearest integer.
Syntax:
{{floor val}}
Example:
{{floor 4.8}}
This will round 4.8 down to 4.
Description: Calculates the percentage of parVal relative to totVal. Syntax:
{{percentage parVal totVal}}
Example:
{{percentage 25 100}}
This will calculate the percentage of 25 relative to 100, resulting in 25%.
Description: Formats a number to a fixed number of decimal places. Syntax:
{{toFixed val num}}
Example:
{{toFixed 3.14159 2}}
This will format 3.14159 to two decimal places, resulting in "3.14".
Description: Converts a string to uppercase. Syntax:
{{toUpperCase str}}
Example:
{{toUpperCase "Hello World"}}
This will convert "Hello World" to "HELLO WORLD".
Description: Converts a string to lowercase. Syntax:
{{toLowerCase str}}
Example:
{{toLowerCase "Hello World"}}
This will convert "Hello World" to "hello world".
Description: Calculates the average of a sum of values. Syntax:
{{avg sum len}}
Example:
{{avg 30 5}}
This will calculate the average of 30 (sum) and 5 (len), resulting in 6.
Description: Returns the appropriate suffix for a given number (e.g., "st", "nd", "rd", or "th"). Syntax:
{{getNumberSuffix number}}
Example:
{{getNumberSuffix 21}}
This will return "st" as the suffix for the number 21.
Description: Increments the given value by 1.
Syntax:
{{inc value}}
Example:
{{inc 5}}
This will increment the value 5 by 1, resulting in 6.
Description: Allows you to create conditional statements in your Handlebars templates. It compares two values using various operators and executes the appropriate block of code based on the comparison result.
Syntax:
{{#ifCond v1 operator v2}}
<!-- Code to execute when the condition is true -->
{{else}}
<!-- Code to execute when the condition is false -->
{{/ifCond}}
Example: Equal (==) Operator
{{#ifCond 5 '==' 5}} The values are equal. {{else}} The values are not equal. {{/ifCond}}
This will output: "The values are equal." Not Equal (!=) Operator
{{#ifCond 5 '!=' 3}} The values are not equal. {{else}} The values are equal. {{/ifCond}}
This will output: "The values are not equal."
You can use various operators, including ==, ===, !=, !==, <, <=, >, >=, &&, and ||, to create different conditional statements in your templates.
-- With these descriptions and usage examples, users of your npm package will have a clear understanding of how to use each helper function in their HTML templates.
Description: Converts a number into words representation.
Syntax:
{{inWords num}}
Example:
{{inWords 12345}}
This will convert the number 12345 into "Twelve Thousand Three Hundred Forty-Five Only".
The stringReplace
helper is a custom Handlebars function that replaces all occurrences of a specific substring within a given string with another substring. This can be especially useful when dynamically manipulating text inside templates.