Hawkejs syntax

Next to EJS, Hawkejs also supports a custom syntax. For this, it uses the {% %} delimiter

Statements

If

Every syntax requires an If keyword. Ours is not fundamentally different from JavaScript's:


    {% if my_variable %}
        Print this text if `my_variable` is truthy
    {% /if %}

What counts as "truthy" in this case?

  • Non-empty strings
  • True booleans
  • Non-zero numbers
  • Non-empty arrays
  • Non-empty objects

Each

Iterating over items can be done with the each keyword:


    {% each items as key, val %}
        {%= key %}: {%= val %}
    {% /if %}

With

The with statement is totally different from the one in plain JavaScript. It's actually used to work with arrays and objects.

Inside the with block, you can use the following branches. (These can be put in any order and repeated as many time as you want)

  • none: Branch that is executed if the context variable is empty
  • all: Branch that is executed if the context variable is NOT empty
  • single: Branch that is executed if the context variable contains only 1 element
  • multiple: Branch that is executed if the context variable contains more than 1 element
  • each: This is the actual iteration branch: it will be repeated for every element in the array/object

    {% with my_array as entry %}
        This line will be printed only once.
        
        {% none %}
            This line will be printed if `my_array` is empty
        {% /none %}
        
        {% all %}
            This line will be printed if `my_array` is NOT empty
        {% /all %}
        
        {% single %}
            This line will be printed if `my_array` contains only 1 element
        {% /single %}
        
        {% multiple %}
            This line will be printed if `my_array` contains more than 1 element
        {% /multiple %}
        
        {% each %}
            This line will be repeatedly be printed for each element in the `my_array` variable.
            The value can be reached with the {%= entry %} name
        {% /each %}
        
    {% /with %}

Expressions

An expression is any valid set of literals, variables, operators, and expressions that evaluates to a single value.

Here are the operators you can use in expressions, used in if statements as example:

Operators

Not

We can negate something by using the `not` keyword:


    {% if not my_variable %}
        Print this text if `my_variable` is falsy
    {% /if %}

Or


    {% if my_variable or other_variable %}
        Print this text if `my_variable` or `other_variable` is truthy
    {% /if %}

And


    {% if my_variable and other_variable %}
        Print this text if `my_variable` and `other_variable` is truthy
    {% /if %}

Eq


    {% if my_variable eq "something" %}
        Print this text if `my_variable` equals "something"
    {% /if %}

Neq


    {% if my_variable neq "something" %}
        Print this text if `my_variable` is not equal to "something"
    {% /if %}

Gt


    {% if my_variable gt 1 %}
        Print this text if `my_variable` is greater than 1
    {% /if %}

Ge


    {% if my_variable ge 1 %}
        Print this text if `my_variable` is greater than or equal to 1
    {% /if %}

Lt


    {% if my_variable lt 1 %}
        Print this text if `my_variable` is greater than 1
    {% /if %}

Le


    {% if my_variable le 1 %}
        Print this text if `my_variable` is greater than or equal to 1
    {% /if %}

Starts with


    {% if my_variable starts with "prefix" %}
        Print this text if `my_variable` starts with the string "prefix"
    {% /if %}

Ends with


    {% if my_variable ends with "suffix" %}
        Print this text if `my_variable` ends with the string "suffix"
    {% /if %}

Empty


    {% if my_variable empty %}
        Print this text if `my_variable` is empty
    {% /if %}

Empty in this case would be:

  • An empty string
  • A string containing only whitespaces
  • An empty array
  • An empty object
  • Any falsy values (0, null, undefined, false)