skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    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 %}
    

  2. I found a better solution, just cast it:

    {% assign valueyouwant = intValue | string %}
    

    Seems to work well. 🙂

    Login or Signup to reply.
  3. For me | string wasn’t working so I’ve used this

    {% assign variant_id = variant.id | append: "" %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search