handlebars - TeamFlowerPower/kb GitHub Wiki
- Handlebars HTML-escapes variables are returned by a
{{expression}}
- If you don't want to escape a value, use raw rendering:
{{{
- (Special) variables need to be surrounded by
{{
and}}
but not within a control sequence (like{{#if @first}}
) - Path expressions:
{{person.firstname}}
-
{{!-- This is a comment --}}
but{{! This as well }}
- Use the former if your comment includes
}}
or handlebars tokens
- Use the former if your comment includes
- To make the comment appear in the output use html comments:
<!-- This comment will be in the output -->
Use []
for accessing lists/arrays:
Lstrip or rstrip whitespaces with ~
:
Escaping means basically that this specific part will not be compiled.
-
false
==undefined
==null
==""
==0
==[]
-
includeZero=true
: treat the conditional as not empty. Determines if0
is handled by the positive or negative path.
Inverse #if
:
For-each loop:
-
{{this}}
/{{.}}
references to the current loop object -
{{@index}}
will return the current loop index -
{{@key}}
returns the current key name for object iteration -
{{@first}}
/{{@last}}
: True if first/last iteration in loop -
Nested loops can access higher hirarchies via
{{@../index}}
-> see also @data
variables
Named parameters:
-
user
will hold the value -
userId
will hold the index
-> Can be used to make a variable available within the #with
block (i.e. city
is made available within the inner block) -> this avoids usage of the ../
syntax
Allows to look-up some value:
Input:
{
people: ["Nils", "Yehuda"],
cities: [
"Darmstadt",
"San Francisco",
],
}
-> {{lookup ../cities @index}}
goes one level up, into cities
and @index
selects the matching index provided by the loop over people
Looks-up a key (the value of persons/resident-in
) in cities
and prints its values (name
+ country
):
Input:
{
persons: [
{
name: "Nils",
"resident-in": "darmstadt",
},
{
name: "Yehuda",
"resident-in": "san-francisco",
},
],
cities: {
darmstadt: {
name: "Darmstadt",
country: "Germany",
},
"san-francisco": {
name: "San Francisco",
country: "USA",
},
},
}
Logging to the console strings and variables:
Set a log level:
-> Levels: debug
/info
(=default)/warn
/error
-> Will always print My Content
-> Will print the value of each firstname
Input:
{
people: [
{ firstname: "Nils" },
{ firstname: "Yehuda" },
],
}
Each inline partial is available to the current block and all children, including execution of other partials.
TODO