twig.js
twig.js copied to clipboard
I cannot use filter with function as parameters
Hi :)
I don't know if it's me or not, but I am unable to use filter function in twig. Here is my test script:
import * as Twig from 'twig';
(async () => {
Twig.extendFilter("filter", (value, args) => {
return value.filter(args);
});
const template = Twig.twig({
data: '{{ [1,2,3]|filter(test => test > 1)|join(",") }}'
});
const content = await template.renderAsync();
console.log(content);
})().catch(console.error);
I would like to extend twigjs to add map/filter filters. But twigjs seems to doesn't implement function parser :/ Here is my error staks:
Error compiling twig template undefined:
TwigException: Unable to parse '=> test > 1)|join(",")' at template position19
TypeError: Cannot read property 'length' of undefined
at Object.Twig.async.forEach (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:8937:19)
at Twig.ParseState.parse (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:1576:26)
at Twig.ParseState.parseAsync (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:8606:17)
at Twig.Template.<anonymous> (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:1750:20)
at Object.Twig.async.potentiallyAsync (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:8668:42)
at Twig.Template.render (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:1748:23)
at Twig.Template.renderAsync (C:\Users\duto\Desktop\test\node_modules\twig\twig.js:8620:17)
at C:\Users\duto\Desktop\test\index.ts:12:36
at step (C:\Users\duto\Desktop\test\index.ts:33:23)
at Object.next (C:\Users\duto\Desktop\test\index.ts:14:53)
How could we fix this ?
I found a small solution, to build the function in the extension like this:
import * as Twig from 'twig';
(async () => {
Twig.extendFilter("filter", (value, args ) => {
const f = new Function(... args);
return value.filter(f);
});
const template = Twig.twig({
data: '{{ [1,2,3]|filter("test","return test > 1")|join(",") }}'
});
const content = await template.renderAsync();
console.log(content);
})().catch(console.error);
That would be nice if the twig parser is already enable to parse directely the function. Thanks !
Hello there,
we are having the same problems in our projects with the twig filter |filter(). https://twig.symfony.com/doc/2.x/filters/filter.html
{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|filter(v => v > 38)|join(', ') }}
These lines will cause the error as described above:
NonErrorEmittedError: (Emitted value instead of an instance of Error) TwigException: Unable to parse '=> v > 38)|join(', ')' at template position2
Is this filter supported, as seen in the implementation notes?
Thank you!
Hello @jzuleger,
You shared the documentation from twig who runs on php language. On the php version, the following filter is implemented, not in the twigjs package. Here is the list of all the features who are implemented on the twigjs package: https://github.com/twigjs/twig.js/wiki/Implementation-Notes
Hey @dupasj, that's absolutly right. My lines are copied from the documentation for the twig version for php. But the php documentation is added as Docs reference in the twig.js implementation note -e.g. for filters. So my understanding would be, that if they are supported, the filters would work as they would in php. But the filter "filter" does not.
@jzuleger , twigjs doesn't support the filter filter if I'm not mistaken. If you are looking for a TypeScript/JavaScript Twig implementation that supports the filter filter, maybe you can give Twing a try:
https://www.npmjs.com/package/twing
Disclaimer: I'm the author of Twing and I rarely advertise my work here. But in this case, since it is about an unsupported feature of twig.js, I allowed myself to do so. :)
Duplicate of #652