harttle / liquidjs

A simple, expressive, safe and Shopify compatible template engine in pure JavaScript.
https://liquidjs.com
MIT License
1.52k stars 238 forks source link

How to update a variable which declare by "assign" #608

Closed zls3201 closed 1 year ago

zls3201 commented 1 year ago

for example, i want to set pos= pos + field.length

data

{
   "fields":[
      {
         "name":"number_field",
         "length":1
      },
      {
         "name":"text_field",
         "length":2
      },
      {
         "name":"validate",
         "length":3
      }
   ]
}

template

{% assign pos = 0 -%}
{% for field in fields %}

  {{ field.name }}  length: {{ field.length }}  [{{ pos }}, {{ field.length | plus: pos | plus:  -1   }}]
  {% # pos= pos + field.length %}
   .....
{% endfor %}
harttle commented 1 year ago

Try this:

  {% assign pos = pos | plus: field.length %}