字符串过滤器
String filters 用于修改字符串。
append
string |
append:
string
将给定字符串添加到字符串的末尾。
{{ shop.url | append: product.url }}
{
"product": {
"url": "/products/reverse-short-sleeve-tee"
}
}
https://shop.myshoplaza.com/products/reverse-short-sleeve-tee
camelcase
string |
camelcase
将字符串转换为驼峰命名法(CamelCase)。
{{ 'camel-case' | camelcase }}
CamelCase
capitalize
string |
capitalize
将字符串中每个单词的首字母大写。
{{ 'this sentence should start with a capitalized word.' | capitalize }}
This Sentence Should Start With A Capitalized Word.
downcase
string |
downcase
将字符串转换为全小写字符。
{{ product.title | downcase }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
ace cashmere beanie
escape
string |
escape
Escapes special characters in HTML, such as <>, ', ", and &, and converts characters into escape sequences. The filter doesn't affect characters within the string that don’t have a corresponding escape sequence.
{{ '<p>Text to be escaped.</p>' | escape }}
<p>Text to be escaped.</p>
escape_once
string |
escape_once
Escapes a string without changing characters that have already been escaped.
{% assign escaped_text = '<p>Text to be escaped.</p>' | escape %}
{{ escaped_text }}
{{ escaped_text | escape_once }}
<p>Text to be escaped.</p>
<p>Text to be escaped.</p>
handleize
string |
handleize
将字符串转换为 handle。
📘 Note
The
handleizefilter has an alias ofhandle.
{{ product.title | handleize }}
{{ product.title | handle }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
ace-cashmere-beanie
ace-cashmere-beanie
hmac_sha1
string |
hmac_sha1:
string
Converts a string into an SHA-1 hash using a hash message authentication code (HMAC).
The secret key for the message is supplied as a parameter to the filter.
{{ 'Ace' | hmac_sha1: 'Salty' }}
5da24ef75f6a8fc7d28c0fcc7236244d29a4511d
hmac_sha256
string |
hmac_sha256:
string
Converts a string into an SHA-256 hash using a hash message authentication code (HMAC).
The secret key for the message is supplied as a parameter to the filter.
{{ 'Ace' | hmac_sha256: 'Salty' }}
8f688efb30c4e26bed3e14923030f9276e463e7933c88fc18c0ff0c934185418
lstrip
string |
lstrip
删除字符串左侧的所有空白字符。
{% assign text = ' Some fruits create whitespace. ' %}
"{{ text }}"
"{{ text | lstrip }}"
" Some fruits create whitespace. "
"Some fruits create whitespace. "
md5
string |
md5
将字符串转换为 MD5 哈希。
{{ '' | md5 }}
d41d8cd98f00b204e9800998ecf8427e
newline_to_br
string |
newline_to_br
Converts newlines (\n) in a string to HTML line breaks (<br>).
{% capture description %}
<h3>Sound inspired</h3>
<div>Get inspired with Solo Pro wireless headphones.</div>
{% endcapture %}
{{ description | newline_to_br }}
<br>
<h3>Sound inspired</h3>
<br>
<div>Get inspired with Solo Pro wireless headphones.</div>
<br>
pluralize
number |
pluralize:
string,
string
Outputs the singular or plural version of a string based on a given number.
{% assign item_count = cart.cart %}
Cart item count: {{ item_count }} {{ item_count | pluralize: 'item', 'items' }}
{
"cart": {
"item_count": 2
}
}
Cart item count: 2 items
prepend
string |
prepend:
string
将给定字符串添加到字符串的开头。
{{ product.url | prepend: shop.url }}
{
"product": {
"url": "/products/reverse-short-sleeve-tee"
}
}
https://shop.myshoplaza.com/products/reverse-short-sleeve-tee
remove
string |
remove:
string
删除字符串中每个子字符串实例。
{{ "I can't do it!" | remove: "'t" }}
I can do it!
remove_first
string |
remove_first:
string
删除字符串中第一个子字符串实例。
{{ "I don't know why, but I don't know how to do it." | remove_first: "don't" }}
I know why, but I don't know how to do it.
replace
string |
replace:
string,
string
将字符串中每个子字符串实例替换为给定字符串。
{{ product.handle | replace: '-', ' ' }}
{
"product": {
"handle": "ace-cashmere-beanie"
}
}
ace cashmere beanie
replace_first
string |
replace_first:
string,
string
将字符串中第一个子字符串实例替换为给定字符串。
{{ product.handle | replace_first: '-', ' ' }}
{
"product": {
"handle": "ace-cashmere-beanie"
}
}
ace cashmere-beanie
rstrip
string |
rstrip
删除字符串右侧的所有空白字符。
{% assign text = ' Some fruits create whitespace. ' %}
"{{ text }}"
"{{ text | rstrip }}"
" Some fruits create whitespace. "
" Some fruits create whitespace."
sha1
string |
sha1
将字符串转换为 SHA-1 哈希。
{{ 'Ace' | sha1 }}
fc2b5a9cb80ab0a1526f4fbc887646dd245632c2
sha256
string |
sha256
将字符串转换为 SHA-256 哈希。
{{ 'Ace' | sha256 }}
a467da82da540f56097b7224d81612bfa212e26efbf0709606144fee0b82a631
slice
string |
slice
Returns a substring or series of array items, starting at a given 0-based index.
By default, the substring has a length of one character, and the array series has one array item. However, you can provide a second parameter to specify the number of characters or array items.
{{ collection.title | slice: 0 }}
{{ collection.title | slice: 0, 3 }}
{{ collection.tags | slice: 1, 2 | join: ', ' }}
{
"collection": {
"tags": [
"Burning",
"fresh",
"music",
"plant",
"Salty"
],
"title": "All products"
}
}
All products
All
fresh, music
Negative index
You can supply a negative index which will count from the end of the string.
{{ title | slice: -8, 8 }}
{
"collection": {
"title": "All products"
}
}
products
split
string |
split:
string
根据给定分隔符将字符串拆分为子字符串数组。
{% assign title_words = product.handle | split: '-' %}
{% for word in title_words %}
{{ word }}
{% endfor %}
{
"product": {
"handle": "ace-cashmere-beanie"
}
}
ace
cashmere
beanie
strip
string |
strip
删除字符串左侧和右侧的所有空白字符。
{% assign text = ' Some fruits create whitespace. ' %}
"{{ text }}"
"{{ text | strip }}"
" Some fruits create whitespace. "
"Some fruits create whitespace."
strip_html
string |
strip_html
删除字符串中的所有 HTML tags。
<!-- With HTML -->
{{ product.description }}
<!-- HTML stripped -->
{{ product.description | strip_html }}
{
"product": {
"description": "<h3>Sound inspired</h3>\n<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>"
}
}
<!-- With HTML -->
<h3>Sound inspired</h3>
<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>
<!-- HTML stripped -->
Sound inspired
Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.
strip_newlines
string |
strip_newlines
删除字符串中的所有换行符(行分隔符)。
<!-- With newlines -->
{{ product.description }}
<!-- Newlines stripped -->
{{ product.description | strip_newlines }}
{
"product": {
"description": "<h3>Sound inspired</h3>\n<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>"
}
}
<!-- With newlines -->
<h3>Sound inspired</h3>
<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>
<!-- Newlines stripped -->
<h3>Sound inspired</h3><div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>
truncate
string |
truncate:
number
Truncates a string down to a given number of characters.
If the specified number of characters is less than the length of the string, then an ellipsis (...) is appended to the truncated string. The ellipsis is included in the character count of the truncated string.
{{ product.title | truncate: 10 }}
{{ product.title | truncate: 20 }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
Ace Cas...
Ace Cashmere Beanie
Specify a custom ellipsis
string |
truncate:
number,
string
You can provide a second parameter to specify a custom ellipsis. If you don't want an ellipsis, then you can supply an empty string.
{{ product.title | truncate: 10, '' }}
{{ product.title | truncate: 10, '···' }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
Ace Cashme
Ace ···
truncatebytes
string |
truncatebytes:
number
Truncates a string down to a given number of bytes.
If the specified number of characters is less than the length of the string, then an ellipsis (...) is appended to the truncated string. The ellipsis is included in the character count of the truncated string.
{{ product.description | truncatebytes: 50 }}
{
"product": {
"description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
}
}
Get inspired with Solo Pro wireless headphones....
Specify a custom ellipsis
string |
truncatebytes:
number,
string
You can provide a second parameter to specify a custom ellipsis. If you don't want an ellipsis, then you can supply an empty string.
{{ product.description | truncatebytes: 50, '' }}
{{ product.description | truncatebytes: 50, '···' }}
{
"product": {
"description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
}
}
Get inspired with Solo Pro wireless headphones. To
Get inspired with Solo Pro wireless headphon···
truncatewords
string |
truncatewords:
number
Truncates a string down to a given number of words.
If the specified number of words is less than the number of words in the string, then an ellipsis (...) is appended to the truncated string.
{{ product.description | truncatewords: 10 }}
{
"product": {
"description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
}
}
Get inspired with Solo Pro wireless headphones. To deliver sound...
Specify a custom ellipsis
string |
truncatewords:
number,
string
You can provide a second parameter to specify a custom ellipsis. If you don't want an ellipsis, then you can supply an empty string.
{{ product.description | truncatewords: 10, '' }}
{{ product.description | truncatewords: 10, '---' }}
{
"product": {
"description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
}
}
Get inspired with Solo Pro wireless headphones. To deliver sound
Get inspired with Solo Pro wireless headphones. To deliver sound---
upcase
string |
upcase
将字符串转换为全大写字符。
{{ product.title | upcase }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
ACE CASHMERE BEANIE
url_decode
string |
url_decode
Decodes any percent-encoded characters in a string.
{{ 'https%3A%2F%2Fwww.shoplazza.com%2F' | url_decode }}
https://www.shoplazza.com/
url_encode
string |
url_encode
Converts any URL-unsafe characters in a string to the percent-encoded equivalent.
📘 Note
Spaces are converted to a
+character, instead of a percent-encoded character.
{{ 'https://www.shoplazza.com/' | url_encode }}
https%3A%2F%2Fwww.shoplazza.com%2F