Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lenientIf not working as expected #313

Closed
scottgeye opened this issue Feb 18, 2021 · 7 comments
Closed

lenientIf not working as expected #313

scottgeye opened this issue Feb 18, 2021 · 7 comments

Comments

@scottgeye
Copy link

I tried setting lenientIf to true in the options, but I'm still getting an exception when an undefined variable is used in an if block. Please let me know if I'm i'm doing something wrong or if lenientIf doesn't work the way I think it does.

Thanks

const { Liquid } = require('liquidjs');
const liquid = new Liquid({ strictVariables: true, lenientIf: true, cache: true });
await liquid.parseAndRender("{% if x == 1 %} test {% endif %}");
RenderError: undefined variable: x, line:1, col:1, line:1, col:1
    at Render.renderTemplates (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1528:53)
    at renderTemplates.throw (<anonymous>)
    at /Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1691:34
    at Object.then (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1658:24)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:40)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at toPromise (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1703:28)
    at Liquid.parseAndRender (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:2963:16)
    at Formatter.process (/Users/scottgeye/Sites/fanbank-pages/Formatter.js:24:24)
    at /Users/scottgeye/Sites/fanbank-pages/server.js:90:39
From undefined variable: x, line:1, col:1
>> 1| x == 1
UndefinedVariableError: undefined variable: x, line:1, col:1
    at evalPropertyAccessToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1104:16)
    at evalToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1083:16)
    at Expression.evaluate (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1075:37)
    at evaluate.next (<anonymous>)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1681:25)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at Object.then (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1649:28)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:40)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at toPromise (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1703:28)
    at Liquid.parseAndRender (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:2963:16)
    at Formatter.process (/Users/scottgeye/Sites/fanbank-pages/Formatter.js:24:24)
    at /Users/scottgeye/Sites/fanbank-pages/server.js:90:39
From InternalUndefinedVariableError: undefined variable: x
    at /Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:534:23
    at Array.reduce (<anonymous>)
    at Context.getFromScope (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:531:22)
    at Context.get (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:526:21)
    at evalPropertyAccessToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1099:20)
    at evalToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1083:16)
    at Expression.evaluate (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1075:37)
    at evaluate.next (<anonymous>)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1681:25)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at Object.then (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1649:28)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:40)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
    at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
    at toPromise (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1703:28)
    at Liquid.parseAndRender (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:2963:16)
    at Formatter.process (/Users/scottgeye/Sites/fanbank-pages/Formatter.js:24:24)
    at /Users/scottgeye/Sites/fanbank-pages/server.js:90:39 {
  originalError: undefined variable: x, line:1, col:1
  >> 1| x == 1
  UndefinedVariableError: undefined variable: x, line:1, col:1
      at evalPropertyAccessToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1104:16)
      at evalToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1083:16)
      at Expression.evaluate (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1075:37)
      at evaluate.next (<anonymous>)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1681:25)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at Object.then (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1649:28)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:40)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at toPromise (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1703:28)
      at Liquid.parseAndRender (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:2963:16)
      at Formatter.process (/Users/scottgeye/Sites/fanbank-pages/Formatter.js:24:24)
      at /Users/scottgeye/Sites/fanbank-pages/server.js:90:39
  From InternalUndefinedVariableError: undefined variable: x
      at /Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:534:23
      at Array.reduce (<anonymous>)
      at Context.getFromScope (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:531:22)
      at Context.get (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:526:21)
      at evalPropertyAccessToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1099:20)
      at evalToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1083:16)
      at Expression.evaluate (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1075:37)
      at evaluate.next (<anonymous>)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1681:25)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at Object.then (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1649:28)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:40)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
      at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
      at toPromise (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1703:28)
      at Liquid.parseAndRender (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:2963:16)
      at Formatter.process (/Users/scottgeye/Sites/fanbank-pages/Formatter.js:24:24)
      at /Users/scottgeye/Sites/fanbank-pages/server.js:90:39 {
    originalError: InternalUndefinedVariableError: undefined variable: x
        at /Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:534:23
        at Array.reduce (<anonymous>)
        at Context.getFromScope (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:531:22)
        at Context.get (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:526:21)
        at evalPropertyAccessToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1099:20)
        at evalToken (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1083:16)
        at Expression.evaluate (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1075:37)
        at evaluate.next (<anonymous>)
        at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1681:25)
        at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
        at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
        at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
        at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
        at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
        at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
        at Object.then (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1649:28)
        at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:40)
        at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
        at reduce (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1688:16)
        at toThenable (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1676:16)
        at toPromise (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:1703:28)
        at Liquid.parseAndRender (/Users/scottgeye/Sites/fanbank-pages/node_modules/liquidjs/dist/liquid.node.cjs.js:2963:16)
        at Formatter.process (/Users/scottgeye/Sites/fanbank-pages/Formatter.js:24:24)
        at /Users/scottgeye/Sites/fanbank-pages/server.js:90:39 {
      variableName: 'x'
    },
    token: PropertyAccessToken {
      kind: 128,
      input: 'x == 1',
      begin: 0,
      end: 1,
      file: '',
      variable: [IdentifierToken],
      props: []
    }
  },
  token: TagToken {
    kind: 4,
    input: '{% if x == 1 %} test {% endif %}',
    begin: 0,
    end: 15,
    file: '',
    trimLeft: false,
    trimRight: false,
    content: 'if x == 1',
    name: 'if',
    args: 'x == 1'
  }
}
@harttle
Copy link
Owner

