Creating Mobile Websites Using Smarty PHP Templates

I have a custom application that uses smarty. I need my application to support mobile devices (iphones, driods, etc.). I know that the general web interface looks fine, and that in many cases I can also use CSS hacks, but is there a way in Smarty to find that browser and then serve templates based on this request?

One type of template for: -Dizavah and laptops Another for -Smart Phones

+4
source share
3 answers

You can use simple PHP in Smarty:

{php} if (preg_match('/iphone/i', $_SERVER['HTTP_USER_AGENT'])) { // iphone } else if (preg_match('/android/i', $_SERVER['HTTP_USER_AGENT'])) { // android } // and so on... {/php} 

Alternatively, you can create a Smarty plugin. I remember that it is something like this:

 function smarty_function_useragent_match($params, $template) { return preg_match($params['pattern'], $_SERVER['HTTP_USER_AGENT']); } 

Look for more information on Writing Plugins at Smarty.

+1
source

it would be better to load a set of templates based on a user agent / device, for example, for example:

 $template_dir = '/path/to/default/templates/'; if($mobile_device) { $template_dir = '/path/to/default/templates/'; } 

or more dynamically:

 $template_dir = '/path/to/templates/' . $device; 

how a granular $ device here maybe depends on the needs of the application.

0
source

Normal PHP is deprecated in Smarty 3, do not use it!

You can use a library such as Mobile Discovery or Mobile Discovery Browsers .

Create a plugin in the plugins directory, function.detect_mobile.php :

 function smarty_function_detect_mobile($params, &$smarty) { require_once './libraries/Mobile_Detect.php'; $detect=new Mobile_Detect; $smarty->assign('is_mobile', false); if($detect->isMobile()) { $smarty->assign('is_mobile', true); } } 

Then use it in the template file:

 {detect_mobile} {if $is_mobile} <link rel="stylesheet" href="css/styles-mobile.css" type="text/css" /> {else} <link rel="stylesheet" href="css/styles.css" type="text/css" /> {/if} 
0
source

Source: https://habr.com/ru/post/1332638/


All Articles