Skip to content

Commit

Permalink
separate theme debugging from database debugging
Browse files Browse the repository at this point in the history
new setting for Settings.php: $theme_show_debug (admin only)
implement theme debugging context in EoS_Smarty.php and output via template hook in footer.tpl
  • Loading branch information
silvercircle committed Mar 21, 2015
1 parent f980d6a commit 1bdaad5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
1 change: 1 addition & 0 deletions Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
$ssi_db_user = '';
$db_character_set = 'utf8';
$db_show_debug = false;
$theme_show_debug = false;
$g_disable_all_hooks = false;
$db_cache_api = 'file';
$db_cache_memcached = 'localhost:11211';
Expand Down
36 changes: 31 additions & 5 deletions Sources/EosSmarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ class EoS_Smarty {
private static $_is_Debug = false;

/**
* @param bool $debug - true enables the debug mode
* @param bool $debug - true enables template debug mode
*
* this initializes the smarty template system. Should be called early in index.php,
* but after all settings have been loaded.
* but after all settings have been loaded, especially AFTER loadTheme()
*/
public static function init($debug = false)
{
global $sourcedir, $settings, $boarddir, $context;
global $sourcedir, $settings, $boarddir, $context, $user_info;

self::$_is_Debug = $debug;
// set debug context from Settings.php, but only for admins
self::$_is_Debug = $user_info['is_admin'] ? $debug : false;

// todo: Installer must check template cache directory for write access!
$compiledir = rtrim($boarddir, '/') . '/template_cache/themeid_' . $settings['theme_id']; // TODO: make this customizable
Expand Down Expand Up @@ -190,10 +191,35 @@ public static function Display()

$context['template_benchmark'] = microtime();
self::$_configInstance->setupContext();
foreach(self::$_template_names as $the_template)

// if we are in template debugging mode, register the output template and
// set up the debugging context.
if(self::$_is_Debug) {
self::$_configInstance->registerHookTemplate('theme_debug_output', 'admin/theme_debug');
self::prepareDebugContext();
}

foreach(self::$_template_names as $the_template)
self::$_smartyInstance->display($the_template);
}

/**
* @static
* prepares the template debug output with information from the template engine
* The special template admin/theme_debug.tpl will output it and is called via
* a template hook (theme_debug_output), by default in the footer template fragment.
*/
public static function prepareDebugContext()
{
global $context, $settings;

loadLanguage('ThemeDebug');
$context['theme_debug'] = array(
'template_hooks' => self::$_configInstance->getHookDebugInfo(),
'template_dirs' => implode('<br>', self::$_smartyInstance->getTemplateDir(null)),
);
}

// Ends execution. Takes care of template loading and remembering the previous URL.
public static function obExit($header = null, $do_footer = null, $from_index = false, $from_fatal_error = false)
{
Expand Down
14 changes: 1 addition & 13 deletions Sources/lib/Subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3349,25 +3349,13 @@ function db_debug_junk()
ob_end_clean();
ob_start('EoS_QueryString::ob_sessrewrite');
}
if(EoS_Smarty::isActive()) {
$_t = EoS_Smarty::getSmartyInstance()->getTemplateDir();
$smarty_template_dirs = is_array($_t) ? implode(', ', $_t) : $_t;
$_t = EoS_Smarty::getTemplates();
$smarty_templates = is_array($_t) ? implode(', ', $_t) : $_t;

$smarty_debug_info = '<h1 class="bigheader secondary borderless">Smarty debug info</h1><span style="font-size:1.1em;color:red;"><strong>Template directories:</strong></span> ' . $smarty_template_dirs . '</br><span style="font-size:1.1em;color:red;"><strong>Templates:</strong></span> ' . $smarty_templates . '<br>' . EoS_Smarty::getConfigInstance()->getHookDebugInfo() . '<br>';
}
else
$smarty_debug_info = '';

echo preg_replace('~</body>\s*</html>~', '', $temp), '
<div class="smalltext" style="text-align: left; margin: 1ex;">
', (isset($context['debug']['templates']) ? ($txt['debug_templates'] . count($context['debug']['templates']) . ': <em>' . implode('</em>, <em>', $context['debug']['templates']) . '</em>.<br />') : ''),'
', (isset($context['debug']['sub_templates']) ? ($txt['debug_subtemplates'] . count($context['debug']['sub_templates']) . ': <em>' . implode('</em>, <em>', $context['debug']['sub_templates']) . '</em>.<br />') : ''),'
', $txt['debug_language_files'], count($context['debug']['language_files']), ': <em>', implode('</em>, <em>', $context['debug']['language_files']), '</em>.<br />
', $txt['debug_stylesheets'], count($context['debug']['sheets']), ': <em>', implode('</em>, <em>', $context['debug']['sheets']), '</em>.<br />
', $txt['debug_files_included'], count($files), ' - ', round($total_size / 1024), $txt['debug_kb'], ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_include_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_include_info" style="display: none;"><em>', implode('</em>, <em>', $files), '</em></span>)<br />',
$smarty_debug_info;
', $txt['debug_files_included'], count($files), ' - ', round($total_size / 1024), $txt['debug_kb'], ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_include_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_include_info" style="display: none;"><em>', implode('</em>, <em>', $files), '</em></span>)<br />';

if (!empty($modSettings['cache_enable']) && !empty($cache_hits))
{
Expand Down
5 changes: 5 additions & 0 deletions Themes/default/languages/ThemeDebug.english.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$txt['theme_debug_title'] = 'Theme debug output (click to view details)';
$txt['theme_debug_hooks'] = 'Hook summary:';
$txt['theme_debug_template_dirs'] = 'Template directories:';
8 changes: 8 additions & 0 deletions Themes/default/tpl/admin/theme_debug.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<strong><a href="#" onClick="$('#theme_debug_content').show(); return false;">{$T.theme_debug_title}</a></strong>
<div id="theme_debug_content" style="display:none; margin:10px;">
{$C.theme_debug.template_hooks}<br>
<strong><span class="alert">{$T.theme_debug_template_dirs}</span></strong><br>
{$C.theme_debug.template_dirs}
</div>
</div>
1 change: 1 addition & 0 deletions Themes/default/tpl/footer.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div class="clear" id="footer_section">
{$SUPPORT->displayHook('theme_debug_output')}
<div>
{*customization space in the footer*}
{$SUPPORT->displayHook('footer_area')}
Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
// The main controlling function.
function smf_main()
{
global $context, $modSettings, $settings, $user_info, $board, $topic, $board_info, $maintenance, $sourcedir, $backend_subdir, $db_show_debug;
global $context, $modSettings, $settings, $user_info, $board, $topic, $board_info, $maintenance, $sourcedir, $backend_subdir, $theme_show_debug;

// Special case: session keep-alive, output a transparent pixel.
if (isset($_GET['action']) && $_GET['action'] == 'keepalive')
Expand All @@ -149,7 +149,7 @@ function smf_main()
// Load the current theme. (note that ?theme=1 will also work, may be used for guest theming.)
else {
loadTheme();
EoS_Smarty::init($db_show_debug);
EoS_Smarty::init($theme_show_debug);
}
$user_info['notify_count'] += (!empty($context['open_mod_reports']) ? 1 : 0);
URL::setSID();
Expand Down

0 comments on commit 1bdaad5

Please sign in to comment.