Filters
Filters are simple methods that modify the output of numbers, strings, variables, and arrays.
They are placed within an output tag {{
}}
and are separated with a pipe character |
.
String/HTML Filters
-
lowercase - converts a string into lowercase.
{{ product.name | lowercase }}
Original: Dash Digital Watch Output : dash digital watch
-
uppercase - converts a string into uppercase.
{{ product.name | lowercase }}
Original: Dash Digital Watch Output : DASH DIGITAL WATCH
-
replace - replaces all occurrences of a string with a substring.
{{ product.name | replace: 'Digital', 'Analog' }}
Original: Dash Digital Watch Output : Dash Analog watch
-
append - appends characters to a string.
{{ product.name | append: ' - best choice' }}
Original: Dash Digital Watch Output : Dash Digital Watch - best choice
-
prepend - prepends characters to a string.
{{ product.name | prepend: 'Best choice - ' }}
Original: Dash Digital Watch Output : Best choice - Dash Digital Watch
-
capitalize - capitalizes the first word in a string.
{{ product.color | capitalize }}
Original: dark red Output : Dark red
-
escape - escapes HTML tags in a string.
{{ product.description | escape }
-
nl2br - inserts a
<br>
linebreak HTML tag in front of each line break in a string.{{ product.short_description | nl2br }
-
remove - removes all occurrences of a substring from a string.
{{ product.name | remove: 'Digital' }}
Original: Dash Digital Watch Output : Dash Watch
-
stripHtml - strips all HTML tags from a string.
{{ product.description | stripHtml }
-
strip all newlines (\n, \r) from a string.
{{ product.description | strip_newlines }}
-
Replace each newline (\n) with html break - replace
\n
to the<br>
.{{ product.description | newline_to_br }}
-
truncate - truncates a string down to 'x' characters.
{{ product.name | truncate: '15' }}
Original: Dash Digital Watch Output : ash Digital Wa
-
truncate string down to x words - truncates a string down to 'x' words.
{{ product.name | truncatewords: '2' }}
Original: Dash Digital Watch Output : Dash Digital
-
plain - converts any text to plain format.
{{ product.description | plain }}
-
ifEmpty - return argument, if the value is an empty string
{{ product.color | ifEmpty: 'Multi color' }}
Original: Output : Multi color
-
dateFormat - converts string to specified date-time format.
{{ product.created_at | dateFormat: 'd.m.Y H:i' }}
Original: 2016-02-18 10:11:12 Output : 18.02.2016 10:11
-
json - converts an object or array to JSON format.
{{ product.gallery | json }}
Number Filters
-
ceil - rounds an output up to the nearest integer.
{{ product.weight | ceil }}
Original: 1.423 Output : 2
-
floor - rounds an output down to the nearest integer.
{{ product.weight | floor }}
Original: 1.423 Output : 1
-
round - rounds the output to the nearest integer or specified number of decimals.
{{ product.weight | round: '2' }}
Original: 1.423 Output : 1.42
{{ product.weight | round }}
Original: 1.423 Output : 1
-
numberFormat - formats the number to a specified format (php function).
{{ product.price | numberFormat: '2', '.', ',' }}
Price/Currency Filters
-
price - formats a price to the default format.
{{ product.price | price }}
Original: 100.4200 Output : 100.42
-
convert - converts a price from the base currency to the specified currency.
{{ product.price | convert: 'EUR' }}
Original: 100 Output : 92.28
-
inclTax - include a tax to the product price.
{{ product.price | inclTax | price }}
-
exclTax - exclude a tax from the product price.
{{ product.price | exclTax | price }}
-
Addition - adds the specified value.
{{ product.price | plus: '2' }}
Original: 100 Output : 102
-
Subtraction - subtracts the specified value.
{{ product.price | minus: '2' }}
Original: 100 Output : 98
-
Multiplication - multiplied by the specified value.
{{ product.price | times: '2' }}
Original: 100 Output : 200
-
Division - divided by the specified value.
{{ product.price | divided_by: '2' }}
Original: 100 Output : 50
-
Modulo - the remainder after dividing by the specified value.
{{ product.price | modulo: '2' }} Original: 100 Output : 0
Array Filters
-
first - return the first element in the array.
-
last - return the last element in the array.
-
join - join an array to a string using glue.
-
count - return the number of elements in the array.
- select - select values for a key from the array.
URL Filters
-
secure - return a secure URL (with https://)
-
unsecure - return an unsecure URL (with http://)
-
mediaSecure - return a media URL using "Base URL for User Media Files"
{{ product.image | mediaSecure }}
Original: http://example.com/pub/media/catalog/product/m/h/mh03-black_main.jpg Output : https://cdn-secure.com/catalog/product/m/h/mh03-black_main.jpg
-
mediaUnsecure - return a media URL using "Secure Base URL for User Media Files "
{{ product.image | mediaUnsecure }}
Original: http://example.com/pub/media/catalog/product/m/h/mh03-black_main.jpg Output : http://cdn.com/catalog/product/m/h/mh03-black_main.jpg
Image Filters
-
resize - resize an image
{{ product.image | resize: 100, 100 }}
Original: http://example.com/pub/media/catalog/product/m/h/mh03-black_main.jpg Output : http://example.com/pub/media/cache/200x200/catalog/product/m/h/mh03-black_main.jpg
Note
{{ product.name | truncate: '150' }}
{{ product.description | plain | truncate: '1000' }}
{{ product.weight | round: '2' }} kg
{{ product.manufacturer | ifEmpty: 'not set' }}