| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Handles integration of templates written in PHPtal with the Drupal theme system. |
* Integrates PHPTAL templates with the Drupal theme system. |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
/** |
| 10 |
|
* @require_once |
| 11 |
|
* Includes the PHPTAL class. |
| 12 |
|
*/ |
| 13 |
|
require_once(drupal_get_path('theme_engine', 'phptal') . 'PHPTAL.php'); |
| 14 |
|
|
| 15 |
|
/** |
| 16 |
|
* Implementation of the hook_extension. |
| 17 |
|
* |
| 18 |
|
* @return |
| 19 |
|
* The default PHPTAL template extension. |
| 20 |
|
* This can be changed to whatever you like. |
| 21 |
|
* However, and ending of .php is recommended. |
| 22 |
|
*/ |
| 23 |
|
function phptal_extension() { |
| 24 |
|
return '.tpl.php'; |
| 25 |
|
} |
| 26 |
|
|
| 27 |
|
/** |
| 28 |
|
* Implementation of the hook_init. |
| 29 |
|
*/ |
| 30 |
function phptal_init($template) { |
function phptal_init($template) { |
| 31 |
$file = dirname($template->filename) . '/template.php'; |
$file = dirname($template->filename) .'/template.php'; |
| 32 |
if (file_exists($file)) { |
if (file_exists($file)) { |
| 33 |
include_once "./$file"; |
include_once "./$file"; |
| 34 |
} |
} |
| 35 |
} |
} |
| 36 |
|
|
| 37 |
/** |
/** |
| 38 |
* Implementation of hook_theme to tell Drupal what templates the engine |
* Implementation of hook_theme. |
| 39 |
|
* |
| 40 |
|
* This tells Drupal what templates the engine |
| 41 |
* and the current theme use. The $existing argument will contain hooks |
* and the current theme use. The $existing argument will contain hooks |
| 42 |
* pre-defined by Drupal so that we can use that information if |
* pre-defined by Drupal so that we can use that information if |
| 43 |
* we need to. |
* we need to. |
| 44 |
*/ |
*/ |
| 45 |
function phptal_theme($existing, $type, $theme, $path) { |
function phptal_theme($existing, $type, $theme, $path) { |
| 46 |
$templates = drupal_find_theme_functions($existing, array('phptal', $theme)); |
$templates = drupal_find_theme_functions($existing, array('phptal', $theme)); |
| 47 |
$templates += drupal_find_theme_templates($existing, '.tal', $path); |
$templates += drupal_find_theme_templates($existing, phptal_extension(), $path); |
| 48 |
return $templates; |
return $templates; |
| 49 |
} |
} |
| 50 |
|
|
| 51 |
/** |
/** |
| 52 |
* Adds additional helper variables to all templates. |
* Implementation of hook_render_template. |
|
* |
|
|
* Counts how many times certain hooks have been called. Sidebar left / right are special cases. |
|
| 53 |
* |
* |
| 54 |
|
* This is called for every phptal template found in the |
| 55 |
|
* theme's directory that matches the extension set above |
| 56 |
|
* in hook_extension. |
| 57 |
|
* |
| 58 |
|
* @param $template_file |
| 59 |
|
* The path to the current template. |
| 60 |
* @param $variables |
* @param $variables |
| 61 |
* A series of key-value value pairs. |
* The Drupal variables array. These are passed to the $phptal class. |
| 62 |
* @param $hook |
* @param $phptal |
| 63 |
* The name of the theme function being executed. |
* The PHPTAL object. All variables are assigned to it. |
| 64 |
*/ |
* @param $phptal->setCacheLifetime() |
| 65 |
function phptal_engine_preprocess(&$variables, $hook) { |
* Sets the lifetime of cache files (in days). |
| 66 |
global $user; |
* @param $phptal->setForceReparse() |
| 67 |
static $count = array(); |
* Forces reparsing (i.e., no cache). Useful for debugging. |
| 68 |
|
* @return |
| 69 |
// Create variables so anything which is themed can be zebra striped automatically. |
* HTML, PHP, etc...(i.e., the current template) |
|
$count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1; |
|
|
$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even'; |
|
|
$variables['id'] = $count[$hook]++; |
|
|
|
|
|
// Tell all templates where they are located. |
|
|
$variables['directory'] = path_to_theme(); |
|
|
$variables['is_front'] = drupal_is_front_page(); |
|
|
// Tell all templates by which kind of user they're viewed. |
|
|
$variables['logged_in'] = ($user->uid > 0); |
|
|
$variables['is_admin'] = user_access('access administration pages'); |
|
|
} |
|
|
|
|
|
/** |
|
| 70 |
*/ |
*/ |
| 71 |
function phptal_render_template($template_file, $variables) { |
function phptal_render_template($template_file, $variables) { |
| 72 |
global $theme_engine; |
$phptal = new PHPTAL($template_file); |
| 73 |
|
|
| 74 |
require_once 'PHPTAL.php'; |
foreach ($variables as $key=>$value) { |
| 75 |
|
$phptal->$key = $value; |
|
$phptal = new PHPTAL($template_file); |
|
|
foreach ($variables as $k=>$v) { |
|
|
$phptal->$k = $v; |
|
|
} |
|
|
|
|
|
try { |
|
|
return html_entity_decode($phptal->execute()); |
|
|
} |
|
|
catch (Exception $e) { |
|
|
watchdog('error', '%engine.engine, %file: %message.', array( |
|
|
'%engine' => $theme_engine, '%file' => $template_file, |
|
|
'%message' => $e->getMessage())); |
|
|
error_log("[phptal] $template_file: " . $e->getMessage()); |
|
| 76 |
} |
} |
| 77 |
|
|
| 78 |
|
$phptal->setCacheLifetime(1); |
| 79 |
|
//$phptal->setForceReparse(true); |
| 80 |
|
|
| 81 |
|
try { |
| 82 |
|
return html_entity_decode($phptal->execute()); |
| 83 |
|
} |
| 84 |
|
catch (Exception $e) { |
| 85 |
|
//echo $e; //FOR DEBUGGING: Uncomment to see errors. |
| 86 |
|
phptal_engine_error(); |
| 87 |
|
} |
| 88 |
} |
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
/** |
/** |
| 92 |
|
* phptal_engine_error |
| 93 |
|
* |
| 94 |
|
* Display errors in a safe way. NOTE: I am still working on this function. |
| 95 |
|
* |
| 96 |
|
* @param $error |
| 97 |
|
* Our error string |
| 98 |
*/ |
*/ |
| 99 |
function phptal_extension() { |
function phptal_engine_error($error='default') { |
| 100 |
return '.tal'; |
$doclink = ' ' . t('Please refer to the documentation on the ').'<a href="http://drupal.org/project/phptal">' . t('PHPTAL project page') . '</a>.'; |
| 101 |
|
$errors = array( |
| 102 |
|
'default' => t('There is a problem with the phptal theme engine') . $doclink, |
| 103 |
|
); |
| 104 |
|
if ( isset($errors[$error]) ) { |
| 105 |
|
$msg = $errors[$error]; |
| 106 |
|
} else { |
| 107 |
|
$msg = $errors['default']; |
| 108 |
|
} |
| 109 |
|
drupal_set_message($msg, 'error'); |
| 110 |
} |
} |