phptemplate.engine
<?php
function phptemplate_init($template) {
$file = dirname($template->filename) . '/template.php';
if (file_exists($file)) {
include_once "./$file";
}
}
function phptemplate_templates($directory = 'themes') {
return system_listing('^page\.tpl\.php$', $directory, 'filename');
}
function phptemplate_regions() {
return array(
'left' => t('left sidebar'),
'right' => t('right sidebar'),
'content' => t('content'),
'header' => t('header'),
'footer' => t('footer')
);
}
function _phptemplate_callback($hook, $variables = array(), $file = NULL) {
$variables = array_merge($variables, _phptemplate_default_variables($hook, $variables));
if (function_exists('_phptemplate_variables')) {
$variables = array_merge($variables, _phptemplate_variables($hook, $variables));
}
if (isset($variables['template_file'])) {
$file = $variables['template_file'];
}
if (function_exists('_phptemplate_' . $hook)) {
return call_user_func('_phptemplate_' . $hook, $variables, $file);
}
elseif (function_exists('_phptemplate_default')) {
return call_user_func('_phptemplate_default', $hook, $variables, $file);
}
}
function _phptemplate_default_variables($hook, $variables) {
global $theme, $sidebar_indicator;
static $count = array();
$count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
$variables['id'] = $count[$hook]++;
if ($hook == 'block') {
$count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1;
$variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even';
$variables['block_id'] = $count['block_counter'][$sidebar_indicator]++;
}
elseif ($hook == 'page') {
$regions = system_region_list($theme);
foreach (array_keys($regions) as $region) {
if (!in_array($region, array('left', 'right', 'footer'))) {
isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region);
}
}
}
$variables['directory'] = path_to_theme();
$variables['is_front'] = drupal_is_front_page();
return $variables;
}
function phptemplate_features() {
return array(
'toggle_logo',
'toggle_comment_user_picture',
'toggle_favicon',
'toggle_mission',
'toggle_name',
'toggle_node_user_picture',
'toggle_search',
'toggle_slogan'
);
}
function phptemplate_page($content) {
if (drupal_is_front_page()) {
$mission = filter_xss_admin(theme_get_setting('mission'));
}
if (theme_get_setting('toggle_favicon') && ($favicon_url = check_url(theme_get_setting('favicon')))) {
drupal_set_html_head('<link rel="shortcut icon" href="'. $favicon_url .'" type="image/x-icon" />');
}
$layout = 'none';
global $sidebar_indicator;
$sidebar_indicator = 'left';
$sidebar_left = theme('blocks', 'left');
if ($sidebar_left != '') {
$layout = 'left';
}
$sidebar_indicator = 'right';
$sidebar_right = theme('blocks', 'right');
if ($sidebar_right != '') {
$layout = ($layout == 'left') ? 'both' : 'right';
}
$sidebar_indicator = NULL;
if (drupal_get_title()) {
$head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'drupal'));
}
else {
$head_title = array(variable_get('site_name', 'drupal'));
if (variable_get('site_slogan', '')) {
$head_title[] = variable_get('site_slogan', '');
}
}
$variables = array(
'base_path' => base_path(),
'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()),
'closure' => theme('closure'),
'content' => '<!-- begin content -->' . $content . '<!-- end content -->',
'footer_message' => filter_xss_admin(variable_get('site_footer', FALSE)) . "\n" . theme('blocks', 'footer'),
'head' => drupal_get_html_head(),
'head_title' => implode(' | ', $head_title),
'help' => theme('help'),
'language' => $GLOBALS['locale'],
'layout' => $layout,
'logo' => theme_get_setting('logo'),
'messages' => theme('status_messages'),
'mission' => isset($mission) ? $mission : '',
'primary_links' => menu_primary_links(),
'search_box' => (theme_get_setting('toggle_search') ? search_box() : ''),
'secondary_links' => menu_secondary_links(),
'sidebar_left' => $sidebar_left,
'sidebar_right' => $sidebar_right,
'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''),
'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''),
'styles' => theme_get_styles(),
'tabs' => theme('menu_local_tasks'),
'title' => drupal_get_title()
);
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$variables['node'] = node_load(arg(1));
}
return _phptemplate_callback('page', $variables);
}
function phptemplate_node($node, $teaser = 0, $page = 0) {
if (module_exist('taxonomy')) {
$taxonomy = taxonomy_link('taxonomy terms', $node);
}
else {
$taxonomy = array();
}
$variables = array(
'content' => ($teaser && $node->teaser) ? $node->teaser : $node->body,
'date' => format_date($node->created),
'links' => $node->links ? theme('links', $node->links) : '',
'name' => theme('username', $node),
'node' => $node, 'node_url' => url('node/'. $node->nid),
'page' => $page,
'taxonomy' => $taxonomy,
'teaser' => $teaser,
'terms' => theme('links', $taxonomy),
'title' => check_plain($node->title)
);
$variables = array_merge((array)$node, $variables);
if (theme_get_setting('toggle_node_info_' . $node->type)) {
$variables['submitted'] = t('Submitted by %a on %b.', array('%a' => theme('username', $node), '%b' => format_date($node->created)));
$variables['picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '';
}
else {
$variables['submitted'] = '';
$variables['picture'] = '';
}
return _phptemplate_callback('node', $variables, 'node-' . $node->type);
}
function phptemplate_comment($comment, $links = 0) {
return _phptemplate_callback('comment', array(
'author' => theme('username', $comment),
'comment' => $comment,
'content' => $comment->comment,
'date' => format_date($comment->timestamp),
'links' => isset($links) ? theme('links', $links) : '',
'new' => $comment->new ? t('new') : '',
'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '',
'submitted' => t('Submitted by %a on %b.',
array('%a' => theme('username', $comment),
'%b' => format_date($comment->timestamp))),
'title' => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid")
));
}
function phptemplate_block($block) {
return _phptemplate_callback('block', array('block' => $block));
}
function phptemplate_box($title, $content, $region = 'main') {
return _phptemplate_callback('box', array(
'content' => $content,
'region' => $region,
'title' => $title
));
}
function _phptemplate_default($hook, $variables, $file = NULL) {
if (!empty($file) && file_exists(path_to_theme() . "/$file.tpl.php")) {
$file = path_to_theme() . "/$file.tpl.php";
}
else {
if (file_exists(path_to_theme() . "/$hook.tpl.php")) {
$file = path_to_theme() . "/$hook.tpl.php";
}
else {
if (in_array($hook, array('node', 'block', 'box', 'comment'))) {
$file = "themes/engines/phptemplate/$hook.tpl.php";
}
else {
$variables['hook'] = $hook;
watchdog('error', t('PHPTemplate was instructed to override the %name theme function, but no valid template file was found.', array('%name' => theme('placeholder', $hook))));
$file = 'themes/engines/phptemplate/default.tpl.php';
}
}
}
if (isset($file)) {
extract($variables, EXTR_SKIP); ob_start(); include "./$file"; $contents = ob_get_contents(); ob_end_clean(); return $contents; }
}
?>