Question

I have a plugin/function that I use for detecting if the device is mobile/tablet/desktop.

E.g:

<img src="/path/to/images/foo{if detect_device == 'desktop'}-highres{/if}.jpg"

Plugin

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.detect_device.php
 * Type:     function
 * Name:     detect_device
 * Purpose:  Simple plugin to access Mobile_Detect methods from within templates
 * Examples:
 * {if detect_device == 'tablet'} - return string either: mobile/tablet/desktop
 * {if detect_device action='isAndroidOS'} - boolean
 * {if detect_device action='isiOS'} - boolean
 * -------------------------------------------------------------
 */
function smarty_function_detect_device($params, &$smarty)
{
    // Include and instantiate the class.
    require_once 'Mobile_Detect/Mobile_Detect.php';
    $detect = new Mobile_Detect;

    // Access specific method
    if(!empty($params)){
        foreach($params as $param){
           if(method_exists($detect, $param)){
               return $detect->$param();
           }
        }
    } else {
        // Simple device type
        switch (true) {
            case $detect->isMobile() : return 'mobile'; 
            case $detect->isTablet() : return 'tablet';
            default                  : return 'desktop';
        }
    }
}
?>

Q1. How come this is never true {if detect_device == 'desktop'} but when I {detect_device|@var_dump} it returns string 'desktop' (length=7)? I think I am just getting mixed up with the type of plugin I should be using, since I am now wanting to pass a parameter to the plugin should I be using a modifier? I would prefer this syntax {if $detect_device|isiOS} but this tries to check if isiOS is a modifier instead of parameter.

Q2. Is there a way to cache the Mobile_Detect object so that this only performs the user agent calculations once?

Mobile detect script from: https://github.com/serbanghita/Mobile-Detect

Was it helpful?

Solution

As you have guessed yourself, the syntax {detect_device} is different than {if detect_device == 'desktop'}. The former calls the function "detect device", while the latter compares the string literal detect_device against a value.

If you want to use a modifier, the correct way is this (manual):

{'isiOS'|detect_device} 

Regarding caching the results, you don't have to store the Mobile_Detect object, but rather its result (the browser string). For this, you can use PHP's usual methods for persisting values: you can use the $GLOBALS variable space, or, if you are using sessions, store the result in a session (which will be different if the user signs in using a different browser). Same goes with cookies.

The session/cookie approach is better in that it will cache the result throughout the user's session (or longer), not just once per page load

OTHER TIPS

You need an $ to access assigned variables

<img src="/path/to/images/foo{if $detect_device == 'desktop'}-highres{/if}.jpg"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top