twigjs / twig.js

JS implementation of the Twig Templating Language
BSD 2-Clause "Simplified" License
1.88k stars 273 forks source link

support autoescape tag #525

Open olets opened 6 years ago

olets commented 6 years ago

Twig.js doesn't currently support the autoescape tag

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

bard83 commented 2 years ago

do you have any plan to support 'autoescape'?

willrowe commented 2 years ago

Yes, support will eventually be added.

angelomelonas commented 10 months ago

Having this feature would be really great :)

pchr-srf commented 2 months ago

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 commented 2 months ago

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

willrowe commented 2 months ago

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

olets commented 2 months ago

React-based Storybook

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

pchr-srf commented 2 months ago

@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 commented 2 months ago

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

willrowe commented 2 months ago

@pchr-srf are you sure that the extend code is being run? Try to do some debugging and confirm that.

pchr-srf commented 2 months ago

I am an idiot and you are right. I put that snippet into preview.js which is run in the browser upon loading a story and not in main.js which is run when starting the whole thing. Works like a charm now, thanks for your patience!

pchr-srf commented 2 months ago

Ah, actually I had to place it in both, if someone else stumbles upon that problem...