Quantcast
Channel: phpBB.com
Viewing all articles
Browse latest Browse all 2290

Extension Writers Discussion • Re: use php to add extra css class to existing class

$
0
0
Hi

Even though cabot has shown a great solution, I would still like to explicitly address one question:
I guess that using php to add an extra class to an existing isn't possible. It would have made so many things more easy
Yes, it is possible. Relevant in this context:
Its a tiny EXT I created for personal use. I use it for small tweaks and style edits.
I also use such a private custom extension to make changes to phpBB, styles and extensions without actually having to change any of them directly.

Immediately after the generation of the finished HTML, you can use a PHP event at the appropriate point in the phpBB code to execute your own custom code in order to be able to change the rendered HTML page before it is delivered to the client.

To do this, your listener, which you certainly have in your custom extension, must define the following event hook in the getSubscribedEvents() method:

Code:

'core.twig_environment_render_template_after'=> 'add_ccs_class',
The function could then look like this:

Code:

public function add_ccs_class_($event){if (strpos($event['output'], '<div class="headerbar" role="banner">') === false){return;}$current_hour = $this->user->format_date(time(), 'H');$spwclass = 'styleedits' . $current_hour - ($current_hour % 2);$event['output'] = str_replace('<div class="headerbar" role="banner">', '<div class="headerbar ' . $spwclass . '" role="banner">', $event['output']);}
That's it as far as the pure PHP method is concerned.

Then, as far as optimizing the generation of the $spwclass variable is concerned, there are other alternatives:

luke1:
With this method, I'm basically imitating your method, including the default condition. This is not needed in either my method or yours because you explicitly define the date format and the return is therefore always a string in the range 00 to 23. But I have designed my code with a default condition anyway so that both methods are easy to compare. This technique is called "array dereferencing", if you don't already know it.

Code:

$spwclass2 = 'styleedits' . ['00' => '0','01' => '0','02' => '2','03' => '2','04' => '4','05' => '4','06' => '6','07' => '6','08' => '8','09' => '8','10' => '10','11' => '10','12' => '12','13' => '12','14' => '14','15' => '14','16' => '16','17' => '16','18' => '18','19' => '18','20' => '20','21' => '20','22' => '22','23' => '22',][$current_hour] ?? '2';
luke2:
In your case in particular, a purely arithmetic solution is also possible, which I have already shown above in the PHP code.

Code:

$spwclass = 'styleedits' . $current_hour - ($current_hour % 2);
I was also interested in how fast the individual methods are, so here is a debug display with runtimes:

Code:

string 'stoker : styleedits16 (2300 ns -> 2.3 µs)' (length=42)string 'luke1  : styleedits16 (900 ns -> 0.9 µs)' (length=41)string 'luke2  : styleedits16 (1200 ns -> 1.2 µs)' (length=42)string 'strpos : (2700 ns -> 2.7 µs)' (length=29)string 'replace: (22100 ns -> 22.1 µs)' (length=31
We are talking about nanoseconds and microseconds, so nothing to worry about. :mrgreen: Still, it is interesting to see which method is faster. In a previous life, I had a lot to do with runtime optimization, so it is basically old habits to look at such things in more detail.

Code:

{% set currenthour = "now"|date("H")|int %}
There is a problem: if you work directly with Twig's date() function, the server's time is used as the basis, not the user's time, which is relevant here.

But the trick with BODY_CLASS is interesting, I didn't know about this (unused) template variable either. :) It makes other things possible that I previously had to solve with more effort.

Oh yes, this posting was translated with Google Translator, please forgive me in advance. ^^

Statistics: Posted by LukeWCS — Fri Jan 31, 2025 3:41 pm



Viewing all articles
Browse latest Browse all 2290

Trending Articles