Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add multiple sub-templates #8381

Merged
merged 7 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions Sources/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ public static function loadTemplate(string|bool $template_name, array|string $st
return null;
}

/**
* Loads sub-templates into the current theme context.
*
* This method loads each sub-template in the `sub_templates` array. If it is
* not defined, loads a single sub-template specified by `sub_template` or
* falls back to loading the `main` template.
*/
public static function loadSubTemplates(): void
{
if (isset(Utils::$context['sub_templates'])) {
foreach (Utils::$context['sub_templates'] as $sub_template) {
self::loadSubTemplate($sub_template, true);
}
} else {
Theme::loadSubTemplate(Utils::$context['sub_template'] ?? 'main');
live627 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Loads a sub-template.
*
Expand All @@ -401,20 +419,25 @@ public static function loadTemplate(string|bool $template_name, array|string $st
*
* @todo get rid of reading $_REQUEST directly
*
* @param string $sub_template_name The name of the sub-template to load
* @param string|array $sub_template_name The name of the sub-template to load
* @param bool|string $fatal Whether to die with an error if the sub-template can't be loaded
*/
public static function loadSubTemplate(string $sub_template_name, bool|string $fatal = false): void
public static function loadSubTemplate(string|array $sub_template_name, bool|string $fatal = false): void
{
if (!empty(Config::$db_show_debug)) {
Utils::$context['debug']['sub_templates'][] = $sub_template_name;
}

// Figure out what the template function is named.
$theme_function = 'template_' . $sub_template_name;
if (is_array($sub_template_name)) {
$theme_function = 'template_' . $sub_template_name[0];
$function_params = $sub_template_name[1];
} else {
$theme_function = 'template_' . $sub_template_name;
}

if (function_exists($theme_function)) {
$theme_function();
if (is_callable($theme_function)) {
call_user_func_array($theme_function, $function_params ?? []);
} elseif ($fatal === false) {
ErrorHandler::fatalLang('theme_template_error', 'template', ['template_name' => (string) $sub_template_name, 'type' => 'sub']);
} elseif ($fatal !== 'ignore') {
Expand Down Expand Up @@ -1290,18 +1313,12 @@ public static function template_header(): void

header('content-type: text/' . (isset($_REQUEST['xml']) ? 'xml' : 'html') . '; charset=' . (empty(Utils::$context['character_set']) ? 'ISO-8859-1' : Utils::$context['character_set']));

// We need to splice this in after the body layer, or after the main layer for older stuff.
// We need to splice this in after the body layer.
if (Utils::$context['in_maintenance'] && User::$me->is_admin) {
$position = array_search('body', Utils::$context['template_layers']);

if ($position === false) {
$position = array_search('main', Utils::$context['template_layers']);
}

if ($position !== false) {
$before = array_slice(Utils::$context['template_layers'], 0, $position + 1);
$after = array_slice(Utils::$context['template_layers'], $position + 1);
Utils::$context['template_layers'] = array_merge($before, ['maint_warning'], $after);
array_splice(Utils::$context['template_layers'], $position + 1, 0, ['maint_warning']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, what is maint_warning? Maybe main_warning, main, or warning?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintenance warning.

}
}

Expand Down Expand Up @@ -2192,15 +2209,15 @@ public static function pickTheme(): void
foreach (self::$current->settings['theme_variants'] as $variant) {
Utils::$context['available_themes'][$id_theme]['variants'][$variant] = [
'label' => Lang::$txt['variant_' . $variant] ?? $variant,
'thumbnail' => file_exists($theme_data['theme_dir'] . '/images/thumbnail_' . $variant . '.png') ? $theme_data['images_url'] . '/thumbnail_' . $variant . '.png' : (file_exists($theme_data['theme_dir'] . '/images/thumbnail.png') ? $theme_data['images_url'] . '/thumbnail.png' : ''),
];
}
'thumbnail' => file_exists($theme_data['theme_dir'] . '/images/thumbnail_' . $variant . '.png') ? $theme_data['images_url'] . '/thumbnail_' . $variant . '.png' : (file_exists($theme_data['theme_dir'] . '/images/thumbnail.png') ? $theme_data['images_url'] . '/thumbnail.png' : ''),
];
}

Utils::$context['available_themes'][$id_theme]['selected_variant'] = $_GET['vrt'] ?? (!empty($variant_preferences[$id_theme]) ? $variant_preferences[$id_theme] : (!empty(self::$current->settings['default_variant']) ? self::$current->settings['default_variant'] : self::$current->settings['theme_variants'][0]));
Utils::$context['available_themes'][$id_theme]['selected_variant'] = $_GET['vrt'] ?? (!empty($variant_preferences[$id_theme]) ? $variant_preferences[$id_theme] : (!empty(self::$current->settings['default_variant']) ? self::$current->settings['default_variant'] : self::$current->settings['theme_variants'][0]));

if (!isset(Utils::$context['available_themes'][$id_theme]['variants'][Utils::$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'])) {
Utils::$context['available_themes'][$id_theme]['selected_variant'] = self::$current->settings['theme_variants'][0];
}
if (!isset(Utils::$context['available_themes'][$id_theme]['variants'][Utils::$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'])) {
Utils::$context['available_themes'][$id_theme]['selected_variant'] = self::$current->settings['theme_variants'][0];
}
Copy link
Member

@Sesquipedalian Sesquipedalian Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These indentation changes appear to be a mistake.

EDIT: for some reason GitHub isn't showing all the lines I commented on, but this comment was meant to include lines -2195 to +2220 (meaning, the indentation change before 'thumbnail' => ... and all following lines.)


Utils::$context['available_themes'][$id_theme]['thumbnail_href'] = Utils::$context['available_themes'][$id_theme]['variants'][Utils::$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'];

Expand Down
4 changes: 2 additions & 2 deletions Sources/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,6 @@ public static function obExit(?bool $header = null, ?bool $do_footer = null, boo
],
);
}

}

// Start up the session URL fixer.
Expand Down Expand Up @@ -2385,7 +2384,7 @@ public static function obExit(?bool $header = null, ?bool $do_footer = null, boo
}

if ($do_footer) {
Theme::loadSubTemplate(Utils::$context['sub_template'] ?? 'main');
Theme::loadSubTemplates();

// Anything special to put out?
if (!empty(Utils::$context['insert_after_template']) && !isset($_REQUEST['xml'])) {
Expand All @@ -2397,6 +2396,7 @@ public static function obExit(?bool $header = null, ?bool $do_footer = null, boo
$footer_done = true;
Theme::template_footer();

// Add $db_show_debug = true; to Settings.php if you want to show the debugging information.
// (since this is just debugging... it's okay that it's after </html>.)
if (!isset($_REQUEST['xml'])) {
Logging::displayDebug();
Expand Down
Loading