All Products
Search
Document Center

Simple Log Service:Built-in template functions

Last Updated:Jun 16, 2026

Built-in functions for content templates allow you to manipulate data and enrich the format and style of your notifications. This topic describes the syntax and examples of these built-in functions.

General functions

Math functions

Function

Description

Filter

Example

float(value, default=0.0)

Converts an integer or a string to a floating-point number.

If the conversion fails, the function returns 0.0 by default. Use the default parameter to specify a different return value for a failed conversion.

Supported

  • The result of {{ float("123") }} is 123.0.

  • The result of {{ float("foo") }} is 0.0.

  • The result of {{ float("foo", default=1.23) }} is 1.23.

int(value, default=0)

Converts a string or a number to an integer.

If the conversion fails, the function returns 0 by default. Use the default parameter to specify a different return value for a failed conversion.

Support

  • The result of {{ int(1.23) }} is 1.

  • The result of {{ int("1.23") }} is 1.

  • The result of {{ int("foo") }} is 0.

  • The result of {{ int("foo", default=5) }} is 5.

length(value)

Returns the length or number of items in an object, such as a string, list, or tuple.

Supported

  • The result of {{ length("foo") }} is 3.

  • The result of {{ length([1, 2]) }} is 2.

  • The result of {{ length({"foo": "bar"}) }} is 1.

abs(value)

Returns the absolute value of a number.

Support

The result of {{ abs(-1) }} is 1.

min(value)

Returns the minimum value.

Support

The result of {{ min([1, 3, 2]) }} is 1.

max(value)

Returns the maximum value.

Supported

The result of {{ max([1, 3, 2]) }} is 3.

ceil(value)

Rounds a number up to the nearest integer.

Support

The result of {{ ceil(1.23) }} is 2.

floor(value)

Rounds a number down to the nearest integer.

Supported

The result of {{ floor(1.23) }} is 1.

round(value, 1)

Rounds a number to the nearest integer.

The argument specifies the number of decimal places to retain. For example, 1 retains one decimal place.

Support

  • The result of {{ round(1.23) }} is 1.

  • The result of {{ round(1.56) }} is 2.

  • The result of {{ round(1.56, 1) }} is 1.6.

sum(value)

Calculates the sum.

Supported

The result of {{ sum([1, 2, 3]) }} is 6.

String functions

Function

Description

Filter

Example

string(value)

Converts an object to a string.

Support

The result of {{ string(1.23) }} is 1.23.

In this case, 1.23 is a string.

capitalize(value)

Converts the first character of a string to uppercase and all other characters to lowercase.

Support

The result of {{ capitalize("heLLO World") }} is Hello world.

lower(value)

Converts a string to lowercase.

Support

The result of {{ lower("FOO") }} is foo.

upper(value)

Converts a string to uppercase.

Support

The result of {{ upper("foo") }} is FOO.

title(value)

Returns a title-cased version of the string, where the first character of each word is uppercase and the rest are lowercase.

Support

The result of {{ title("hello world") }} is Hello World.

trim(value)

Removes whitespace from the beginning and end of a string.

Supported

The result of {{ trim(" foo\n") }} is foo.

replace(value, old, new)

Replaces a substring within a string.

Not supported

The result of {{ replace("foo", "oo", "ly") }} is fly.

wordcount(value)

Counts the number of words in a string.

Support

The result of {{ wordcount("hello world") }} is 2.

truncate(value, n, end='')

Truncates a string.

  • Use `truncate(value, n)` to specify the number of characters to keep.

  • Use `truncate(value, n, end='...')` to specify a suffix to add.

Not supported

  • The result of {{ truncate("foo bar", 5) }} is foo b.

  • The result of {{ truncate("foo bar", 5, end="...") }} is foo b....

quote(value)

Encloses a string in double quotation marks ("").

Support

  • The result of {{ quote(123) }} is "123"

  • The result of {{ quote("foo") }} is "foo".

indent(value, n=4)

Indents each line of a string. The default indent is 4 spaces.

Use the n parameter to specify the number of spaces for the indent.

Supported

  • The result of {{ "foobar\n" }}{{ indent("foo\nbar") }} is as follows:

    foobar
        foo
        bar
  • The result of {{ "foobar\n" }}{{ indent("foo\nbar", 2) }} is as follows:

    foobar
      foo
      bar

