跳到主要内容

字符串过滤器

字符串过滤器用于修改字符串

append

string | append: string
returns  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
returns  string

将字符串转换为驼峰命名法(CamelCase)。

{{ 'camel-case' | camelcase }}
CamelCase

capitalize

string | capitalize
returns  string

将字符串中每个单词的首字母大写。

{{ 'this sentence should start with a capitalized word.' | capitalize }}
This Sentence Should Start With A Capitalized Word.

downcase

string | downcase
returns  string

将字符串转换为全小写字符。

{{ product.title | downcase }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
ace cashmere beanie

escape

string | escape
returns  string

转义 HTML 中的特殊字符(例如 <>'"&),并将这些字符转换为转义序列。该过滤器不会影响字符串中没有对应转义序列的字符。

{{ '<p>Text to be escaped.</p>' | escape }}
&lt;p&gt;Text to be escaped.&lt;/p&gt;

escape_once

string | escape_once
returns  string

转义字符串,但不会改变已经转义过的字符。

{% assign escaped_text = '<p>Text to be escaped.</p>' | escape %}

{{ escaped_text }}
{{ escaped_text | escape_once }}
&lt;p&gt;Text to be escaped.&lt;/p&gt;
&lt;p&gt;Text to be escaped.&lt;/p&gt;

handleize

string | handleize
returns  string

将字符串转换为 handle

📘 注意

handleize 过滤器有一个别名 handle

{{ product.title | handleize }}
{{ product.title | handle }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
ace-cashmere-beanie
ace-cashmere-beanie

hmac_sha1

string | hmac_sha1: string
returns  string

使用哈希消息认证码(HMAC)将字符串转换为 SHA-1 哈希。

消息的密钥作为参数提供给过滤器。

{{ 'Ace' | hmac_sha1: 'Salty' }}
5da24ef75f6a8fc7d28c0fcc7236244d29a4511d

hmac_sha256

string | hmac_sha256: string
returns  string

使用哈希消息认证码(HMAC)将字符串转换为 SHA-256 哈希。

消息的密钥作为参数提供给过滤器。

{{ 'Ace' | hmac_sha256: 'Salty' }}
8f688efb30c4e26bed3e14923030f9276e463e7933c88fc18c0ff0c934185418

lstrip

string | lstrip
returns  string

删除字符串左侧的所有空白字符。

{% assign text = ' Some fruits create whitespace. ' %}
"{{ text }}"
"{{ text | lstrip }}"
" Some fruits create whitespace. "
"Some fruits create whitespace. "

md5

string | md5
returns  string

将字符串转换为 MD5 哈希。

{{ '' | md5 }}
d41d8cd98f00b204e9800998ecf8427e

newline_to_br

string | newline_to_br
returns  string

将字符串中的换行符(\n)转换为 HTML 换行标签(<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
returns  string

根据给定数字输出字符串的单数或复数形式。

{% 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
returns  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
returns  string

删除字符串中每个子字符串实例。

{{ "I can't do it!" | remove: "'t" }}
I can do it!

remove_first

string | remove_first: string
returns  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
returns  string

将字符串中每个子字符串实例替换为给定字符串。

{{ product.handle | replace: '-', ' ' }}
{
"product": {
"handle": "ace-cashmere-beanie"
}
}
ace cashmere beanie

replace_first

string | replace_first: string, string
returns  string

将字符串中第一个子字符串实例替换为给定字符串。

{{ product.handle | replace_first: '-', ' ' }}
{
"product": {
"handle": "ace-cashmere-beanie"
}
}
ace cashmere-beanie

rstrip

string | rstrip
returns  string

删除字符串右侧的所有空白字符。

{% assign text = ' Some fruits create whitespace. ' %}
"{{ text }}"
"{{ text | rstrip }}"
" Some fruits create whitespace. "
" Some fruits create whitespace."

sha1

string | sha1
returns  string

将字符串转换为 SHA-1 哈希。

{{ 'Ace' | sha1 }}
fc2b5a9cb80ab0a1526f4fbc887646dd245632c2

sha256

string | sha256
returns  string

将字符串转换为 SHA-256 哈希。

{{ 'Ace' | sha256 }}
a467da82da540f56097b7224d81612bfa212e26efbf0709606144fee0b82a631

slice

string | slice
returns  string

从给定的从 0 开始的索引位置起,返回一个子字符串或一系列数组元素。

默认情况下,子字符串长度为一个字符,数组系列包含一个数组元素。不过,您可以提供第二个参数来指定字符数或数组元素的数量。

{{ 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

负索引

您可以提供一个负数索引,它将从字符串的末尾开始计数。

{{ title | slice: -8, 8 }}
{
"collection": {
"title": "All products"
}
}
products

split

string | split: string
returns  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
returns  string

删除字符串左侧和右侧的所有空白字符。

{% assign text = ' Some fruits create whitespace. ' %}
"{{ text }}"
"{{ text | strip }}"
" Some fruits create whitespace. "
"Some fruits create whitespace."

strip_html

string | strip_html
returns  string

删除字符串中的所有 HTML tag。

<!-- 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
returns  string

删除字符串中的所有换行符(行分隔符)。

<!-- 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
returns  string

将字符串截断到给定的字符数。

如果指定的字符数小于字符串的长度,则会在截断后的字符串末尾追加一个省略号(...)。该省略号会计入截断后字符串的字符数。

{{ product.title | truncate: 10 }}
{{ product.title | truncate: 20 }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
Ace Cas...
Ace Cashmere Beanie

指定自定义省略号

string | truncate: number, string

您可以提供第二个参数来指定自定义省略号。如果您不想要省略号,则可以提供一个空字符串。

{{ product.title | truncate: 10, '' }}
{{ product.title | truncate: 10, '···' }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
Ace Cashme
Ace ···

truncatebytes

string | truncatebytes: number
returns  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....

指定自定义省略号

string | truncatebytes: number, 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
returns  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...

指定自定义省略号

string | truncatewords: number, string
returns  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
returns  string

将字符串转换为全大写字符。

{{ product.title | upcase }}
{
"product": {
"title": "Ace Cashmere Beanie"
}
}
ACE CASHMERE BEANIE

url_decode

string | url_decode
returns  string

解码字符串中所有百分号编码的字符。

{{ 'https%3A%2F%2Fwww.shoplazza.com%2F' | url_decode }}
https://www.shoplazza.com/

url_encode

string | url_encode
returns  string

将字符串中所有 URL 不安全的字符转换为对应的百分号编码

📘 注意

空格会被转换为 + 字符,而非百分号编码的字符。

{{ 'https://www.shoplazza.com/' | url_encode }}
https%3A%2F%2Fwww.shoplazza.com%2F