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

getting current line number and template name from a filter #762

Closed
mohas opened this issue Oct 15, 2024 · 3 comments
Closed

getting current line number and template name from a filter #762

mohas opened this issue Oct 15, 2024 · 3 comments

Comments

@mohas
Copy link

mohas commented Oct 15, 2024

when using a custom filter it is very helpful to get the template name and current line that the filter is called in, is there a way to do that? so we can use it for logging purposes

@harttle
Copy link
Owner

harttle commented Oct 16, 2024

Not yet, but I think it can be easily implemented. Still need further investigation, will post back before end of this week.

github-actions bot pushed a commit that referenced this issue Oct 16, 2024
# [10.18.0](v10.17.0...v10.18.0) (2024-10-16)

### Features

* expose FilterToken to filter `this`, [#762](#762) ([d705888](d705888))
@harttle
Copy link
Owner

harttle commented Oct 16, 2024

Published 10.18.0. You can get file name from this.token.file, and line/col from this.token.getPosition() (which is calculated from token.begin and token.input). Example here:

const engine = new Liquid()
engine.registerFilter('pos', function (val: string) {
const [line, col] = this.token.getPosition()
return `[${line},${col}] ${val}`
})
const result = engine.parseAndRenderSync(`\n{{ "foo" | pos }}`)
expect(result).toEqual('\n[2,12] foo')

@mohas
Copy link
Author

mohas commented Oct 19, 2024

Great work 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