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

I cannot use filter with function as parameters #734

Closed
dupasj opened this issue Apr 5, 2020 · 5 comments
Closed

I cannot use filter with function as parameters #734

dupasj opened this issue Apr 5, 2020 · 5 comments

Comments

@dupasj
Copy link

dupasj commented Apr 5, 2020

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 !

@jzuleger
Copy link

jzuleger commented Nov 8, 2021

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!

@dupasj
Copy link
Author

dupasj commented Nov 24, 2021

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

@jzuleger
Copy link

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.

@ericmorand
Copy link
Contributor

ericmorand commented Nov 24, 2021

@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. :)

@willrowe
Copy link
Collaborator

willrowe commented Apr 3, 2023

Duplicate of #652

@willrowe willrowe closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2023
@willrowe willrowe marked this as a duplicate of #652 Apr 3, 2023
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

4 participants