News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Extended body class options: current area and subaction.

Started by Antechinus, May 17, 2022, 08:11:05 PM

Previous topic - Next topic

Antechinus

If anyone wants to know what came from this: 2.1 body tag ternary: me wants area too!

I ended up with...

Code (index.template.php - Find) Select
echo '
</head>
<body id="', $context['browser_body_id'], '" class="action_', !empty($context['current_action']) ? $context['current_action'] : (!empty($context['current_board']) ?
'messageindex' : (!empty($context['current_topic']) ? 'display' : 'home')), !empty($context['current_board']) ? ' board_' . $context['current_board'] : '', '">

Code (Replace) Select
// Extended options for setting <body> class.
// The array is excessive for most use cases.
// Add, or comment out, options as required.
$body_class = array();

// If there is an action...
if (!empty($context['current_action'])) {

// ...set body class by action.
$body_class[] = 'action_'. $context['current_action'];

// If there is a current area...
if (!empty($context['max_menu_id'])) {

// ...set body class by area.
$body_class[] = 'area_' . $context['menu_data_' . $context['max_menu_id']]['current_area'];

// If there is a current subaction...
if (!empty($context['current_subaction']))
$body_class[] = 'sa_' . $context['current_subaction'];
}
}
// If no action, but we're in a board...
elseif (!empty($context['current_board'])) {

// ...set body class by board.
$body_class[] = 'board_'. $context['current_board'];

// If there is a current topic...
if (!empty($context['current_topic'])) {

// ...set body class by template.
$body_class[] = 'topic_display';
}
else
{
// ...set body class by template.
$body_class[] = 'message_index';
}

}
// Or, if none of those apply...
else
$body_class[] = 'home_index';

echo '
</head>
<body id="', $context['browser_body_id'], '" class="', implode(' ',$body_class), '">

Which works, and seems to be the sanest way of minimising asking the database silly questions. :)

If you are wondering what this is for, that probably means you don't want to know. OTOH, If you are the sort of lunatic who wants specific CSS targeting by obscure pages, so you can do strange things to them, you will immediately recognise what is it for.


I know it could go in a monster ternary, but I really do not like monster ternaries, and from what I can gather there is no significant performance advantage in using them. Lots of PHP boffins apparently think that there comes a point where just using brackets and a clear layout makes more sense. I don't know if they are right or not, but I like what they're saying anyway. ;)

ETA: Edited the code slightly, just to save a silly question sometimes (no point asking about subaction if action does not exist).

live627

Monster ternaries are unreadable. That is reason enough to get rid of them.

Advertisement: