Open olets opened 6 years ago
do you have any plan to support 'autoescape'?
Yes, support will eventually be added.
Having this feature would be really great :)
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?
Oh, I found https://github.com/twigjs/twig.js/wiki/Extending-twig.js-With-Custom-Tags - but struggling with the parse
and compile
part 😅
You can take a look at how the built-in tags are implemented for reference.
React-based Storybook
@pchr-srf consider upvoting and/or joining the related Twing discussion at https://gitlab.com/nightlycommit/twing/-/issues/561
@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? 😅
Oh, or is it related to the loader we're using? https://github.com/zimmo-be/twig-loader
@pchr-srf are you sure that the extend code is being run? Try to do some debugging and confirm that.
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!
Ah, actually I had to place it in both, if someone else stumbles upon that problem...
Twig.js doesn't currently support the
autoescape
taghttps://twig.symfony.com/doc/2.x/tags/autoescape.html