Filter-format(数据格式)

Filter format

format(数据格式)**

  • date
  • json
  • url_encode
  • url_decode
  • string
  • px
  • is_empty
  • _default

date

将时间戳转换为另一种日期格式。

// 1.数据
{
  "article": {
    "created_at": "2022-04-14 16:56:02 -0400"
  }
}


// 2.代码
{{ article.created_at | date: '%B %d, %Y' }}


// 3.输出
April 14, 2022

json

将字符串或对象转换为 JSON 格式。

  • 在 JavaScript 中使用 JSON 输出时,无需将其括在引号中,因为json过滤器已包含引号。json过滤器还会对输出中的任何引号进行转义。
{{ product | json }}

{"id":6792602320961,"title":"Crocodile tears","handle":"crocodile-tears","description":"","published_at":"2022-04-22T11:55:58-04:00","created_at":"2022-04-22T11:55:56-04:00","vendor":"Polina's Potent Potions","type":"","tags":["Salty"],"price":5600,"price_min":5600,"price_max":5600,"available":false,"price_varies":false,"compare_at_price":null,"compare_at_price_min":0,"compare_at_price_max":0,"compare_at_price_varies":false,"variants":[{"id":39888242344001,"title":"Default Title","option1":"Default Title","option2":null,"option3":null,"sku":"","requires_shipping":true,"taxable":true,"featured_image":null,"available":false,"name":"Crocodile tears","public_title":null,"options":["Default Title"],"price":5600,"weight":0,"compare_at_price":null,"inventory_management":"shopify","barcode":"","requires_selling_plan":false,"selling_plan_allocations":[],"quantity_rule":{"min":1,"max":null,"increment":1}}],"images":["\/\/polinas-potent-potions.myfecify.com\/cdn\/shop\/products\/amber-beard-oil-bottle.jpg?v=1650642958"],"featured_image":"\/\/polinas-potent-potions.myfecify.com\/cdn\/shop\/products\/amber-beard-oil-bottle.jpg?v=1650642958","options":["Title"],"media":[{"alt":null,"id":21772501975105,"position":1,"preview_image":{"aspect_ratio":1.5,"height":2974,"width":4460,"src":"\/\/polinas-potent-potions.myfecify.com\/cdn\/shop\/products\/amber-beard-oil-bottle.jpg?v=1650642958"},"aspect_ratio":1.5,"height":2974,"media_type":"image","src":"\/\/polinas-potent-potions.myfecify.com\/cdn\/shop\/products\/amber-beard-oil-bottle.jpg?v=1650642958","width":4460}],"requires_selling_plan":false,"selling_plan_groups":[],"content":""}

url_encode

将url字符串编码为 URL 安全的格式,主要用于 构建 URL 查询参数。

  • 把 URL 中不允许或有特殊意义的字符,转换成 %xx 形式的 百分号编码(percent-encoding)。
  • 保留的安全字符(不会编码):A–Z a–z 0–9 - _ . ~
  • 空格 编码为
  • 其它字符 → 转成 %XX 的形式(16 进制表示 ASCII/UTF-8 字节)。
// 1.代码:
{{ 'test@test.com' | url_encode }}

// 2.输出
test%40test.com

url_decode

解码url字符串

// 1.代码:
{{ 'test%40test.com' | url_decode }}

// 2.输出
test@test.com

string

把一个变量转换为字符串类型,返回对应的字符串。

  • int、float → 直接转换为字符串形式。
  • bool → true 转换成 "1",false 转换成 ""(空字符串)。
  • string → 原样返回。
  • null → 转换为 ""(空字符串)。
// 1.代码:
{{ 323 | string }}

// 2.输出字符串
323

px

在字符串后面加上px

  • 如果字符串结尾为:pxPX,则会处理成px
  • 如果字符串结尾不是px,则会加上px
// 1.代码:
{{ 200 | px }}

// 2.输出字符串
200px

is_empty

检查值是否为空

// 1.代码:
{{ 200 | is_empty }}
{{ '0.00' | is_empty }}
{{ 0.0 | is_empty }}
{{ '0' | is_empty }}

// 2.输出字符串
false
true
true
true

_default

为下列任意变量设置默认值:

  • 空字符串
  • false
  • null
// 1.数据
{
  "product": {
    "selected_variant": null,
    "url": "/products/health-potion"
  }
}

// 2.代码
{{ product.selected_variant.url | default: product.url }}

// 3.输出
/products/health-potion
Copyright © fecify.com 2025 all right reserved,powered by Gitbook该文件修订时间: 2025-09-08 19:14:52

results matching ""

    No results matching ""