phptemplate.engine

  1. drupal
    1. 4.7 themes/engines/phptemplate/phptemplate.engine
    2. 5 themes/engines/phptemplate/phptemplate.engine
    3. 6 themes/engines/phptemplate/phptemplate.engine
    4. 7 themes/engines/phptemplate/phptemplate.engine
    5. 8 core/themes/engines/phptemplate/phptemplate.engine

Handles integration of templates written in pure php with the Drupal theme system.

Functions & methods

NameDescription
phptemplate_blockPrepare the values passed to the theme_block function to be passed into a pluggable template engine.
phptemplate_boxPrepare the values passed to the theme_box function to be passed into a pluggable template engine.
phptemplate_commentPrepare the values passed to the theme_comment function to be passed into a pluggable template engine.
phptemplate_features
phptemplate_init
phptemplate_node
phptemplate_pagePrepare the values passed to the theme_page function to be passed into a pluggable template engine.
phptemplate_regionsDeclare the available regions implemented by this engine.
phptemplate_templates
_phptemplate_callbackExecute a template engine call.
_phptemplate_defaultDefault callback for PHPTemplate.
_phptemplate_default_variablesAdds additional helper variables to all templates.

File

themes/engines/phptemplate/phptemplate.engine
View source
  1. // $Id$
  2. /**
  3. * @file
  4. * Handles integration of templates written in pure php with the Drupal theme system.
  5. */
  6. function phptemplate_init($template) {
  7. $file = dirname($template->filename) . '/template.php';
  8. if (file_exists($file)) {
  9. include_once "./$file";
  10. }
  11. }
  12. function phptemplate_templates($directory = 'themes') {
  13. return system_listing('^page\.tpl\.php$', $directory, 'filename');
  14. }
  15. /**
  16. * Declare the available regions implemented by this engine.
  17. *
  18. * @return
  19. * An array of regions. The first array element will be used as the default region for themes.
  20. */
  21. function phptemplate_regions() {
  22. return array(
  23. 'left' => t('left sidebar'),
  24. 'right' => t('right sidebar'),
  25. 'content' => t('content'),
  26. 'header' => t('header'),
  27. 'footer' => t('footer')
  28. );
  29. }
  30. /**
  31. * Execute a template engine call.
  32. *
  33. * Each call to the template engine has two parts. Namely preparing
  34. * the variables, and then doing something with them.
  35. *
  36. * The first step is done by all template engines / themes, the second
  37. * step is dependent on the engine used.
  38. *
  39. * @param $hook
  40. * The name of the theme function being executed.
  41. * @param $variables
  42. * A sequential array of variables passed to the theme function.
  43. * @param $file
  44. * A suggested template file to use. If the file is not found, the default $hook.tpl.php will be used.
  45. * @return
  46. * The HTML generated by the template system.
  47. */
  48. function _phptemplate_callback($hook, $variables = array(), $file = NULL) {
  49. $variables = array_merge($variables, _phptemplate_default_variables($hook, $variables));
  50. // Allow specified variables to be overridden
  51. if (function_exists('_phptemplate_variables')) {
  52. $variables = array_merge($variables, _phptemplate_variables($hook, $variables));
  53. }
  54. if (isset($variables['template_file'])) {
  55. $file = $variables['template_file'];
  56. }
  57. if (function_exists('_phptemplate_' . $hook)) {
  58. return call_user_func('_phptemplate_' . $hook, $variables, $file);
  59. }
  60. elseif (function_exists('_phptemplate_default')) {
  61. return call_user_func('_phptemplate_default', $hook, $variables, $file);
  62. }
  63. }
  64. /**
  65. * Adds additional helper variables to all templates.
  66. *
  67. * Counts how many times certain hooks have been called. Sidebar left / right are special cases.
  68. *
  69. * @param $hook
  70. * The name of the theme function being executed.
  71. * @param $variables
  72. * A sequential array of variables passed to the theme function.
  73. */
  74. function _phptemplate_default_variables($hook, $variables) {
  75. global $theme, $sidebar_indicator;
  76. static $count = array();
  77. $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
  78. $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
  79. $variables['id'] = $count[$hook]++;
  80. if ($hook == 'block') {
  81. $count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1;
  82. $variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even';
  83. $variables['block_id'] = $count['block_counter'][$sidebar_indicator]++;
  84. }
  85. elseif ($hook == 'page') {
  86. $regions = system_region_list($theme);
  87. // Load all region content assigned via blocks.
  88. foreach (array_keys($regions) as $region) {
  89. // Skip blocks in this region that have already been loaded.
  90. // This pre-loading is necessary because phptemplate uses variable names different from
  91. // the region names, e.g., 'sidebar_left' instead of 'left'.
  92. if (!in_array($region, array('left', 'right', 'footer'))) {
  93. isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region);
  94. }
  95. }
  96. }
  97. // Tell all templates where they are located.
  98. $variables['directory'] = path_to_theme();
  99. $variables['is_front'] = drupal_is_front_page();
  100. return $variables;
  101. }
  102. /**
  103. * @return
  104. * Array of template features
  105. */
  106. function phptemplate_features() {
  107. return array(
  108. 'toggle_logo',
  109. 'toggle_comment_user_picture',
  110. 'toggle_favicon',
  111. 'toggle_mission',
  112. 'toggle_name',
  113. 'toggle_node_user_picture',
  114. 'toggle_search',
  115. 'toggle_slogan'
  116. );
  117. }
  118. /**
  119. * Prepare the values passed to the theme_page function to be passed
  120. * into a pluggable template engine.
  121. */
  122. function phptemplate_page($content) {
  123. /* Set title and breadcrumb to declared values */
  124. if (drupal_is_front_page()) {
  125. $mission = filter_xss_admin(theme_get_setting('mission'));
  126. }
  127. /* Add favicon */
  128. if (theme_get_setting('toggle_favicon') && ($favicon_url = check_url(theme_get_setting('favicon')))) {
  129. drupal_set_html_head('<link rel="shortcut icon" href="'. $favicon_url .'" type="image/x-icon" />');
  130. }
  131. /**
  132. * Populate sidebars.
  133. */
  134. $layout = 'none';
  135. global $sidebar_indicator;
  136. /**
  137. * Sidebar_indicator tells the block counting code to count sidebars separately.
  138. */
  139. $sidebar_indicator = 'left';
  140. $sidebar_left = theme('blocks', 'left');
  141. if ($sidebar_left != '') {
  142. $layout = 'left';
  143. }
  144. $sidebar_indicator = 'right';
  145. $sidebar_right = theme('blocks', 'right');
  146. if ($sidebar_right != '') {
  147. $layout = ($layout == 'left') ? 'both' : 'right';
  148. }
  149. $sidebar_indicator = NULL;
  150. // Construct page title
  151. if (drupal_get_title()) {
  152. $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'drupal'));
  153. }
  154. else {
  155. $head_title = array(variable_get('site_name', 'drupal'));
  156. if (variable_get('site_slogan', '')) {
  157. $head_title[] = variable_get('site_slogan', '');
  158. }
  159. }
  160. $variables = array(
  161. 'base_path' => base_path(),
  162. 'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()),
  163. 'closure' => theme('closure'),
  164. 'content' => '<!-- begin content -->' . $content . '<!-- end content -->',
  165. 'footer_message' => filter_xss_admin(variable_get('site_footer', FALSE)) . "\n" . theme('blocks', 'footer'),
  166. 'head' => drupal_get_html_head(),
  167. 'head_title' => implode(' | ', $head_title),
  168. 'help' => theme('help'),
  169. 'language' => $GLOBALS['locale'],
  170. 'layout' => $layout,
  171. 'logo' => theme_get_setting('logo'),
  172. 'messages' => theme('status_messages'),
  173. 'mission' => isset($mission) ? $mission : '',
  174. 'primary_links' => menu_primary_links(),
  175. 'search_box' => (theme_get_setting('toggle_search') ? search_box() : ''),
  176. 'secondary_links' => menu_secondary_links(),
  177. 'sidebar_left' => $sidebar_left,
  178. 'sidebar_right' => $sidebar_right,
  179. 'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''),
  180. 'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''),
  181. 'styles' => theme_get_styles(),
  182. 'tabs' => theme('menu_local_tasks'),
  183. 'title' => drupal_get_title()
  184. );
  185. if ((arg(0) == 'node') && is_numeric(arg(1))) {
  186. $variables['node'] = node_load(arg(1));
  187. }
  188. return _phptemplate_callback('page', $variables);
  189. }
  190. /*
  191. * Prepare the values passed to the theme_node function to be passed
  192. * into a pluggable template engine.
  193. */
  194. function phptemplate_node($node, $teaser = 0, $page = 0) {
  195. if (module_exist('taxonomy')) {
  196. $taxonomy = taxonomy_link('taxonomy terms', $node);
  197. }
  198. else {
  199. $taxonomy = array();
  200. }
  201. $variables = array(
  202. 'content' => ($teaser && $node->teaser) ? $node->teaser : $node->body,
  203. 'date' => format_date($node->created),
  204. 'links' => $node->links ? theme('links', $node->links) : '',
  205. 'name' => theme('username', $node),
  206. 'node' => $node, // we pass the actual node to allow more customization
  207. 'node_url' => url('node/'. $node->nid),
  208. 'page' => $page,
  209. 'taxonomy' => $taxonomy,
  210. 'teaser' => $teaser,
  211. 'terms' => theme('links', $taxonomy),
  212. 'title' => check_plain($node->title)
  213. );
  214. // Flatten the node object's member fields.
  215. $variables = array_merge((array)$node, $variables);
  216. // Display info only on certain node types.
  217. if (theme_get_setting('toggle_node_info_' . $node->type)) {
  218. $variables['submitted'] = t('Submitted by %a on %b.', array('%a' => theme('username', $node), '%b' => format_date($node->created)));
  219. $variables['picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '';
  220. }
  221. else {
  222. $variables['submitted'] = '';
  223. $variables['picture'] = '';
  224. }
  225. return _phptemplate_callback('node', $variables, 'node-' . $node->type);
  226. }
  227. /**
  228. * Prepare the values passed to the theme_comment function to be passed
  229. * into a pluggable template engine.
  230. */
  231. function phptemplate_comment($comment, $links = 0) {
  232. return _phptemplate_callback('comment', array(
  233. 'author' => theme('username', $comment),
  234. 'comment' => $comment,
  235. 'content' => $comment->comment,
  236. 'date' => format_date($comment->timestamp),
  237. 'links' => isset($links) ? theme('links', $links) : '',
  238. 'new' => $comment->new ? t('new') : '',
  239. 'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '',
  240. 'submitted' => t('Submitted by %a on %b.',
  241. array('%a' => theme('username', $comment),
  242. '%b' => format_date($comment->timestamp))),
  243. 'title' => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid")
  244. ));
  245. }
  246. /**
  247. * Prepare the values passed to the theme_block function to be passed
  248. * into a pluggable template engine.
  249. */
  250. function phptemplate_block($block) {
  251. return _phptemplate_callback('block', array('block' => $block));
  252. }
  253. /**
  254. * Prepare the values passed to the theme_box function to be passed
  255. * into a pluggable template engine.
  256. */
  257. function phptemplate_box($title, $content, $region = 'main') {
  258. return _phptemplate_callback('box', array(
  259. 'content' => $content,
  260. 'region' => $region,
  261. 'title' => $title
  262. ));
  263. }
  264. /**
  265. * Default callback for PHPTemplate.
  266. *
  267. * Load a template file, and pass the variable array to it.
  268. * If the suggested file is not found, PHPTemplate will attempt to use
  269. * a $hook.tpl.php file in the template directory, and failing that a
  270. * $hook.tpl.php in the PHPTemplate directory.
  271. *
  272. * @param $hook
  273. * The name of the theme function being executed.
  274. * @param $variables
  275. * A sequential array of variables passed to the theme function.
  276. * @param $file
  277. * A suggested template file to use.
  278. */
  279. function _phptemplate_default($hook, $variables, $file = NULL) {
  280. if (!empty($file) && file_exists(path_to_theme() . "/$file.tpl.php")) {
  281. $file = path_to_theme() . "/$file.tpl.php";
  282. }
  283. else {
  284. if (file_exists(path_to_theme() . "/$hook.tpl.php")) {
  285. $file = path_to_theme() . "/$hook.tpl.php";
  286. }
  287. else {
  288. if (in_array($hook, array('node', 'block', 'box', 'comment'))) {
  289. $file = "themes/engines/phptemplate/$hook.tpl.php";
  290. }
  291. else {
  292. $variables['hook'] = $hook;
  293. watchdog('error', t('PHPTemplate was instructed to override the %name theme function, but no valid template file was found.', array('%name' => theme('placeholder', $hook))));
  294. $file = 'themes/engines/phptemplate/default.tpl.php';
  295. }
  296. }
  297. }
  298. if (isset($file)) {
  299. extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
  300. ob_start(); // Start output buffering
  301. include "./$file"; // Include the file
  302. $contents = ob_get_contents(); // Get the contents of the buffer
  303. ob_end_clean(); // End buffering and discard
  304. return $contents; // Return the contents
  305. }
  306. }
Login or register to post comments