Skip to content

Instantly share code, notes, and snippets.

@mariuswilms
Created October 25, 2013 06:41
Show Gist options
  • Save mariuswilms/7150324 to your computer and use it in GitHub Desktop.
Save mariuswilms/7150324 to your computer and use it in GitHub Desktop.
Lithium filter to inject data into elements.
<?php
use lithium\net\http\Media;
$injectDataIntoElements = function(array $elements = [], array $data = []) {
Media::applyFilter('view', function($self, $params, $chain) use ($elements, $data) {
$view = $chain->next($self, $params, $chain);
$view->applyFilter('_step', function($self, $params, $chain) use ($elements, $data) {
$whitelisted = in_array($params['params']['template'], $elements);
if ($params['step']['path'] == 'element' && $whitelisted) {
$params['data'] += $data;
}
return $chain->next($self, $params, $chain);
});
return $view;
});
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment