Functions

Magic wp.* function

The magic wp.* function allows you to use all WordPress native functions. Just replace the * with the function name you’d like to use, e.g. wp.get_post().

Note

You can use all WordPress functions following this syntax: {{ wp.function_name(parameter) }}

Warning

ASA2 does not check what the WP function does or returns. This is up to you. If the function does not exist, an empty string will be returned.

Examples

Here are some examples to demonstrate the flexibility of the wp.* function.

wp.get_userdata

This example uses the WordPress function get_userdata to retrieve the user data based on the Products placeholder {{ repo_author_id }} which contains the author ID of the Repo item author.

{{ wp.get_userdata( repo_author_id ) }}

To work with the user data you can assign the result to a variable and then access the array keys using the dot separator.

{% set user_data = wp.get_userdata( repo_author_id ) %}
{{ user_data.first_name }}
{{ user_data.last_name }}
{{ user_data.user_login }}
{{ user_data.user_url }}

For more inforamtion about get_userdata visit the get_userdata codex page.

wp.get_post

This example uses the WordPress function get_post to retrieve the data of a post. The post ID comes from a Products custom field (see Custom Fields) called “related_post”.

{% set post_data = wp.get_post( repo_custom_field_related_post ) %}
<h1>{{ post_data.title }}</h1>

For more inforamtion about get_post visit the get_post codex page.

cycle

The cycle function cycles on an array of values:

{% set start_year = date() | date('Y') %}
{% set end_year = start_year + 5 %}

{% for year in start_year..end_year %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}

The array can contain any number of values:

{% set fruits = ['apple', 'orange', 'citrus'] %}

{% for i in 0..10 %}
    {{ cycle(fruits, i) }}
{% endfor %}

date

The date function converts an argument to a date to allow date comparison. The argument must be in one of PHP’s supported date and time formats.

{% if date(RequestTimestamp) < date('-2days') %}
    <p>Product data is more than 2 days old.</p>
{% endif %}

You can pass a timezone as the second argument:

{% if date(RequestTimestamp) < date('-2days', 'Europe/Paris') %}
    <p>Product data is more than 2 days old.</p>
{% endif %}

If no argument is passed, the function returns the current date.

max

The function max returns the biggest value of a sequence or a set of values.

{{ max(1, 3, 2) }}
{{ max([1, 3, 2]) }}

When called with a mapping, max ignores keys and only compares values:

{{ max({2: "e", 1: "a", 3: "b", 5: "d", 4: "c"}) }}
{# returns "e" #}

min

The function min returns the lowest value of a sequence or a set of values.

{{ min(1, 3, 2) }}
{{ min([1, 3, 2]) }}

When called with a mapping, min ignores keys and only compares values:

{{ min({2: "e", 3: "a", 1: "b", 5: "d", 4: "c"}) }}
{# returns "a" #}

random

The random function returns a random value depending on the supplied parameter type.

  • a random item from a sequence;

  • a random character from a string;

  • a random integer between 0 and the integer parameter (inclusive).

{{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}
{{ random('ABC') }}                         {# example output: C #}
{{ random() }}                              {# example output: 15386094 (works as the native PHP mt_rand function) #}
{{ random(5) }}                             {# example output: 3 #}

range

With range you define a custom range for a loop. It returns a list containing an arithmetic progression of integers.

The range function works as the native PHP range function.

{% for i in range(0, 3) %}
    {{ i }},
{% endfor %}

{# outputs 0, 1, 2, 3, #}

When step is given (as the third parameter), it specifies the increment (or decrement):

{% for i in range(0, 6, 2) %}
    {{ i }},
{% endfor %}

{# outputs 0, 2, 4, 6, #}