startswith(value, prefix)

Checks if a string starts with a specific prefix.

Support

The result of {{ startswith("football", "foo") }} is true.

endswith(value, suffix)

Checks if a string ends with a specific suffix.

Support

The result of {{ endswith("football", "all") }} is true.

removeprefix(value, prefix)

Removes the prefix from a string.

Support

The result of {{ removeprefix("football", "foot") }} is ball.

removesuffix(value, suffix)

Removes the suffix from a string.

Supported

The result of {{ removesuffix("football", "ball") }} is foot.

split(value, sep=None, maxsplit=-1)

Splits a string.

  • Use the sep parameter to specify the separator.

  • Use the maxsplit parameter to limit the number of splits.

    If maxsplit is not specified or is set to -1, there is no limit on the number of splits.

Support

  • The result of {{ split('a b c ') }} is ['a', 'b', 'c'].

  • The result of {{ split('a-b-c', sep='-') }} is ['a', 'b', 'c'].

  • The result of {{ split('a-b-c', sep='-', maxsplit=1) }} is ['a', 'b-c'].

  • The result of {{ split('a<>b<>c', sep='<>') }} is ['a', 'b', 'c'].

List and object functions

Function

Description

Filter

Example

enumerate(value)

Pairs each element in an iterable with its index, returning a list of (index, element) tuples.

Not supported

The result of {{ enumerate(["foo", "bar"]) }} is [(0, 'foo'), (1, 'bar')].

list(value)

Converts an iterable object to a list.

Supported

  • The result of {{ list(("foo", "bar")) }} is ['foo', 'bar'].

  • The result of {{ list("foo") }} is ['f', 'o', 'o'].

dict(value)

Creates a dictionary, similar to using {}.

Not supported

The result of {{ dict(foo=1, bar="hello") }} is {'foo': 1, 'bar': 'hello'}.

first(value)

Returns the first item in a list.

Support

The result of {{ first([1, 2, 3]) }} is 1.

last(value)

Returns the last item in a list.

Supported

The result of {{ last([1, 2, 3]) }} is 3.

sort(value, reverse=true)

Sorts the elements of a list.

To sort in reverse order, specify reverse=true.

Help and support

  • The result of {{ sort([3, 1, 2]) }} is [1, 2, 3].

  • The result of {{ sort([3, 1, 2], reverse=true) }} is [3, 2, 1].

dictsort(value)

Sorts the key-value pairs of an object by key and returns an array of the sorted pairs.

Support

  • Example of the alert.labels field

    {
        "host": "host-1",
        "app": "nginx"
    }
  • Content template configuration

    {%- for key, val in dictsort(alert.labels) %}
    {{ key }}: {{ val }}
    {%- endfor %}
  • Result

    app: nginx
    host: host-1

join(value, d='')

Joins the elements of a list with a separator.

Use the d parameter to specify the separator.

Support

  • The result of {{ join([1, 2, 3]) }} is 123.

  • The result of {{ join([1, 2, 3], ',') }} is 1,2,3.

Formatting functions

Function

Description

Filter

Example

escape_markdown(value)

Escapes special Markdown characters.

Support

The result of {{ escape_markdown("__a__ **b** #c") }} is &#95;&#95;a&#95;&#95; &#42;&#42;b&#42;&#42; &#35;c.

escape_html(value)

Escapes special HTML characters.

Support

The result of {{ escape_html("<div>") }} is &lt;div&gt;.

to_json(value)

Converts an object to JSON format.

Support

  • The result of {{ to_json("foo") }} is "foo".

  • The result of {{ to_json(1.23) }} is 1.23.

  • The result of {{ to_json(True) }} is true.

  • The result of {{ to_json(alert.labels) }} is {"host": "host-1", "app": "nginx"}.

parse_json(value)

Parses a string into a JSON data structure.

Support

  • The result of {{ parse_json('{"foo": "bar"}').foo }} is bar.

  • The result of {{ parse_json('[1, 2, 3]')[1] }} is 2.

Encoding and decoding functions

Function

Description

Filter

Example

base64_encoding(value)

Encodes the input value in Base64.

Supported

The result of {{ base64_encoding("foo") }} is Zm9v.

base64_decoding(value)

