liquid
liquid copied to clipboard
Add support for named filter arguments
System information
- Version:
github.com/osteele/liquid v1.2.4 - Go:
go version go1.13.7 darwin/amd64
Problem
I am receiving syntax errors while trying to parse templates that are meant for shopify. I have filters that look like this
{{image | img_url: '580x', scale: 2}}
{{ order.created_at | date: format: 'date' }}
{{ 'customer.order.title' | t: name: order.name }}
I have tried to just define the filters:
cfg.AddFilter("t", func(value interface{}) interface{} {
return value
})
cfg.AddFilter("date", func(value interface{}) interface{} {
return value
})
And I get a generic Syntax error that does not define what the error is.
Steps to reproduce the behavior
Here is a test that will reproduce this:
package lint
import (
"testing"
"github.com/osteele/liquid"
"github.com/stretchr/testify/assert"
)
func TestPathToProject(t *testing.T) {
engine := liquid.NewEngine()
template := `{{ 'customer.order.title' | t: name: order.name }}`
bindings := map[string]interface{}{}
engine.RegisterFilter("t", func(value interface{}) interface{} {
return value
})
_, err := engine.ParseAndRenderString(template, bindings)
assert.Nil(t, err)
}
Possible Solution
I have a hunch that you do not support named arguments to filters.
Follow-up question, it does not seem like your for loop supports else and I cannot seem to override the standard for loop with my own
That is correct, this library does not support named arguments to filters. (In fact, I did not know that Shopify did.)
I would be interested in having a fix for this, but I don't promise to find time to write one.
Here is documentation for Shopify's image_url parameters: https://shopify.dev/api/liquid/filters/url-filters#other-image-url-parameters
Here is a test case for filter parameters in the Ruby implementation's parser: https://github.com/Shopify/liquid/blob/9b8e3d437e1ac461fc32bd896e7f35dfa8b94a2a/test/unit/parser_unit_test.rb#L73 (from this bug report against the C implementation — which I just learned exists)
I appear to have come across these issues too.