SVG as your MVC View
Lately, I’ve been playing with the SVG format - a W3C backed XML format for Scalable Vector Graphics. Firefox can now render SVG natively, and this opens the door for lots of fun things you can do with it.
There are already very nice implementations of PHP classes that, for example, generate nice graphs using SVG - although I’ve never used it, the eZ Components Graph module is a very good example.
However, if you are looking to generate graphics dynamically from a pre-built template, you don’t really
need to use any libraries or heavy OO and XML abstractions. Since SVG is just another type of XML, just
like XHTML, using PHP to generate dynamic SVG is quite intuitive. Looking at this from an MVC
perspective, you can think of SVG as yet another form of view for data - and it can be quite easily used
as a “template” just like XHTML is used.
It is also quite easy to embed SVG into XHTML or vice-versa - but this means that you will need an SVG
capable browser in order to render the page (did I already mention Firefox is one?)
I am still playing around with the format and with the whole concept - but in the mean time, here is a (non-scalable png) screenshot of an SVG graph I generated using simple PHP code:
You can see how this graph is embedded within XHTML and generated here - it also shows the entire source code. A few notes though:
- I have no idea how other browsers render this. It works with Firefox - I’m quite sure IE (even 7.0) does not render SVG without special plugins
- Pay attention to the header() call - it’s important!
- The use of Zend Framework is there only for fetching the data from digg - the real stuff doesn’t rely on Zend Framework
If I have enough time (and I’m able to get myself to focus
) I’ll study this some more and maybe build some nice tutorial.













