Home Explore Blog CI



tera

9th chunk of `docs/content/docs/_index.md`
e5e0b583e78add66827705c2c6164826cfeccf26cf573dd40000000100000fb1
Takes 2 mandatory string named arguments: `from` and `to`. It will return a string with all instances of
the `from` string replaced with the `to` string.

Example: `{{ name | replace(from="Robert", to="Bob")}}`

#### addslashes
Adds slashes before quotes.

Example: `{{ value | addslashes }}`

If value is "I'm using Tera", the output will be "I\\'m using Tera".

#### slugify
Only available if the `builtins` feature is enabled.

Transforms a string into ASCII, lowercases it, trims it, converts spaces to hyphens and
removes all characters that are not numbers, lowercase letters or hyphens.

Example: `{{ value | slugify }}`

If value is "-Hello world! ", the output will be "hello-world".

#### title
Capitalizes each word inside a sentence.

Example: `{{ value | title }}`

If value is "foo  bar", the output will be "Foo  Bar".

#### trim
Removes leading and trailing whitespace if the variable is a string.

#### trim_start
Removes leading whitespace if the variable is a string.

#### trim_end
Removes trailing whitespace if the variable is a string.

#### trim_start_matches
Removes leading characters that match the given pattern if the variable is a string.

Example: `{{ value | trim_start_matches(pat="//") }}`

If value is "//a/b/c//", the output will be "a/b/c//".

#### trim_end_matches
Removes trailing characters that match the given pattern if the variable is a string.

Example: `{{ value | trim_end_matches(pat="//") }}`

If value is "//a/b/c//", the output will be "//a/b/c".

#### truncate
Only available if the `builtins` feature is enabled.

Truncates a string to the indicated length. If the string has a smaller length than
the `length` argument, the string is returned as is.

Example: `{{ value | truncate(length=10) }}`

By default, the filter will add an ellipsis at the end if the text was truncated. You can
change the string appended by setting the `end` argument.
For example, `{{ value | truncate(length=10, end="") }}` will not append anything.

#### linebreaksbr
Replaces line breaks (`\n` or `\r\n`) with HTML line breaks (`<br>`).

Example: `{{ value | linebreaksbr }}`

If value is "Hello\r\nworld\n", the output will be "Hello&lt;br&gt;world&lt;br&gt;".

Note that if the template you are using it in is automatically escaped, you will
need to call the `safe` filter after `linebreaksbr`.

#### spaceless
Remove space (` `) and line breaks (`\n` or `\r\n`) between HTML tags

Example: `{{ value | spaceless }}`

If the value is "&lt;p&gt;\n&lt;a&gt; &lt;/a&gt;\r\n &lt;/p&gt;", the output will be "&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;".

Note that only whitespace between successive opening tags and successive closing tags is removed.

Also note that if the template you are using it in is automatically escaped, you will
need to call the `safe` filter after `spaceless`.

#### indent
Indents a string by injecting a prefix at the start of each line.  The `prefix` argument (default 4 spaces) specifies the prefix to insert per line.  If the `first` argument (default false) is set true spaces are inserted for the first line.  If the `blank` argument (default false) is set true spaces are inserted for blank/whitespace lines.

#### striptags
Tries to remove HTML tags from input. Does not guarantee well formed output if input is not valid HTML.

Example: `{{ value | striptags }}`

If value is "&lt;b&gt;Joel&lt;/b&gt;", the output will be "Joel".

Note that if the template you are using it in is automatically escaped, you will need to call the `safe` filter
after `striptags`.

#### first
Returns the first element of an array.
If the array is empty, returns empty string.

#### last
Returns the last element of an array.
If the array is empty, returns empty string.

#### nth
Returns the nth element of an array.§
If the array is empty, returns empty string.
It takes a required `n` argument, corresponding to the 0-based index you want to get.

Example: `{{ value | nth(n=2) }}`

#### join
Joins an array with a string.

Example: `{{ value | join(sep=" // ") }}`

Title: Tera Built-in Filters (Continued)
Summary
This section continues the list of Tera's built-in filters, detailing their purpose, usage, and example outputs. The filters covered are: `replace`, `addslashes`, `slugify`, `title`, `trim`, `trim_start`, `trim_end`, `trim_start_matches`, `trim_end_matches`, `truncate`, `linebreaksbr`, `spaceless`, `indent`, `striptags`, `first`, `last`, `nth`, and `join`.