Quantcast
Channel: Using a PHP function in Twig - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by duncan for Using a PHP function in Twig

$
0
0

What you need to do is create your own Twig function:https://twig.symfony.com/doc/3.x/advanced.html#functions

So using their examples you should be able to do something like

$function = new \Twig\TwigFunction('MobileDetect', function () {    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo    |fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i"    , $_SERVER["HTTP_USER_AGENT"]);});$twig->addFunction($filter);

Then call it like

{% if MobileDetect() %}

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>