10000 allow_async twig option in expressjs doesn't work for 1.14.0 · Issue #706 · twigjs/twig.js · GitHub
[go: up one dir, main page]

Skip to content

allow_async twig option in expressjs doesn't work for 1.14.0 #706

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

Closed
dfinton opened this issue Dec 26, 2019 · 3 comments < 8000 span> · Fixed by #852
Closed

allow_async twig option in expressjs doesn't work for 1.14.0 #706

dfinton opened this issue Dec 26, 2019 · 3 comments · Fixed by #852

Comments

@dfinton
Copy link
dfinton commented Dec 26, 2019

"allow_async" should allow the adding of extension functions that are defined as "async". This issue (which was resolved for 1.12.0) documents this: #553

This option appears to be broken for the latest twig release (1.14.0). When I attempt to add an async function with allow_async enabled, I get the following error: throw new Twig.Error('You are using Twig.js in sync mode in combination with async extensions.');

The following code snippet does work for Twig.js versions 1.12.0 and 1.13.3. I am running node version 12.13.1 (latest LTS), but I was also able to reproduce his behavior in node v10.

test.js

const express = require('express');
const twig = require('twig');

const app = express();

const testAsync = async () => {
  const testPromise = new Promise(resolve => {
    resolve('Hello World');
  });

  const testResult = await testPromise;

  return testResult;
};

app.set('view engine', 'twig');

app.set('twig options', { 
    allow_async: true,
    strict_variables: false
});

twig.extendFunction('testAsync', testAsync);

app.get('/test', (req, res) => {
  res.render('test.html.twig');
});

app.listen(3000);

views/test.html.twig

<html>
  <head>
    <title>ASync Test</title>
  </head>
  <body>
    <h1>This is a test of an ASync function</h1>
    <p>
      {% set myVal = testAsync() %}
      {{ myVal }}
    </p>
  </body>
</html>
@dfinton
Copy link
Author
dfinton commented Dec 26, 2019

EDIT: I replaced the typescript example with vanilla javascript

@JROUD
Copy link
JROUD commented Apr 21, 2020

This option is now called allowAsync. Not yet fixed in the documentation.

@willrowe
Copy link
Collaborator
willrowe commented Aug 1, 2022

As @JROUD mentioned, it was renamed to allowAsync. Documentation will be updated accordingly for the next version.

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
0