A standard way to style any OGC layer is using SLD. Styled Layer Descriptors.
The Styled Layer Descriptors are a simple (and standard) way to style your web maps. But when you style you also want to generate a nice legend for it.
I recently found myself with the situation of reworking a website styling code. Instead of writing it from scratch we used existing pieces to cleanly fill our webapp divs with the correct legend.