Added twig extension for preg_match and preg_split#2788
Added twig extension for preg_match and preg_split#2788thexmanxyz wants to merge 1 commit intogetgrav:developfrom
Conversation
| new \Twig_SimpleFunction('repeat', [$this, 'repeatFunc']), | ||
| new \Twig_SimpleFunction('regex_replace', [$this, 'regexReplace']), | ||
| new \Twig_SimpleFunction('regex_filter', [$this, 'regexFilter']), | ||
| new \Twig_SimpleFunction('preg_match', [$this, 'pregMatch']), |
There was a problem hiding this comment.
Cannot we just call the method directly:
new \Twig_SimpleFunction('preg_match', 'preg_match')There was a problem hiding this comment.
@mahagr The preg_match code is taken directly from Gantry 5 in the form it is currently in this PR (without offset and flags parameter). TBH IDK if incompatibilities might occur if we have different method signatures. Same for the the preg_split but only with the flags parameter omitted. I decided to also omit the flags parameter as it was skipped for preg_match within Gantry5 as well.
Additionally preg_match in Gantry 5 does not act the same way for return values as the default PHP method. Hence we can only update this PR for preg_split to something like this:
new \Twig_SimpleFunction('preg_split', 'preg_split')otherwise we might break existing code that relies on the Twig preg_match function. Let me know and I will push a commit with the updated code for preg_split.
There was a problem hiding this comment.
@mahagr you probably know best about the code in G5, but it seems that false return value is there for a reason in G5.
There was a problem hiding this comment.
Oh, right. The preg_match() PHP method returns true/false by default and puts the matching strings into the third parameter. It looks like @newkind added this method back in 2015, so I guess it's being used in a few themes.
If this was a filter, I would agree on the returned value, but in the function, not sure. I guess that you cannot update values inside parameters in twig, so it would make sense to remove the third parameter altogether -- it's being returned anyway.
|
Merged but renamed the methods to match the other two. |
I added a twig extension for the PHP methods
preg_splitandpreg_match(including code documentation) to cover those aspects not only within Gantry but also in Grav as well. For more information please also check this FR (gantry/gantry5#2586) and this PR (gantry/gantry5#2590).