A collection of Handlebars Helpers for use with Solidus. Not that you can't use it elsewhere, mind you.
Converts a string to all lowercase characters.
liquid!
Converts a string to all uppercase characters.
BROTHER!
Replaces part of a string with a string.
Solid Snake
URI encodes a string. Useful for links that use URLs as query parameters.
http%3A%2F%2Fsparkart.com
Returns the length of the array, string, or object.
{
array: [1,2,3],
object: {
one: 1,
two: 2,
three: 3
},
string: 'Metal Gear'
}
Array length: 3
Object length: 3
String length: 10
Render something if the specified value is in the array, object or string; else render some other thing.
{
array: [ 'Solid', 'Liquid', 'Solidus' ],
array2: [ 'Chell', 'GLAdOS', 'Wheatley' ],
object: {
one: 'Solid',
two: 'Liquid',
three: 'Solidus'
},
object2: {
one: 'Chell',
two: 'GLaDOS',
three: 'Wheatley'
},
string: 'Solidus Snake',
string2: 'Solid Snake'
}
Array contains "Solidus": Yup.
Array2 contains "Solidus": Nope.
Object contains "Solidus": Yup.
Object2 contains "Solidus": Nope.
String contains "Solidus": Yup.
String2 contains "Solidus": Nope.
Render something with the first item in an array. Or render something with the first count
items in an array, I won't judge.
This can also display the "first" count
items in an object. Use with caution, as browsers don't always agree on what the "first" item in an object is.
{
array: ['Solid', 'Liquid', 'Solidus'],
object: {
one: 'Solid',
two: 'Liquid',
three: 'Solidus'
}
}
First item in array: Solid
First two items in array: Solid Liquid
First item in object: Solid
First two items in object: Solid Liquid
Render something with the last item in an array. Or render something with the last count
items in an array, it's cool.
{
array: ['Solid', 'Liquid', 'Solidus']
}
Last item in array: Solid
Last two items in array: Liquid Solidus
Loop through the data in an array between two indexes. Here's a quick run down:
- If only one index is provided, it is treated as the
start
and it will continue to the end of the array. - These are zero-based indexes, so the first item is
0
, the second is1
, and so forth. - This is an inclusive helper, so it not only gets things between two indexes, it includes them as well.
- Indexes can be negative! Negative indexes count backwards from the end of the array.
{
array: ['Psycho Mantis','Sniper Wolf', 'Vulcan Raven', 'Decoy Octopus', 'Revolver Ocelot', 'Liquid Snake']
}
Between the 3rd item and the last: Vulcan Raven Decoy Octopus Revolver Ocelot Liquid Snake
Between the 2nd item and the 4th: Sniper Wolf Vulcan Raven Decoy Octopus
Between the -4th item and the -1st: Vulcan Raven Decoy Octopus Revolver Ocelot
Loop through amount
data items in an array starting from index
. Some notes:
- If only the
index
is provided, it will loop through every item from there to the end. - The index is zero-based, so the first item is
0
, the second is1
, and so forth. - This is an inclusive helper, so it gets everything beyond the specified index and the item at
index
itself. - The
index
can be negative! Negative indexes count backwards from the end of the array.
{
array: ['Psycho Mantis','Sniper Wolf', 'Vulcan Raven', 'Decoy Octopus', 'Revolver Ocelot', 'Liquid Snake']
}
Range from 3rd item to the end: Vulcan Raven Decoy Octopus Revolver Ocelot Liquid Snake
Range of 2 items starting from the 2nd item: Sniper Wolf Vulcan Raven
Range of 2 items starting from the -3rd item: Decoy Octopus Revolver Ocelot
Loop through an array of objects and render when the value
at key
matches the supplied values. You can also provide a limit
to stop looping after you've had enough.
{
array: [{
title: 'Metal Gear Solid',
system: 'Playstation',
release_year: 1998
}, {
title: 'Metal Gear Solid 2',
system: 'Playstation 2',
release_year: 2001
}, {
title: 'Metal Gear Solid 3',
system: 'Playstation 2',
release_year: 2004
}]
}
Where release_year is 1998: Metal Gear Solid
Where system is "Playstation 2": Metal Gear Solid 2 Metal Gear Solid 3
Where system is "Playstation 2", limit 1: Metal Gear Solid 2
Loop through an array, but in random order.
{
array: ['Psycho Mantis','Sniper Wolf', 'Vulcan Raven', 'Decoy Octopus', 'Revolver Ocelot', 'Liquid Snake']
}
Shuffled array: Vulcan Raven Psycho Mantis Revolver Ocelot Liquid Snake Sniper Wolf Decoy Octopus
Loop through an array in reverse order.
{
array: ['Psycho Mantis','Sniper Wolf', 'Vulcan Raven', 'Decoy Octopus', 'Revolver Ocelot', 'Liquid Snake']
}
Reversed array: Liquid Snake Revolver Ocelot Decoy Octopus Vulcan Raven Sniper Wolf Psycho Mantis
Join a collection together with the specified separator. If run on an object, it just joins the values together (ignoring keys).
{
array: ['Psycho Mantis', 'Sniper Wolf', 'Vulcan Raven', 'Decoy Octopus', 'Revolver Ocelot', 'Liquid Snake'],
object: { '1': 'Psycho Mantis', '2': 'Sniper Wolf', '3': 'Vulcan Raven', '4': 'Decoy Octopus', '5': 'Revolver Ocelot', '6': 'Liquid Snake' }
}
Joined array: Psycho Mantis, Sniper Wolf, Vulcan Raven, Decoy Octopus, Revolver Ocelot, Liquid Snake
Joined object: Psycho Mantis, Sniper Wolf, Vulcan Raven, Decoy Octopus, Revolver Ocelot, Liquid Snake
Render a human friendly string denoting how long ago the supplied date was.
{
now: new Date,
earlier: new Date + 1000 * 60 * 60 * 6,
way_earlier: new Date + 1000 * 60 * 60 * 24 * 30
}
Ago for right now: Just now
Ago for earlier: 6 hours ago
Ago for way earlier: 30 days ago
Render a date or date-like value however you wanna. Uses samsonjs/strftime under the hood, so check out its documentation for the list of format options.
I should also add that this is using strftimeTZ
, which allows us to specify an offset from GMT in minutes. This is especially useful when using Handlebars Helper server side, as the server will likely use an arbitrary time zone by default.
{
dates: [
'2013-09-30T15:00:00.340Z',
'2013/09/30 15:00:00 +0000',
'Mon Sep 30 2013 15:00:00 GMT-0700 (PDT)',
1380578400000
]
}
Monday, September 30th 2013
Sep. 30th 2013
Monday at 10:00PM
Monday at 8:00PM
30-Sep-2013
Render one thing if both variables are equal. Render another thing if they're not. If you specify "exact", the comparison will be done with ===
, which checks for equality AND type.
{
one: 1,
one_string: '1',
two: 2
}
1 and 1 are equal: Yup.
1 and '1' are equal: Yup.
1 and 2 are equal: Nope.
1 and 1 are exactly equal: Yup.
1 and '1' are exactly equal: Nope.
1 and 2 are not equal: Yup.
Render one thing if the first item is greater than the second. Render another thing if it isn't. If you specify "equal", the comparison will be greater than and equal to.
{
one: 1,
two: 2
}
2 is greater than 1: Yup.
1 is greater than 2: Nope.
2 is greater than 2: Nope.
2 is greater than or equal to 1: Yup.
2 is greater than or equal to 2: Yup.
1 is not greater than 2: Yup.
2 is not greater than 2: Yup.
Render one thing if the first item is less than the second. Render another thing if it isn't. If you specify "equal", the comparison will be less than and equal to.
{
one: 1,
two: 2
}
2 is less than 1: Nope.
1 is less than 2: Yup.
2 is less than 2: Nope.
1 is less than or equal to 2: Yup.
2 is less than or equal to 2: Yup.
2 is not less than 1: Yup.
2 is not less than 2: Yup.
Render this block x times. If "zero" is specified, the count starts at 0
instead of 1
.
Do this 1 time: 1
Do this 5 times: 1 2 3 4 5
Do this 1 time, starting from 0: 0
Do this 5 times, starting from 0: 0 1 2 3 4
Add numbers together. Anything that's not a number will be converted to a number, then added.
1 + 2 = 3
-1 + 2 = 1
"1" + 2 = 3
1.5 + 1 = 2.5
1 + 2 + 3 = 6
Subtract numbers from one another. Anything that's not a number will be converted to a number, then subtracted.
1 - 2 = -1
-1 - 2 = -3
"1" - 2 = -1
1.5 - 1 = 0.5
1 - 2 - 3 = -4
Multiply numbers. Anything that's not a number will be converted to a number, then multiplied.
2 * 2 = 4
-2 * 2 = -4
"2" * 2 = 4
1.5 * 2 = 3
2 * 2 * 2 = 8
Divide numbers. Anything that's not a number will be converted to a number, then divided.
4 ÷ 2 = 2
-4 ÷ 2 = -2
"4" ÷ 2 = 2
3 ÷ 1.5 = 2
8 ÷ 2 ÷ 2 = 2