idea
Via jQuery, I was able to mark all :first-child and :last-child elements in document (well, almost all :)) with class first which could I later style (i.e. first li in ul#navigation would be easily adressable as ul#navigation .first).
I used following code:
var $f = $('*:first-child')
$f.addClass('first');
var $l = $('body *:last-child')
$l.addClass('last');
example
Example is already here – it’s not the way to do it however, it’s just an idea prototyped in other, for me at the moment easier language.
question
Now, my question is if it’s possible to do the same via php, so non-JS users/gadgets could have the same effects and additional styling and also it would be less overkill on browser.
So, is it possible to capture output, parse it as html and inject this class easily in php?
clarification
I’m quite aware of output buffering, just haven’t done much stuff with it – also, i’m not sure about modificating output string in php as parsed dom (without regex) – and how tough on server it’ll be – with caching of course, so this whole stuff will run once until the page will be edited again.
I’m sure you could use output buffering to capture your assembled PHP page and then use DOM and XPath on it to add the class attributes, but the question is, why don’t you just put the classes onto the elements when assembling the page in the first place? Saves you the jQuery and the capturing.
Also, adding the CSS classes with jQuery to be able to do
ul#navigation.firstis somewhat odd too, because the jQuery expression you used is a CSS selector, so you could use it directly to style the first child from your CSS file. The only reason to add a class.firstis if you want to be backwards compatible with browsers unable to process:first-child.