Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to convert string to number #130

Closed
vasiliyk opened this issue Jun 9, 2012 · 7 comments
Closed

add possibility to convert string to number #130

vasiliyk opened this issue Jun 9, 2012 · 7 comments
Labels

Comments

@vasiliyk
Copy link

vasiliyk commented Jun 9, 2012

We need possibility to convert string to number, it is basic feature in programing, and there are a lot of awful workarounds on forums to overcome lack of such conversion, so we all need it urgently.

Consider following example:
assuming that there is a need to display a special message if the user has selected 20 or more items with the word 'orange' in their title:

{% assign total_orange_items = '0' %}
{% for item in cart.items %}
    {% if item.title contains "orange" %}
        {% capture temp_total %}{{ total_orange_items | plus:item.quantity }}{% endcapture %}
        {% assign total_orange_items = temp_total %}
    {% endif %}
{% endfor %}
{% if total_orange_items >= 20 %}
   

blah blah blah

{% endif %}

Another code:

{% capture difference %}{{ total_orange_items | minus:20 }}{% endcapture %}
{% unless difference contains '-' %}
   

blah blah blah

{% endunless %}
@nhocki
Copy link

nhocki commented Sep 13, 2012

Hmm, you should be able to do this. Liquid should parse numbers as numbers (here.

Also the math filters convert the strings to numbers (here)

If you still need some personalised parsing, you could easily create a filter for that.

@asumaran
Copy link

I achieved this using the 'times' filter, this fix the integer variables that turned to string after manipulating and capture
Here's the code I used.

{% assign color_counter = 0 %}
{% for variant in product.variants %}
{% assign img_index = color_counter | times: 1 %}
<img src="{{ product.images[img_index] | product_img_url: 'grande'  }}">
{% capture color_counter %}{{ color_counter | plus: 1 }}{% endcapture %}
 {% endfor %}

Anyway, would be great if a filter is available to transform string to integer or a capture_integer method.

@phoet
Copy link
Contributor

phoet commented Jun 9, 2013

@asumaran when using assign you should actually have a number, not a string. using capture will store the string that would get rendered. if you want to increment the counter, just do a {% assign color_counter = color_counter | plus: 1 %}

@asumaran
Copy link

@phoet Yes, but the color_counter variable turns into string when is captured after the plus operation. So that's why I use the times filter to ensure the img_index is numeric when the loop starts again.

Update: I just tried your method and seems works fine too, however your method is more explicitly, I'll consider it for the next time.

@phoet
Copy link
Contributor

phoet commented Jun 11, 2013

@asumaran so i think that you can close the issue

@fw42
Copy link
Contributor

fw42 commented Jun 16, 2013

Closing for now. Please re-open if still an issue.

@fw42 fw42 closed this as completed Jun 16, 2013
@SzieberthAdam
Copy link

Generally, do a | plus: 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants