是否可以从布局文件中加载另一个主题的模板文件?所以不是像
那样的东西<reference name="head">
<block type="page/template_links" name="customvars" as="customVars" template="page/html/customvars.phtml"/>
</reference>
有人可能有:
<reference name="head">
<block type="page/template_links" name="customvars" as="customVars" template="page/html/customvars.phtml" template="otherTheme" />
</reference>
或:
<reference name="head">
<block type="page/template_links" name="customvars" as="customVars" template="/app/design/frontend/default/otherTheme/page/html/customvars.phtml"/>
</reference>
答案 0 :(得分:1)
如果查看Mage_Core_Block_Template::getTemplateFile()
,标准Magento无法做到这一点。
public function getTemplateFile()
{
$params = array('_relative'=>true);
$area = $this->getArea();
if ($area) {
$params['_area'] = $area;
}
$templateName = Mage::getDesign()->getTemplateFilename($this->getTemplate(), $params);
return $templateName;
}
您需要在代码中设置$params['_package']
和$params['_theme']
。作为起点,您可以将此信息存储在块参数中,然后将其复制到$params
数组。
但是您可以为产品和类别包指定自定义主题 - 这可能已经有用吗?
答案 1 :(得分:0)
使用事件观察器,可以在渲染指令编译之前注入自定义渲染配置。碰巧的是,这与控制器动作执行相关,考虑到请求对象的参与,这似乎是合适的。要观察的事件是controller_action_layout_load_before
,这是观察者的逻辑:
class Some_Observer
{
/**
* Observer to inject update handles via query string.
*
*
* @param Varien_Event_Observer $o
* @return void
*/
public function addQueryStringHandle(Varien_Event_Observer $o)
{
$update = $o->getLayout()->getUpdate();
/* @var $update Mage_Core_Model_Layout_Update */
$query = $o->getAction()->getRequest()->getQuery();
if (is_array($query) && count($query)) {
foreach ($query as $name => $param) {
if($this->_isValidXmlName($name)) {
$update->addHandle('QS_'.$name); // QS_ prefix should prevent collisions
}
}
}
}
/**
* Check validity of arbitrary external string as XML safe
*
* @link http://stackoverflow.com/a/2519943/833795
* @param string $name
* @return bool
*/
protected function _isValidXmlName($name)
{
try {
new DOMElement($name);
return true;
} catch(DOMException $e) {
return false;
}
}
}
可以通过将以下内容添加到 ./ index.php 的末尾来检查效果:
Zend_Debug::dump(Mage::app()->getLayout()->getUpdate()->getHandles());
应该测试此解决方案,尤其是在启用布局缓存的情况下。将某些参数列入白名单甚至可能是谨慎的。
答案 2 :(得分:0)
I'm adding this answer for whoever want's to achieve this.
To load a template from a different theme, you just have to keep the local.xml the same (the usual configuration), and then within the controller, add this code specifiying the package and theme to use to find the template file in:
Mage::getSingleton('core/design_package')
->setPackageName('package')
->setTheme('theme');
This code was taken from how Magento applies a different theme for category pages when using "Custom Design". its tested and working on one of my live sites.