Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

type conversion - Convert string to integer in Shopify Liquid?

I just read this related answer:

How can I convert a number to a string? - Shopify Design — Ecommerce University

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

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

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

question from:https://stackoverflow.com/questions/27198710/convert-string-to-integer-in-shopify-liquid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...