Hi.
I think this can be improved by using the following in plugins.inc
/* Execute all registered plugins of a particular hook. */
function plugin_call_hook($type, $data=array()) {
global $plugin_hooks;
if (isset($plugin_hooks[$type])) {
if ($type == 'PRE_TEMPLATE') {
if (isset($plugin_hooks['POST_TEMPLATE'])) {
// We only start the buffer if there is a need
ob_start();
}
} elseif ($type == 'POST_TEMPLATE') {
// Abort if plugins trigger this hook without the PRE_TEMPLATE
if (!isset($plugin_hooks['PRE_TEMPLATE'])) {
return null;
}
$data = ob_get_contents();
ob_end_clean();
}
foreach ($plugin_hooks[$type] as $func) {
$data = call_user_func($func, $data);
}
}
return $data;
}
root_index.php.t gets changed to read ...
/* Call themed template. */
if (defined('plugins')) {
plugin_call_hook('PRE_TEMPLATE');
}
require($WWW_ROOT_DISK . fud_theme .'language.inc'); // Initialize theme's language helper functions.
require($WWW_ROOT_DISK . fud_theme . $t .'.php');
if (defined('plugins')) {
$htmlData = plugin_call_hook('POST_TEMPLATE');
if (isset($plugin_hooks['POST_TEMPLATE']) && !empty($htmlData)) {
// We only send output if process was not aborted
echo $htmlData;
}
}
The sample plugin (file: x.plugin) to capture and process output becomes:
<?php
plugin_add_hook('PRE_TEMPLATE', 'plugin_x_capture_output');
plugin_add_hook('POST_TEMPLATE', 'plugin_x_process_output');
function plugin_x_capture_output() {
//Run some code if desired.
}
function plugin_x_process_output($data) {
//$modified_data = Modify $data, add headers, footers, go mad...
return $modified_data;
}
?>
Yes it means the application is doing some hand holding but that exactly is what good applications do and the user does not have to worry about stuff as the hook behaves consistently with others.
In this case, html is passed in, user processes, returns and sees processed html displayed without having to do anything special.
PRE_TEMPLATE remains flexible as a user might want this without wanting to change the output
What do you think?
[Updated on: Sun, 17 April 2011 15:16]
Report message to a moderator