Decodes the Base64-encoded input value.

Support

The result of {{ base64_decoding("Zm9v") }} is foo.

md5_encoding(value)

Generates the MD5 hash of the input value.

Support

The result of {{ md5_encoding("foo") }} is acbd18db4cc2f85cedef654fccc4a4d8.

url_encoding(value)

URL-encodes the input value.

Support

The result of {{ url_encoding("https://example.com?a=b&c=d") }} is https%3A%2F%2Fexample.com%3Fa%3Db%26c%3Dd.

url_decoding(value)

URL-decodes the input value.

Support

The result of {{ url_decoding("https%3A%2F%2Fexample.com%3Fa%3Db%26c%3Dd") }} is https://example.com?a=b&c=d.

Date and time functions

Function

Description

Filter

Example

parse_date(value, fmt="%Y-%m-%d %H:%M:%S")

Converts a Unix timestamp or formatted string to a datetime object.

Use the fmt parameter to specify the datetime format.

Support

  • The result of {{ parse_date(1629820800) }} is 2021-08-25 00:00:00.

  • The result of {{ parse_date("2021|08|25|00|00|00", fmt="%Y|%m|%d|%H|%M|%S") }} is 2021-08-25 00:00:00.

format_date(value, tz=None, fmt="%Y-%m-%d %H:%M:%S")

Formats a datetime value as a string.

Use the fmt parameter to specify the output format.

If the input value is not a date object, the function first converts it to a date object and then formats it. For more information about datetime format directives, see Datetime formatting instructions. For more information about time zones, see Time zones.

Not supported

  • The result of {{ format_date(1629820800) }} is 2021-08-25 00:00:00.

  • The result of {{ format_date(1629820800, fmt="%Y/%m/%d %H:%M:%S") }} is 2021/08/25 00:00:00.

  • The result of {{ format_date(1629820800, tz="UTC", fmt="%Y/%m/%d %H:%M:%S") }} is 2021/08/24 16:00:00.

timestamp(value)

Converts a date and time string to a Unix timestamp.

If the input value is not a date object, the function first converts it to a date object before returning the timestamp.

Supported

  • The result of {{ timestamp("2021-08-25 00:00:00") }} is 1629820800.

  • The result of {{ timestamp(parse_date("2021-08-25 00:00:00")) }} is 1629820800.

format_duration(value, locale='en-US', sep='')

Formats a time interval. The unit of value is seconds.

Use the locale parameter to specify the language. For valid values of the locale parameter, see Valid values for the locale parameter in alerting functions.

Support

  • The result of {{ 10 | format_duration }} is 10s.

  • The result of {{ 60 | format_duration }} is 1m.

  • The result of {{ 3600 | format_duration }} is 1h.

  • The result of {{ 86400 | format_duration }} is 1d.

  • The result of {{ 100000 | format_duration }} is 1d3h46m40s.

  • The result of {{ 100000 | format_duration(sep=",") }} is 1d,3h,46m,40s.

Alerting functions

Alerting functions operate within the alert context and adapt to the content template configuration. They automatically detect the following:

Note

Running alerting functions in different alert contexts may return different results.

  • Alert properties, such as the severity and status of the current alert.

  • The language configured in the content template, such as Chinese or English.

  • The notification channel, such as DingTalk or email.

Function

Description

Filter

Example

format_type(alert.type, locale=None)

Converts the alert type to a text description.

Use the locale parameter to specify the language. For valid values of the locale parameter, see Valid values for the locale parameter in alerting functions.

Support

  • The alert type is sls_pub.

  • Content template configuration

    {{ format_type(alert.type) }}
  • Result

    • If the language in the content template is set to Chinese, the notification content is alert ingestion system.

    • If the language in the content template is set to English, the notification content is Pub Alert.

format_region(alert.region, locale=None)

Converts the alert region to a text description.

Use the locale parameter to specify the language. For valid values of the locale parameter, see Valid values for the locale parameter in alerting functions.

Support

  • The current alert region is cn-hangzhou.

  • Content template configuration

    {{ format_region(alert.region) }}
  • Result

    • If the language in the content template is set to Chinese, the notification content is China (Hangzhou).

    • If the language in the content template is set to English, the notification content is China (Hangzhou).

format_severity(alert.severity, locale=None)

