5

How do you change the datatype from a number to a string in liquid?

This could be used to create dynamic css classes within blocks. However, if it comes through as an integer it isn't recognized as a class.

{{block.id}} // Integer

I have answered my own question below along with an example.

shanestreator
  • 69
  • 1
  • 1
  • 7
  • Why do you need that? – Vladimir May 01 '20 at 06:39
  • There's already a question with an answer for that: https://stackoverflow.com/questions/27198710/convert-string-to-integer-in-shopify-liquid If that doesn't solve it you could go to the shopify community: https://community.shopify.com/ Or just ask google there are plenty of answers there. – Leo May 01 '20 at 08:59
  • Does this answer your question? [Convert string to integer in Shopify Liquid?](https://stackoverflow.com/questions/27198710/convert-string-to-integer-in-shopify-liquid) – Leo May 01 '20 at 09:00
  • 1
    @Leo, the OP is wanting to do the exact opposite of the questions you have linked to. – Luke Aug 19 '21 at 03:58

3 Answers3

6

For me | string wasn't working so I've used this

{% assign variant_id = variant.id | append: "" %}
Fabio Filippi
  • 1,732
  • 25
  • 40
1

Add quotation marks...

{% for block in section.blocks %}
  {% assign blockId = "{{block.id}}" %}
  <style>
    .{{blockId}} .text-color {
      color: {{block.settings.text_color}}
    }
  </style>

  <div class="{{blockId}}">
    <span class="text-color">I am whatever color you set me to</span>
  </div>
{% endfor %}
shanestreator
  • 69
  • 1
  • 1
  • 7
1

I found a better solution, just cast it:

{% assign valueyouwant = intValue | string %}

Seems to work well. :)