twig.js icon indicating copy to clipboard operation
twig.js copied to clipboard

allow_async twig option in expressjs doesn't work for 1.14.0

Open dfinton opened this issue 6 years ago • 3 comments

"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: https://github.com/twigjs/twig.js/issues/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 avatar Dec 26 '19 14:12 dfinton

EDIT: I replaced the typescript example with vanilla javascript

dfinton avatar Dec 26 '19 19:12 dfinton

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

JROUD avatar Apr 21 '20 06:04 JROUD

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

willrowe avatar Aug 01 '22 14:08 willrowe