其它方法 - 友好速搭 -- 使用手册

友好速搭

模板引擎
简介 Liquid 基础语法 Liquid 内置方法 友好速搭主题对象

其它方法

本节介绍格式化输出、类型转换和特殊样式输出的方法,包括以下:


money

友好速搭中,输出的金额单位默认是,可以使用此方法,输出以为单位的金额:

输入:

{{ 145 | money }}

输出:

1.45

money_with_decimal

指定输出金额的精度,默认为2,输出为字符串,小数点后默认补0。

输入:

{{ 145 | money_with_decimal }}
{{ 145 | money_with_decimal: 3 }}

输出:

1.45
1.450

currency

输出带币种符号的金额:

输入:

{{ 145 | currency }}

输出:

¥145

weight

友好速搭中,输出的重量单位默认是,可以使用此方法,输出以千克为单位的重量:

输入:

<!-- line_item.weight 是2000克 -->
{{ line_item.weight | weight }}千克

输出:

2千克

weight_with_unit

格式化输出重量,默认显示kg

输入:

{{ 145 | weight_with_unit }}
{{ 145 | weight_with_unit: '' }}

输出:

1.45kg
1.45千克

volume

友好速搭中,输出的体积单位默认是立方分米,可以使用此方法,输出以立方米为单位的体积:

输入:

<!-- line_item.volume 是6000立方分米 -->
{{ line_item.volume | volume}}立方米

输出:

6立方米

volume_with_unit

格式化输出体积,默认显示

输入:

{{ 145 | volume_with_unit }}
{{ 145 | volume_with_unit:  }}

输出:

1.45m³
1.45立方米

date

格式化输出日期时间。

输入:

{{ current_datetime | date: "%Y-%m-%d" }}

输出:

2014-07-08

格式化可使用的参数如下:

参数 描述
%d 输出日期中的天,自动补0,例如01,02等: {{ current_datetime | date: "%d" }} <!-- 08 -->
%-d 输出日期中的天,不补0,例如1,2等: {{ current_datetime | date: "%-d" }} <!-- 8 -->
%F 以ISO 8601标准格式化日期(yyyy-mm-dd): {{ current_datetime | date: "%F" }} <!-- 2014-07-08 -->
%H 以24小时制返回小时(00 - 23): {{ current_datetime | date: "%H" }} <!-- 22 -->
%I 以12小时制返回小时(1 - 12): {{ current_datetime | date: "%I" }} <!-- 10 -->
%j 日期所在全年的天数(001 - 366): {{ current_datetime | date: "%j" }} <!-- 189 -->
%k 以24小时制返回小时(1 - 24): {{ current_datetime | date: "%k" }} <!-- 22 -->
%m 返回月份(01 - 12): {{ current_datetime | date: "%m" }} <!-- 07 -->
%M 返回分钟(00 - 59): {{ current_datetime | date: "%M" }} <!-- 52 -->
%R 返回24小时制的时间(%H:%M): {{ current_datetime | date: "%R" }} <!-- 22:52 -->
%T 返回24小时制的时间(%H:%M:%S): {{ current_datetime | date: "%T" }} <!-- 22:52:33 -->
%U 返回当前日期是第几周,从第一周的第一个周一开始算: {{ current_datetime | date: "%U" }} <!-- 27 -->
%W 返回当前日期是第几周,从第一周的第一个周日开始算: {{ current_datetime | date: "%W" }} <!-- 27 -->
%w 返回当前日期周几: {{ current_datetime | date: "%w" }} <!-- 2 -->
%X 输出时间(hh:mm:ss): {{ current_datetime | date: "%X" }} <!-- 22:52:33 -->
%y 输出排除世纪的年份(00 - 99): {{ current_datetime | date: "%y" }} <!-- 14 -->
%Y 输出完整年份: {{ current_datetime | date: "%Y" }} <!-- 2014 -->
%Z 输出时区信息: {{ current_datetime | date: "%Z" }} <!-- CST -->

default

设置对象的默认值,在对象为空或不存在时显示:

输入:

<!-- customer.name 为空 -->
你好,{{ customer.name | default: "" }}

输入:

你好,顾客

default_pagination

pagination对象生成默认的分页样式。

输入:

{{ pagination | default_pagination }}

输出:

<span class="page current">1</span>
<span class="page"><a href="/all_products?size=30&page=2" title="">2</a></span>
<span class="page"><a href="/all_products?size=30&page=3" title="">3</a></span>
<span class="deco">&hellip;</span>
<span class="page"><a href="/all_products?size=30&page=17" title="">17</a></span>
<span class="next"><a href="/all_products?size=30&page=2" title="">下一页 &raquo;</a></span>

content_html

用于productpage对象获取富文本内容。传入富文本对象的ID,获取富文本内容:

输入:

{% if product.content %}
  {{ product.content | content_html }}
{% endif %}

输出:

<p><strong>这是商品描述</strong></p>

json

将指定对象,转换成json格式字符串,通常用于将对象传入javascript中使用。

输入:

$yhsd.component.variantSelector.init({{ product | json }});

输出:

$yhsd.component.variantSelector.init({"name":"商品样例","id":"393","variants":[{"id":64,"stock":3,"price":258.0,"compare_price":0.0,"weight":0.0,"volume":0.0,"barcode":"","stock_type":"B","options":[{"option_id":6,"option_value":""}],"sale":0}],"created_at":"2014-04-19 09:25:07 +0800","vendor":{"id":27,"name":"品牌"},"content":"535224d32567a5183000000b","feature_image":"535225762567a51836000005","sale":"0","page_url":"/products/yulu2","options":[{"id":6,"name":"颜色"}],"page_title":"商品样例","page_desc":"商品样例","handle":"yulu2","images":[{"image_id":"535225762567a51836000005","alt":"","position":0},{"image_id":"5352256d2567a5183300000a","alt":"","position":1}],"types":[{"id":30,"name":"分类"}]} );
以上内容仍未解决您的问题? 联系在线客服
免费领取15天试用
立即注册
联系客服
微信咨询
微信二维码

领取免费试用资格

姓名 *

电话 *

公司名称

所在地区

意向产品

提交

提交成功

你好, XXX女士/先生 ,你的需求已提交成功,后续会有专门的客户经理与你电话联系。谢谢!