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

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

问题描述

我刚刚阅读了这个相关的答案:

I just read this related answer:

如何将数字转换为字符串?- Shopify 设计——电子商务大学

要将字符串转换为数字,只需在变量中添加 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天全站免登陆