Download

{{#checkIf}} helper

An extended version of the #if block helper with support for operators. Useful if you need to compare two arguments in a non-standard way.

An extended version of the #if block helper with support for operators allows for more complex comparisons between two arguments. This utility is particularly useful when standard equality checks do not suffice.

Supported operators:

OperatorWord EquivalentDescription
==equalequal to
!=differentnot equal
===strictEqualequal value and equal type
!==strictDifferentnot equal value or not equal type
&&andlogical and
||orlogical or
<lesserless than
<=lesserEqualless than or equal to
>greatergreater than
>=greaterEqualgreater than or equal to
contains-checks if a value is in an array
notContains-checks if a value is not in an array

Descriptions of operators:

  • equal: Checks if two values are equal.
  • different: Checks if two values are not equal.
  • strictEqual: Checks if two values are equal in value and type.
  • strictDifferent: Checks if two values are not equal in value or type.
  • and: Checks if both conditions are true.
  • or: Checks if at least one condition is true.
  • lesser: Checks if one value is less than another.
  • lesserEqual: Checks if one value is less than or equal to another.
  • greater: Checks if one value is greater than another.
  • greaterEqual: Checks if one value is greater than or equal to another.
  • contains: Verifies if an array contains a specific value.
  • notContains: Confirms that an array does not contain a specific value.

Syntax:

{{#checkIf ARG_1 OPERATOR ARG_2}}
   Code displayed when above condition is true
{{else}}
   Code displayed when above condition is false
{{/checkIf}}

Example:

You can use:

{{#checkIf author '&&' author.avatar}}
    <img src="{{author.avatar}}" alt="" />
{{/checkIf}}

Instead of:

{{#if author}}
    {{#if author.avatar}}
        <img src="{{author.avatar}}" alt="" />
    {{/if}}
{{/if}}

We can also find out if we are in a particular post by ID; eg. add a code just for the post with ID=10

{{#post}}
   {{#checkIf id '==' 10}}
         your code here...
   {{/checkIf}}
{{/post}}

To check if an array contains a specific value:

{{#each tags}}
   {{#checkIf '1,2,3' 'contains' id}}
       <a href="{{url}}" class="filter__item">{{name}}</a>
   {{/checkIf}}
{{/each}}

So, the first argument is a string separated by commas. The type of the second argument (id) is also detected - if it is a number, the array for comparison purposes is mapped to numbers.

To ensure a value is not within an array:

{{#each tags}}
   {{#checkIf '1,2,3' 'notContains' id}}
       <a href="{{url}}" class="filter__item">{{name}}</a>
   {{/checkIf}}
{{/each}}

What are you waiting for?

Start building your site today.

  1. 1 Download Publii
  2. 2 Write your content
  3. 3 Publish your site
Create website