Show Menu
Cheatography

Jinja Cheat Sheet (DRAFT) by

A cheat sheet for Jinja

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic Expres­sions

{{ "­Hello World" }}
Outputs Hello World
{{ foo }}
Outputs value of 'foo' variable
{{ 2 + 3 }}
Outputs number 5
{{ 5 > 10 }}
Outputs "­Fal­se"

If Statement

{% if hour > 5 and hour < 12 %}
  Good Morning!
{% elif number >= 12 and hour < 17 %}
  Good Afternoon!
{% elif number >= 17 %}
  Good Evening!
{% endif %}

For Loop

{% for number in 10 %}
  {{ number }}
{% endfor %}

For loop with conditions

{% for item in states if item.a­ttr­ibu­tes.em­ula­ted­_hu­e_n­ame­|length > 0-%}
{%- if loop.first %}{% elif loop.last %}, and {% else %}, {% endif -%}
{{item.attributes.emulated_hue_name|title}}
{%- endfor -%}.
When iterating through a loop, sometimes we would like to add additional logic based on the loop counter. This is an example.
 

String Manipu­lations

{{ "­Hel­lo" ~ " World" }}
Outputs "­Hello World" (String Concat­ena­tion)
{{ "­Hello World" | lower }}
Outputs "­hello world"
{{ "­hello world" | title }}
Outputs "­Hello World"
{{ "­hello world" | upper }}
Outputs "­HELLO WORLD"
{{ "­hello world".s­plit(' ')[0] }}
Splits by delimiter ' ' and outputs "­hel­lo"
{{ "­hel­lo" | replac­e("h­e", "­she­") }}
Replaces "­he" with "­she­", and outputs "­she­llo­"
{{ "­hel­lo" | length }}
Outputs number of charac­ters, which is 5
{{ "­hel­lo" == "­hel­lo" }}
Compares strings, if equal, returns True
{{ "­Hello world!­"[:4] }}
Get Substring - Returns "­Hel­l", the first 4 characters
{{ "­HEL­LO"[2:4] }}
Get Substring - Returns "­LL"
{{ "­HEL­LO"[:-3] }}
Get Substring - Removes last three charac­ters, Returns "­HE"

Date and Time

{{ now() }}
Prints current date and time value
{{ now().s­tr­fti­me(­"­%m-­%d-­%Y")}}
Outputs "­07-­26-­201­7" by converting datetime value to the specified format
{{ as_tim­est­amp­(now() ) | timest­amp­_cu­sto­m("%­m-%­d-%­Y", true) }}
Outputs "­07-­26-­201­7" by converting datetime value to the specified format
{{ now().s­tr­fti­me(­"­%m")}}
Outputs Month value. For ex: 07
For additional date and time related formats, please visit http:/­/st­rft­ime.org/
 

Number Conver­sions

{{ 10.5 }}
Prints the number as is
{{ 10.500­482737 | int }}
Converts float to decimal
{{ 10.6 | round }}
Rounds to nearest decimal number
{{ 10.4 | round }}
Rounds to nearest decimal number
{{ 10 | float }}
Converts Decimal to float
{{ '%0x' % 255 }}
Changes Decimal to Hex Value
{{ "­%0x­" | format ( some_n­ume­ric­_value | int) }}
Converts numeric value to Hex Value
{{ '%0.2f' % 24.12345 }}
Only displays two digits after decimal point
{{ "­${:.2f­}".f­orm­at(­15.45) }}
Formats currency

Basic Macros

{% macro sayHello() -%}
  Hello!
{%- endmacro %}
Call the macro using {{ sayHello() }}

Random

{{["hello", "hi there!­", "­how­dy!­", "hey there!­" ]|random}}

Troubl­esh­ooting

1. Always convert to proper data type prior to using any filters.
For example,
{{ set value = "­123­" }}
{{ value > 100 }} given the value is of "­str­ing­" type, make sure you convert to decimal first.
{{ (value | int) > 123 }}