Converts the alert severity to a text description with colored formatting.

Note

Colored text is supported only in DingTalk, WeCom, email, and Message Center notifications.

Use the locale parameter to specify the language. For valid values of the locale parameter, see Valid values for the locale parameter in alerting functions.

Support

  • The alert severity is 6.

  • Content template configuration

    {{ format_severity(alert.severity) }}
  • Result

    • If the language in the content template is set to Chinese, the notification content is Medium in yellow font.

    • If the language in the content template is set to English, the notification content is Medium in yellow font.

format_status(alert.status, locale=None)

Converts the alert status to a text description with colored formatting.

Note

Colored text is supported only in DingTalk, WeCom, email, and Message Center notifications. For other channels, the output remains unchanged.

Use the locale parameter to specify the language. For valid values of the locale parameter, see Valid values for the locale parameter in alerting functions.

Support

  • The current alert is in the Firing state.

  • Content template configuration

    {{ format_status(alert.status) }}
  • Result

    • If the language in the content template is set to Chinese, the notification content is Firing in red font.

    • If the language in the content template is set to English, the notification content is Firing in red font.

to_list(value)

Converts an array or object to a list.

Support

  • Alert labels

    {
        "app": "nginx",
        "host": "host-1"
    }
  • Content template configuration

    {{ to_list(alert.labels) }}
  • Result

    • If the notification content is in Markdown format, the result is as follows:

      The function automatically escapes Markdown characters based on the requirements of the notification channel.

      - app: nginx
      - host: host&#45;1
    • If the notification content is in HTML format, the result is as follows:

      <ul>
        <li>app: nginx</li>
        <li>host: host-1</li>
      </ul>
    • If the notification content is in plain text format, the result is as follows:

      [app: nginx][host: host-1]

annotations_to_list(alert.annotations, locale=None)

Converts alert annotations to a list. This function works like to_list(alert.annotations), but automatically maps standard field names to human-readable labels. For example, it converts the title field to Title or Title. For a list of standard names, see Alert annotation field mappings.

Use the locale parameter to specify the language. For valid values of the locale parameter, see Valid values for the locale parameter in alerting functions.

Support

  • Alert annotations

    {
        "title": "Nginx access abnormal",
        "desc": "PV decreased by 80% year-over-year",
        "cnt": "120"
    }
  • Content template configuration

    {{ annotations_to_list(alert.annotations) }}
  • Result

    If the notification content is in Markdown format, the result is as follows:

    - Title: Nginx access abnormal
    - Description: PV decreased by 80% year-over-year
    - cnt: 120

blockquote(value)

Adds blockquote formatting to the notification content.

  • If the notification content is in Markdown format, a > symbol is added to the beginning of each line.

  • If the notification content is in HTML format, the content is wrapped in a <blockquote> tag.

Supported

  • Content template configuration

    {{ blockquote("foo\nbar") }}
  • Result

    • If the notification content is in Markdown format, the result is as follows:

      > foo
      > bar
    • If the notification content is in HTML format, the result is as follows:

      <blockquote>
      foo
      bar
      </blockquote>

References

  • Valid values for the locale parameter in alerting functions

    locale value

    Description

    None or an empty string

    Uses the language configured in the content template.

    en-US

    English.

    zh-CN

    Chinese.

  • Alert annotation field mappings

    Annotation

    Mapping value (Chinese)

    Mapping value (English)

    title

    Title

    Title

    desc

    Description

    Description

    anomaly_score

    Anomaly Score

    Anomaly Score

    job_id

    Task ID

    Task ID

    model_id

    Model ID

    Model ID

    severity

    Anomaly Severity

    Anomaly Severity

    __pub_alert_app__

    Application

    Application

    __pub_alert_protocol__

    Protocol

    Protocol

    __pub_alert_region__

    Region

    Region

    __pub_alert_service__

    Service

    Service

    __ensure_url__

    Anomaly Confirmation

    Anomaly Confirmation

    __mismatch_url__

    False Positive Confirmation

    False Positive Confirmation

    __plot_image__

    Time Series Chart

    Time Series Chart

    __host_ip__

    Machine Address

    Machine Address

    __host_group_name__

    Machine Group Name

    Machine Group Name

    __cloud_monitor_type__

    CloudMonitor

    CloudMonitor