harttle commented Feb 18, 2021

It only works when used directly in if/unless tag or with default filter, like:

{{ notDefined | default: 'foo' }}
{{ if notDefined }}
{{ unless notDefined }}

It's purely allows you to test whether it's defined or not, so any additional computation is not allowed.

@scottgeye
Copy link
Author

Got it, thanks for the quick reply.

Our software actually extends the ability to use liquid templates to our end users, and this comes up a lot when they write their own template code. They want to check an undefined variable against some value and don't realize they have to wrap that check in another if tag to see if the variable exists or not. We keep strict variables on just as another way to help them know if they've used incorrect variable names or not.

Is there any appetite to extend the functionality of lenientIf to support my usecase? If so, I could work on a PR if there's the opportunity for it to get merged.

@emawby
Copy link

emawby commented Feb 19, 2021

I am having similar confusion about lenientIf. The following code results in an error when testing in the jsfiddle browser example. I would expect it to render properly since the name variable is followed by the default filter. Flipping strictVariables to false uses the default value as expected

HTML

<script type="text/template">
<h3>{{ name | default: "default name" | capitalize | prepend: "Welcome to "}}</h3>
</script>
<div id="result"></div>

JS

const template = document.querySelector('[type="text/template"]')
const result = document.querySelector('#result')
const engine = new liquidjs.Liquid({
    strictVariables: true,
    lenientIf: true
})

engine
    .parseAndRender(template.innerHTML, {badname: 'liquid'})
    .then(html => result.innerHTML = html)
    .catch(err => console.log(err.message))

@harttle
Copy link
Owner

harttle commented Feb 19, 2021

Is there any appetite to extend the functionality of lenientIf to support my usecase? If so, I could work on a PR if there's the opportunity for it to get merged.

@scottgeye the shortcut is to copy/paste the current if tag implementation and change it accordingly then register back into liquidjs engine object. I'm OK if you want to export more classes or utils from liquidjs. The drawback is tag implementations are easily broken by future iterations, you'll need to keep an eye on it each time upgrading liquidjs.

harttle pushed a commit that referenced this issue Feb 19, 2021
## [9.23.1](v9.23.0...v9.23.1) (2021-02-19)

### Bug Fixes

* lenientIf not working for the umd bundle, closes [#313](#313) ([2e66e8b](2e66e8b))
@harttle
Copy link
Owner

harttle commented Feb 19, 2021

🎉 This issue has been resolved in version 9.23.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@harttle
Copy link
Owner

harttle commented Feb 19, 2021

@emawby now fixed on 9.23.1, try again!

@harttle harttle added the bug label Feb 19, 2021
@emawby
Copy link

emawby commented Feb 19, 2021

Thank you for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants