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

support autoescape tag

Open olets opened this issue 7 years ago • 12 comments

Twig.js doesn't currently support the autoescape tag

https://twig.symfony.com/doc/2.x/tags/autoescape.html

olets avatar Jan 05 '18 20:01 olets

do you have any plan to support 'autoescape'?

bard83 avatar Jul 05 '22 13:07 bard83

Yes, support will eventually be added.

willrowe avatar Jul 05 '22 14:07 willrowe

Having this feature would be really great :)

angelomelonas avatar Sep 01 '23 12:09 angelomelonas

In general, how should one work around missing tags?

We use twig.js to render Twig-based components in our React-based Storybook and at the moment the whole storybook crashes because we're using autoescape in one single twig template 😅 We already mock our custom twig functions and filter:

Twig.extendFilter('hex2Rgba', (value) => value);
Twig.extendFunction('generate_random_id', (value) => value);

Is there a similar way for whole tags? E.g. Twig.extendTag('autoescape', (content) => content); // treat autoescape tag as if it weren't there or a setting that ignores unsupported tags?

pchr-srf avatar May 03 '24 09:05 pchr-srf

Oh, I found https://github.com/twigjs/twig.js/wiki/Extending-twig.js-With-Custom-Tags - but struggling with the parse and compile part 😅

pchr-srf avatar May 03 '24 09:05 pchr-srf

You can take a look at how the built-in tags are implemented for reference.

willrowe avatar May 03 '24 13:05 willrowe

React-based Storybook

@pchr-srf consider upvoting and/or joining the related Twing discussion at https://gitlab.com/nightlycommit/twing/-/issues/561

olets avatar May 04 '24 01:05 olets

@willrowe I tried the following code (inspired by the unit tests):

Twig.extend(function(Twig) {
  Twig.exports.extendTag({
    type: "autoescape",
    regex: /^autoescape$/,
    next: ["endautoescape"],
    open: true,
    compile: function(token) {
      return token;
    },
    parse: function(token, context, chain) {
      return {
        chain: false,
        output: 'nothing to see here'
      };
    },
  });

  Twig.exports.extendTag({
    type: "endautoescape",
    regex: /^endautoescape$/,
    next: [ ],
    open: false
  });
});

...and placed it at the same place where I also define custom filters/functions (which work).

However, I'm still met with TwigException: Unable to parse 'autoescape'

Any ideas? 😅

pchr-srf avatar May 06 '24 07:05 pchr-srf

Oh, or is it related to the loader we're using? https://github.com/zimmo-be/twig-loader

pchr-srf avatar May 06 '24 08:05 pchr-srf