Note that there are some explanatory texts on larger screens.

plurals
  1. POSmarty cache object instance inside plugin
    primarykey
    data
    text
    <p>I have a plugin/function that I use for detecting if the device is mobile/tablet/desktop.</p> <p>E.g:</p> <p><code>&lt;img src="/path/to/images/foo{if detect_device == 'desktop'}-highres{/if}.jpg"</code></p> <p>Plugin</p> <pre><code>&lt;?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, &amp;$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-&gt;$param(); } } } else { // Simple device type switch (true) { case $detect-&gt;isMobile() : return 'mobile'; case $detect-&gt;isTablet() : return 'tablet'; default : return 'desktop'; } } } ?&gt; </code></pre> <p>Q1. How come this is never true <code>{if detect_device == 'desktop'}</code> but when I <code>{detect_device|@var_dump}</code> 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 <code>{if $detect_device|isiOS}</code> but this tries to check if isiOS is a modifier instead of parameter.</p> <p>Q2. Is there a way to cache the Mobile_Detect object so that this only performs the user agent calculations once?</p> <p>Mobile detect script from: <a href="https://github.com/serbanghita/Mobile-Detect" rel="nofollow">https://github.com/serbanghita/Mobile-Detect</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload