UPDATE: 1
I am using this method with DarkBee's help
$detect = new Mobile_Detect;
class Project_Twig_Extensions extends Twig_Extension{
public function getFunctions(){
return array(new Twig_SimpleFunction('detectDevice', function() use($detect){
if($detect->isMobile()){
return true;
} else {
return false;
}
}));
}
}
I am calling it in Twig using
{% if detectDevice %}
There is no value being passes and the condition isn't being met so checking the documentation I wrote another method just in case https://twig.symfony.com/doc/2.x/advanced.html#functions
$twig = new Twig_environment($loader);
$function = new Twig_Function('detectDevice', function() use($detect){
if($detect->isMobile()){
return true;
} else {
return false;
}
});
$twig->addFunction($function);
Which also passes no values, my 500 Server Errors have happily stopped thanks to DarkBee but i still can't seem to pass this properly into Twig.
Original Question
I am having immense trouble installing the brilliant Mobile_Detect plugin into my Opencart 3.0 project (https://github.com/serbanghita/Mobile-Detect) I have inserted it and successfully checked if the device is desktop or mobile within php.
I've learned in order to pass this php check into twig, i can either add it to global variables
$twig = new Twig_Environment($loader);
$mobTwig->addGlobal('isMobile', true);
or create a public function that extends twig
require_once(DIR_SYSTEM . 'detect/mobiledetectlib/Mobile_Detect.php');
$detect = new Mobile_Detect;
$detectFunction = new function(){
if($detect->isMobile()){
return true;
} else {
return false;
}
}
class Project_Twig_Extensions extends Twig_Extension{
public function getFunctions(){
return array(new Twig_SimpleFunction('detectDevice', '$detectFunction'));
}
}
Both options return a 500 Internal Server Error and immediately stop the page loading. For the last few days i cannot figure out why. No amount of googling has helped me. Does anyone have any insight why this might be happening?