String Functions
at
at(index?: int = 0): str
Returns the character at the given index.
- When the index is out of bounds, it will return
nil
type, which will be converted to an empty string when rendered in the template - When the index is negative, it will count from the end of the string. For example,
-1
will return the last character of the string
Arguments
index
(int) - The index of the character to return. Default is0
, which returns the first character of the string
Input example
{{ "Textwire".at(1) }}
Output
e
capitalize
capitalize(): str
Capitalizes the first letter of a string
Input example
{{ "hello, world!".capitalize() }}
Output
Hello, world!
contains
contains(substr: str): bool
Returns true if the string contains the given substring, otherwise false. The function is sase-sensitive, so the substring must match the case of the string
Arguments
substr
(str) - The substring to search for
Input example
{{ "Hello, World!".contains("World") }}
Output
true
If the substr
argument is an empty string, the function will always return true
. It's done this way because an empty string is always a substring of any string
If you want to search for a substring without considering the case, you can use the lower
function to convert both the string and the substring to lowercase:
{{ "Hello, World!".lower().contains("world") }} <!-- true -->
decimal
decimal(separator?: str = ".", decimals?: int = 2): str
Converts to a string with a decimal part by appending a decimal separator and the number of decimal places. Here are some rules:
- When the string is not a number, it will return the string as is
- When the string is already a decimal number, it will return the string as is
- When the you use on a string, it will return the string as is if it's not a number
Arguments
separator
(str) (optional) - The separator to use for the decimal. Default is"."
decimals
(int) (optional) - The number of decimal places add to the number. Default is2
Input example
{{ "123".decimal() }}
Output
123.00
first
first(): str
Returns the first character of a string. When the index is out of bounds, it will return nil
type, which will be converted to an empty string when rendered in the template
Input example
{{ "Textwire".first() }}
Output
T
last
last(): str
Returns the last character of a string. When the index is out of bounds, it will return nil
type, which will be converted to an empty string when rendered in the template
Input example
{{ "Textwire".last() }}
Output
e
len
len(): int
Returns the length of the string
Input example
{{ "Hello, World!".len() }}
Output
13
lower
lower(): str
Converts a string to lowercase
Input example
<span>{{ "Hello, World!".lower() }}<span>
Output
<span>hello, world!</span>
raw
raw(): str
Function raw
is used to render a string as raw HTML. This is useful when you want to render HTML tags from a string. By default, HTML tags in a string are escaped to prevent XSS attacks
Input example
{{ "<h1>Test</h1>".raw() }}
Output
<h1>Test</h1>
reverse
reverse(): str
Returns a string with the characters reversed
Input example
{{ "stressed".reverse() }}
Output
desserts
split
split(separator?: str = " "): arr
Function split
is used to split a string into an array of substrings. It takes an optional argument separator
which is used to split the string. If no separator is provided, it defaults to a space
Arguments
separator
(str) (optional) - What separator to use to split the string. Default is" "
(space)
Input example
<div>{{ "one two".split(" ") }}</div>
Output
<div>one, two</div>
trim
trim(chars?: str = "\t \n\r"): str
Trims a string from spaces and special characters like tabs, spaces and new lines by default. You can pass a argument to trim a specific set of characters from a string
Arguments
chars
(str) (optional) - A string of characters to trim from a string. Default is"\t \n\r"
Input example
<span>{{ " Anna ".trim() }}</span>
Output
<span>Anna</span>
truncate
truncate(length: int, ellipsis: str = "..."): str
Returns a string truncated to the given length with an optional ellipsis at the end
Arguments
length
(int) - The length to truncate the string toellipsis
(str) (optional) - The ellipsis to append to the truncated string. Default is"..."
Input example
{{ "Hello, World!".truncate(5) }}
Output
Hello...
upper
upper(): str
Converts a string to uppercase
Input example
{{ "Hello, World!".upper() }}
Output
HELLO, WORLD!