Search code examples
stringintegershopifyliquidcoercion

Shopify - Convert Int to String in Liquid


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.


Solution

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