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

How to rewrite a registerTag from v9 to be v10 compatible? #570

Closed
HenrikPoulsen opened this issue Dec 15, 2022 · 3 comments
Closed

How to rewrite a registerTag from v9 to be v10 compatible? #570

HenrikPoulsen opened this issue Dec 15, 2022 · 3 comments

Comments

@HenrikPoulsen
Copy link

in v9 I have the followin registerTag:

engine.registerTag('metadata_file', {
  parse(tagToken: TagToken, remainTokens: TopLevelToken[]) {
    this.str = tagToken.args;
  },
  async render(ctx: Context) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    const content = await readLocalFile(this.str);
    return this.liquid.parseAndRender(content.toString(), ctx);
  },
});

Upgrading to v10 causes this to fail since str has been removed.
In the docs here it says to use the following:

engine.registerTag('upper', {
    parse: function(tagToken: TagToken, remainTokens: TopLevelToken[]) {
        this.value = new Value(token.args, liquid)
    },
    render: function*(ctx: Context) {
        const str = yield this.value.value(ctx); // 'alice'
        return str.toUpperCase() // 'ALICE'
    }
});

However it complains that value does not exist on Tag, that token cannot be found as well as that liquid cannot be found.
So presumably the samples are incorrect. I am trying to update to liquidjs 10.3.2.

harttle added a commit that referenced this issue Dec 18, 2022
@harttle
Copy link
Owner

harttle commented Dec 18, 2022

Yeah the type is broken, previously it sets this to [key: string]: any, now applying this type definition back.

Another problem is parseAndRender doesn't support a Context in recent versions. I'm also adding this support back to allow plain object OR Context as a scope.

github-actions bot pushed a commit that referenced this issue Dec 18, 2022
## [10.3.3](v10.3.2...v10.3.3) (2022-12-18)

### Bug Fixes

* type compatible with v9 tag definition, support `Context` as scope in various render APIs, [#570](#570) ([fb6a9f8](fb6a9f8))
@harttle
Copy link
Owner

harttle commented Dec 18, 2022

I added this case and it now works for me, please check v10.3.3

liquidjs/test/e2e/issues.ts

Lines 349 to 364 in fb6a9f8

it('#570 tag registration compatible to v9', async () => {
const liquid = new Liquid()
liquid.registerTag('metadata_file', {
parse (tagToken: TagToken, remainTokens: TopLevelToken[]) {
this.str = tagToken.args
},
async render (ctx: Context) {
const content = await Promise.resolve(`{{${this.str}}}`)
return this.liquid.parseAndRender(content.toString(), ctx)
}
})
const tpl = '{% metadata_file foo %}'
const ctx = { foo: 'FOO' }
const html = await liquid.parseAndRender(tpl, ctx)
expect(html).to.equal('FOO')
})

@HenrikPoulsen
Copy link
Author

I have tried the new version and it works perfectly with my existing code. Thanks!

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

No branches or pull requests

2 participants