Skip to content

Instantly share code, notes, and snippets.

@johanjanssens
Created August 8, 2011 02:02
Show Gist options
  • Save johanjanssens/1131081 to your computer and use it in GitHub Desktop.
Save johanjanssens/1131081 to your computer and use it in GitHub Desktop.
Nooku Framework - Module Template Filter
<?php
/**
* @version $Id: johanjanssens $
* @category Nooku
* @package Nooku_Components
* @subpackage Default
* @copyright Copyright (C) 2007 - 2011 Johan Janssens. All rights reserved.
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link http://www.nooku.org
*/
/**
* Module Filter
*
* Filter to dynamically populate a module position from data rendered through your template.
*
* Filter will parse elements of the form <module position="[position]">[content]</module>
* and prepend or append the content to the module position.
.*
* @author Johan Janssens <johan@nooku.org>
* @category Nooku
* @package Nooku_Components
* @subpackage Default
*/
class ComDefaultTemplateFilterModule extends KTemplateFilterAbstract implements KTemplateFilterWrite
{
public function write(&$text)
{
$matches = array();
if(preg_match_all('#<module([^>]*)>(.*)</module>#siU', $text, $matches))
{
foreach($matches[0] as $key => $match)
{
//Remove placeholder
$text = str_replace($match, '', $text);
//Create attributes array
$attributes = array(
'style' => 'component',
'params' => '',
'title' => '',
'class' => '',
'prepend' => true
);
$attributes = array_merge($attributes, JUtility::parseAttributes($matches[1][$key]));
//Create module object
$module = new KObject();
$module->id = 0;
$module->content = $matches[2][$key];
$module->position = $attributes['position'];
$module->params = $attributes['params'];
$module->showtitle = !empty($attributes['title']);
$module->title = $attributes['title'];
$module->attribs = $attributes;
$module->user = 0;
$module->module = 'mod_dynamic';
KFactory::get('lib.joomla.document')->modules[$attributes['position']][] = $module;
}
}
return $this;
}
}
/**
* Modules Renderer
*
* This is a specialised modules renderer which prepends or appends the dynamically created modules
* to the list of modules before rendering them.
.*
* @author Johan Janssens <johan@nooku.org>
* @category Nooku
* @package Nooku_Components
* @subpackage Default
*/
class JDocumentRendererModules extends JDocumentRenderer
{
public function render( $position, $params = array(), $content = null )
{
//Get the modules
$modules = JModuleHelper::getModules($position);
if(isset($this->_doc->modules[$position]))
{
foreach($this->_doc->modules[$position] as $module)
{
if($module->attribs['prepend']) {
array_push($modules, $module);
} else {
array_unshift($modules, $module);
}
}
}
//Render the modules
$renderer = $this->_doc->loadRenderer('module');
$contents = '';
foreach ($modules as $module) {
$contents .= $renderer->render($module, $params, $content);
}
return $contents;
}
}
@johanjanssens
Copy link
Author

To make it work you need to put the file into /components/com_default/template/filters and you need to add the template filter to your view. You can do so using :

class ComFooViewHtml extends ComDefaultViewHtml
{
    protected function _initialize(KConfig $config)
    {
        $config->append(array(
            'template_filters' => array('module'),
        ));

        parent::_initialize($config);
     }
}

@johanjanssens
Copy link
Author

Code has been integrated in Nooku Framework for Alpha 4. For more info see this ticket : https://nooku.assembla.com/spaces/nooku-framework/tickets/150-dynamic-module-injector

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment