使用 Smarty PHP 模板构建移动网站

发布于 2024-10-08 11:11:29 字数 189 浏览 0 评论 0原文

我有一个使用 smarty 的定制应用程序。我需要我的应用程序支持移动设备(iphone、driods 等...)我知道一般的网络看起来不错,而且在很多情况下我可以使用 CSS hacks,但是 Smarty 中有没有一种方法可以检测浏览器是什么被使用然后根据该请求提供模板?

一种类型的模板用于: - 台式机和笔记本电脑 另一个用于 -智能手机

I have a custom built app that uses smarty. I need my app to support mobile devices (iphones, driods etc...) I know that the general web looks ok, and that also in many cases I can use CSS hacks, but is there a way in Smarty to detect what browser is being used then serve up templates based on that request?

One type of template for:
-Desktops and Laptops
Another for
-Smart Phones

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-10-15 11:11:29

您可以在 Smarty 中使用纯 PHP:

{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}

或者,您可以创建一个 Smarty 插件。我记得它是这样的:

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

查找有关编写插件的更多信息在 Smarty 网站上。

You can use plain 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 recall 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 on the Smarty site.

一抹苦笑 2024-10-15 11:11:29

更好的方法是根据用户代理/设备加载模板集,例如:

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

或者像这样更动态:

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

这里的 $device 可以有多精细,取决于应用程序的需求。

way more better, would be to load the set of templates based on the user agent / device, so for example:

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

or more dynamic like this:

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

how granular $device here can be, depends on the application needs.

柠北森屋 2024-10-15 11:11:29

普通 PHP 在 Smarty 3 中已被弃用,不要使用它!

您可以使用库,例如​​ Mobile Detect检测移动浏览器

plugins 目录中创建一个插件,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);
}
}

然后在模板文件中使用它:

{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}

Plain PHP is deprecated in Smarty 3, don't use it!

You can use a library, such as Mobile Detect or Detect Mobile Browsers.

Create a plugin in 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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文