Search code examples
shopifyliquidliquid-layout

How to display two variables in a .liquid file?


I am trying to replace

<form action="{{ form_action }}" data-productid="{{product.id}}" method="{{form_method}}" enctype="multipart/form-data" id="{{form_id}}" data-section="{{ section.id }}" class="product-form-{{ section.id }} {{form_class}}">

with

{% form 'product', product, data-productid: product.id, id: form_id, data-section: section.id, class: "product-form-{{ section.id }} {{form_class}}" %}

Everythink is great, except:

class: "product-form-{{ section.id }} {{form_class}}" 

I have no idea how to display "section.id" and "form_class" as a variable, but not as a text.

I was trying to display as in PHP ("product-form-" . section.id . " " . form_class) and as in Javascript ("product-form-" + section.id + " " + form_class) but it return an error :(


Solution

  • You have to split your logic a little.

    First set the classes in a variable and call that one instead.

    {%- capture classes -%}
      product-form-{{ section.id }} {{form_class}}
    {%- endcapture -%}
    
    {% form 
      'product', 
      product, 
      data-productid: product.id, 
      id: form_id, 
      data-section: section.id, 
      class: classes 
    %}