How to retrieve all Variables from a Twig Template?

前端 未结 14 1517
抹茶落季
抹茶落季 2020-12-08 00:24

Is it possible to retrieve all variables inside a Twig template with PHP?

Example someTemplate.twig.php:

Hello {{ name }}, 
your new email is {{ ema         


        
14条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 01:00

    If you need all Twig elements inside of a text, just use:

    preg_match_all('/\{\%\s*(.*)\s*\%\}|\{\{(?!%)\s*((?:[^\s])*)\s*(?

    I had a issue where the WSIWYG editor placed HTML tags inside of Twig variables. I filter them with:

    public function cleanHTML($text)
    {
        preg_match_all('/\{\%\s*(.*)\s*\%\}|\{\{(?!%)\s*((?:[^\s])*)\s*(?

    UPDATE

    Use this expression to find all {{ }} and {% %}

    preg_match_all('/\{\%\s*([^\%\}]*)\s*\%\}|\{\{\s*([^\}\}]*)\s*\}\}/i', $text, $matches);
    

提交回复
热议问题