我目前正在模块中使用MobileDetect类用于某些事情(例如向主体添加“移动”类以进行样式化等)。我一直在尝试考虑为移动设备加载不同模板的方法,例如列表视图或产品页面 via xml 。
我认为可以做到的一种方法是使用帮助器来设置模板。例如,对于列表视图:
<catalog_category_view>
<reference name="root">
<action method="setTemplate">
<template helper="mobiledetect/switchTemplate" />
</action>
</reference>
</catalog_category_view>
...然后在我的帮手中:
public function switchTemplate()
{
$detect = new Mobile_Detect();
if($detect->isMobile()){
$template = 'catalog/category/mobile_view.phtml';
}
else{
$template = 'catalog/category/view.phtml';
};
return $template;
}
这很好用,但是如果我想为其他部分做类似的事情,例如产品详细信息页面,我需要明确说明帮助器中的每个模板。
任何人都可以想到它可以用来为默认模板或其他东西添加前缀吗?例如,对于列表视图,默认为 catalog / category / view.phtml - 因此,如果在移动设备上查看,您可以将 mobile _ 添加到标准模板之前,从而获得目录/类别/ mobile_view.phtml ?
或者其他任何关于获得类似成就的想法?我希望能够从local.xml进行设置,而不是每次我想添加新的移动模板时都要编辑帮助器。
我还考虑过一个观察者添加一个“移动”句柄,用于在xml布局中使用,但如果可行的话,则不是100%。
P.S。我知道我可以在模板文件中使用帮助程序为移动设备提供不同的内容(我已经这样做了)但我对通过local.xml执行此操作的可能性感兴趣。
另外,请注意,我不想使用标准设计例外的原因是我可能希望拥有桌面/平板电脑/移动版本的文件。提前致谢!
答案:根据Simon的回答,我做了以下https://stackoverflow.com/a/22579344/161056
答案 0 :(得分:1)
你知道magento默认设计例外吗?它们允许使用完全分离的包或主题。默认情况下,主题是基于用户代理选择的。你可以通过移动检测来扩展它。主题在此处确定:Mage_Core_Model_Design_Package::getTheme
// set exception value for theme, if defined in config
$customThemeType = $this->_checkUserAgentAgainstRegexps("design/theme/{$type}_ua_regexp");
if ($customThemeType) {
$this->_theme[$type] = $customThemeType;
}
这将是一个很好的开始。
答案 1 :(得分:1)
所以,考虑到Simon的建议,我扩展了 Mage_Core_Model_Design_Package 并使用了以下内容:
const MOBILE_THEME = 'mobile';
const TABLET_THEME = 'tablet';
public function getTheme($type)
{
$detect = new Mobile_Detect();
if($detect->isTablet()){
$this->_theme[$type] = self::TABLET_THEME;
}
elseif($detect->isMobile()){
$this->_theme[$type] = self::MOBILE_THEME;
};
return $this->_theme[$type];
}
所以基本上,如果检测到手机或平板电脑,它实质上是用“移动”更新系统 - >配置 - >设计 - >主题 - >主题 - >默认或“平板电脑”。这意味着我可以拥有以下主题结构:
app
- design
- frontend
- default
- default
- mobile
- tablet
...只需将模板覆盖放在这些文件夹中就意味着它们可以提供给正确的设备。
似乎工作正常 - 如果我遇到任何问题,我会回复。