在Shopify Liquid中将字符串转换为整数? [英] Convert string to integer in Shopify Liquid?

查看:230
本文介绍了在Shopify Liquid中将字符串转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了以下相关答案:

I just read this related answer:

要将字符串转换为数字,只需在变量中添加0:

To convert a string to a number just add 0 to the variable:

{%分配变量名=变量名|加0%}

{% assign variablename = variablename | plus:0 %}

不是超级优雅,但可以!

Not super elegant but it works!

不管是不是优雅,那里给出的答案对我来说都行不通.什么是正确的方法?

Inelegant or not, the answer given there isn't working for me. What's the right way to do this?

Liquid文档是否真的缺少这样的基本答案,还是我只是找不到合适的位置?

Are the Liquid docs really missing such basic answers or am I just not finding the right place to look?

推荐答案

在数学过滤器中使用assign是正确的. 在GitHub上查看此线程

Using assign with a math filter is correct. See this thread on GitHub, and this blog post.

通过{% capture %}创建的变量是字符串.使用assign时,以下两个选项中的任何一个都应为您提供一个数字:

Variables created through {% capture %} are strings. When using assign, either of these options should give you a number:

{% assign var1 = var1 | plus: 0 %}
{% assign var2 = var2 | times: 1 %}

如果这对您不起作用,您可以张贴相关代码吗?

If this doesn't work for you, can you post the relevant code?

这篇关于在Shopify Liquid中将字符串转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