php - Parse/search for a certain function in twig templates -


In the latest version of

, the customer function is implemented as the old method in the form of Twig_SimpleFunction s I have a custom translation function to handle message formats using

  {{trans ('key'}}} . 

I am creating PHP scripts to go through all my templates to retrieve those things. I do not want to render templates in this script because I have a template Not all variables to pass. Functions are not called when the functions are loaded using load () (which is important for display), so I'll need a laser to parse my template. Unfortunately, there is really a lot of information about this, so it is hard to say where to start.

Alternatively, if there is one then { {Trans ('key')} ?

To add a customer laser to parse such tasks, the better / simpler way to get it, I would love to know definitely!

The solution is quite simple. We have to catch all the templates, load each template and then run the parser to generate AST. Then we walk through the AST which we want to choose the things we want. There is no need to compile the template.

How can this be done:

  Public function process ($ node) {// Process nodes which are function expressions ($ Node instanceof \ Twig_Node_Expression_Function) {/ Check the name of the function / (if the $ node-> getAttribute ('name') == 'trans') {// logic foreach ($ node- & gt; getNode ('arguments') as the $ argument) { $ Key = eval ('return'. $ This- & gt; twig- & gt; compile ($ argument). ';'); If (! In_array ($ key, $ translationKeys)) {$ this- & gt; Trans [] = $ key; } // In my break of implementation I only needed the first argument; }}} // consecutive loop (if $ $ child instof \ Twig_Node) {process ($ child) through AST foreign currency ($ child as $ node); }}}  

Tip: If you are not loading templates from external files, then there are no strings, then you get getContent () to use:

  $ parseed = $ twig- & gt; Parse ($ twig- & gt; tokenize ($ loader-> getSource ($ template));  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -