Idle question about 2.0.x CSS calls

Started by Antechinus, September 24, 2019, 09:44:35 PM

Previous topic - Next topic

Antechinus

Yes I know, but I'd prefer to not load them at all. Is that possible in 2.0.x, without hacking Load.php?

SychO

Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

Arantor

For some definition of possible. You should be able to do something by registering hooks (or futzing with global state) during template_init() which is a real PHP function which is loaded pretty early, not sure if too early or too late for that, but you can always rewrite template_html_above() to be way more picky about what it emits as things to load.

Bloc

Template_init is called *after* the stylesheets are added to $context['html_headers']. So you have to mangle that one to rip them out. 

Bloc

No, sorry. Seems I got it wrong..loadTemplate actually checks for a template_{}_init function..which means it can check it there. I didn't even know there was such a thing. :)

The template_init is called way earlier, before stylesheets are called.

Arantor

That's probably too early, then, but you can always dismember it before outputting.

Bloc

Yes, taking apart $context['html_headers'] seems the only way.

Antechinus

Ok, this is sounding interesting. What I'm really after is preventing the loading of admin.css and editor.css. They're the only two additional default files that SMF will load in practice (IE and wireless being obsolete). Although I suppose I should add webkit.css to that list, since it has never really done anything anyway but still gets called whenever Load.php sniffs Webkit.

Antechinus

Quote from: Arantor on September 28, 2019, 03:30:33 PM
That's probably too early, then, but you can always dismember it before outputting.
Quote from: Bloc on September 28, 2019, 03:36:13 PM
Yes, taking apart $context['html_headers'] seems the only way.

Ok, how do I do this? This sorting of RTL handling is the last thing stopping Mutant Responsive Beastie from infesting the Themes Site.*

*Well, apart from actual approval, but I'm sure that can be dealt with.

Arantor

What exactly do you want to remove from html_headers before it goes out?

Antechinus

Admin.css, editor.css, webkit.css.
The first two so I can call RTL versions in head, or not, depending on chosen language. The third because it's effectively deprecated these days anyway.

Arantor

Ok, so I need to set up a 2.0 to see exactly what that is, but essentially you'd construct a call to str_replace to find that CSS and replace it with empty string to remove.

Antechinus

The other option is that, according to Theme Site rules, it is permissible to have a theme rely on a mod, and it would be easy to write a mod to edit the relevant array in Load.php.
So if the theme-only solution is going to get gnarly, and if in coding terms it would be significantly saner to use a mod, then I could be talked into going with the mod (I could easily write that myself). But I'd prefer to not have to rely on a mod, if it's possible to avoid it without going nuts on code.

Arantor

Removing a given CSS call is a one liner if you happen to know exactly what the CSS call is exactly, but I can't do that without actually looking at the code, as I have no 2.0 code on my laptop any more.

I'll install it tonight and take a look.


Arantor

Sorry, I just forgot about it, in the madness that has been my work lately. Will look tomorrow when I get up.

Antechinus

Yeah I figured you were probably busy, which is why I didn't want to hassle you too soon. :)

Arantor

So if you want to remove admin, editor, webkit you can use this piece of code:

function template_html_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;

$remove_bundled_stylesheet = function($sheets) use ($settings, &$context)
{
foreach ($sheets as $sheet)
{
$sheet_path = file_exists($settings['theme_dir']. '/css/' . $sheet . '.css') ? 'theme_url' : (file_exists($settings['default_theme_dir']. '/css/' . $sheet . '.css') ? 'default_theme_url' : '');
if ($sheet_path)
{

$header = "\n\t" . '<link rel="stylesheet" type="text/css" id="' . $sheet . '_css" href="' . $settings[$sheet_path] . '/css/' . $sheet . '.css" />';
$context['html_headers'] = str_replace($header, '', $context['html_headers']);
}
}
};
$remove_bundled_stylesheet(['admin', 'editor', 'editor_ie']);


The first couple of lines give you the place to put it inside index.template.php, the rest of the code handles doing the work which amounts to looking for the way loadTemplate adds these things to html_headers and removes them again. You need editor_ie as well as it can load either depending on situation.

Webkit is only added inside index.template.php so you can hack that out at your leisure:

// Some browsers need an extra stylesheet due to bugs/compatibility issues.
foreach (array('ie7', 'ie6', 'webkit') as $cssfix)
if ($context['browser']['is_' . $cssfix])
echo '
<link rel="stylesheet" type="text/css" href="', $settings['default_theme_url'], '/css/', $cssfix, '.css" />';

Antechinus

Cool. Thanks.
Turns out I'd already removed the call for ie/webkit anyway. I'd just forgotten about it.

Arantor


Advertisement: