0

I'm using Twig in a standalone PHP app. So my composer only has this requirement: "twig/twig": "^3.6".

I initialize twig, and then add custom filter like so:

$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
    'cache' => 'tmp/cache',
]);

$twig->addFilter(new \Twig\TwigFilter('rot13', function ($string) {
    return str_rot13($string);
}));

In my templates, I always get Unknown Filter error whenever I try to use the filter in {{ 'MYNAME' | rot13 }}

Also tried with Function, but getting similar Unknown Function error.

Am I missing something?

This is the error on the page:

Unknown "rot13" filter. [/myfiles/templates/tester.html:26]

[vendor/twig/twig/src/ExpressionParser.php:562] Twig\ExpressionParser->getFilterNodeClass('rot13',26)
[vendor/twig/twig/src/ExpressionParser.php:547] Twig\ExpressionParser->parseFilterExpressionRaw(TwigNodeExpressionConstantExpression::__set_state([]))
[vendor/twig/twig/src/ExpressionParser.php:405] Twig\ExpressionParser->parseFilterExpression(TwigNodeExpressionConstantExpression::__set_state([]))
[vendor/twig/twig/src/ExpressionParser.php:288] Twig\ExpressionParser->parsePostfixExpression(TwigNodeExpressionConstantExpression::__set_state([]))
[vendor/twig/twig/src/ExpressionParser.php:177] Twig\ExpressionParser->parsePrimaryExpression()
[vendor/twig/twig/src/ExpressionParser.php:72] Twig\ExpressionParser->getPrimary()
[vendor/twig/twig/src/Parser.php:127] Twig\ExpressionParser->parseExpression()
[vendor/twig/twig/src/TokenParser/ForTokenParser.php:42] Twig\Parser->subparse([TwigTokenParserForTokenParser::__set_state([]),'decideForFork'])
[vendor/twig/twig/src/Parser.php:170] Twig\TokenParser\ForTokenParser->parse(TwigToken::__set_state([]))
[vendor/twig/twig/src/TokenParser/BlockTokenParser.php:47] Twig\Parser->subparse([TwigTokenParserBlockTokenParser::__set_state([]),'decideBlockEnd'],true)
[vendor/twig/twig/src/Parser.php:170] Twig\TokenParser\BlockTokenParser->parse(TwigToken::__set_state([]))
[vendor/twig/twig/src/Parser.php:83] Twig\Parser->subparse(NULL,false)
[vendor/twig/twig/src/Environment.php:491] Twig\Parser->parse(TwigTokenStream::__set_state([]))
[vendor/twig/twig/src/Environment.php:519] Twig\Environment->parse(TwigTokenStream::__set_state([]))
[vendor/twig/twig/src/Environment.php:351] Twig\Environment->compileSource(TwigSource::__set_state([]))
[vendor/twig/twig/src/Environment.php:312] Twig\Environment->loadTemplate('__TwigTemplate_9fcd4d27f601c5dddfd436979e4446b0','admin/classlist.html')
6
  • 2
    FIxed code. I had a use \Twig|twigFilter at the top. And the Error does say Unknown "rot13" filter. Commented Jun 13, 2023 at 11:21
  • How are you rendering the template? With the current posted code I'm not able to reproduce the error - demo Commented Jun 13, 2023 at 12:05
  • In the template, as I mentioned I have {{ 'STRING' | rot13 }}. To ensure my syntax is correct, I checked for {{ 'STRING' | lower }} and this correctly produces 'string'. Commented Jun 13, 2023 at 12:23
  • 1
    Yes, but how are you rendering the template? Are you using $twig->render(...)? Commented Jun 13, 2023 at 12:25
  • DOH!!! That is EXACTLY the right question :-). I had TWO places where $twig was used. and I incorrectly added the filter to the wrong one. Cl;aned up now. Commented Jun 13, 2023 at 12:33

2 Answers 2

0

User error. There were two $twig instances, filters were added to one instance and rendered using the other one. Thanks to @DarkBee for asking the right question :-)

Sign up to request clarification or add additional context in comments.

Comments

-1

Try

$twig->addFilter(new \Twig\TwigFilter('rot13', function ($string) {
    return str_rot13($string);
}));

Maybe he's just not able to resolve the class path. Works in my environment

2 Comments

Tried this one too, still Unknown "rot13" filter
If this were true then OP would get an entirely different error e.g. Fatal error: Class "xxxxx" not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.