在模块、组件的子模板中添加模块位置的方法

Joomla的很多组件、模块都带有有子模板功能,例如k2组件。我们如果需要在这些子模板中添加模块位置,只需要在在模板文件中添加以下代码:

<?php 
$ycItem = &JModuleHelper::getModules( 'yc-module-position' );
foreach ($ycItem as $yc) {
$_options = array( 'style' => 'none' );
echo JModuleHelper::renderModule($yc,$_options);
}
?>

使用以上代码,只需要把yc-module-position替换成你的模块位置名称即可(当然你需要同时修改模板的templateDetails.xml文件,添加相应的模块位置名称),此方法在k2的子模板中测试通过,适用于joomla 1.5平台,joomla 2.5尚未测试

此方法出处:http://forum.joomla.org/viewtopic.php?p=975582&sid=c47b1f84a05567368ae0823efd0f8bc5#p975582

更新日期:2016年2月1日

如果使用的是Joomla 1.6、Joomla 2.5、Joomla 3这些版本,可以使用下面的方法:

$doc = JFactory::getDocument();
$renderer = $doc->loadRenderer( 'modules' );
$raw = array( 'style' => 'anycustomstyle' );
echo $renderer->render('sidebar', $raw, null);

又或者这样:

// Display position content-middle modules
$this->middlemodules = JModuleHelper::getModules('content-middle');
foreach ($this->middlemodules as $middlemodule) {
$output = JModuleHelper::renderModule($middlemodule, array('style' => 'none'));
$params = new JRegistry;
$params->loadString($middlemodule->params);
echo $output;
}