Twig_Error_Syntax for “Unknown filter” with a Twig filter in Timber

前端 未结 1 1499

This has got to be simple, but I can\'t see what\'s wrong. I\'m using the simple filter example at https://twig.symfony.com/doc/1.x/advanced.html#filters with Twig 1.34 in T

1条回答
  •  被撕碎了的回忆
    2021-01-19 14:31

    According to documentation here (title: Adding to Twig)

    it should be done like this (in functions.php):

    add_filter('timber/twig', function($twig) {
       $twig->addExtension(new Twig_Extension_StringLoader());
    
       // add Your filters here
       $twig->addFilter(
         new Twig_SimpleFilter(
           'rot13', 
           function($string) {
             return str_rot13($string);
           }
         )
       );
       // or simply: 
       // $twig->addFilter(new Twig_SimpleFilter('rot13', 'str_rot13'));
    
       $twig->addFilter(
         new Twig_SimpleFilter(
           'hello', 
           function($name) {
             return 'Hello, '.$name;
           }
         )
       );
    
       return $twig;
    });
    

    0 讨论(0)
提交回复
热议问题