"Glamorous" with Gismeteo widget for MODx

Actually, on many sites anyway, it can be useful to stick the details on what now weather in a particular place on our planet. Because contain its own weather service to most people at least expensive, far more logical to use some of the resources providing free weather widgets on rather soft grounds (just need to put a link on their website). One of the most famous and popular is gismeteo.ru. But its standard widgets is not suitable for everyone... and that if you want to alter the design of the widget under the brand identity of the company, or to cover a single widget multiple cities (by default, there is one widget — one city)? For example, a recent client, a travel company, wanted a widget with a list of places in which they work. Here's a (screenshot of the end result):

what we want to achieve

I think it can be done? As far back as possible, and, moreover, I already have made your job easier, today, for a special evening writing a snippet for MODx with a couple of demo templates!

Actually, kachem small arhivchik. In it, we see three files — actually, the snippet (file gismeteo.php for correct operation requires libxslt, however, most of the normal purpose of this library is included) and two xsl file with templates. Actually, I think, for a person who is familiar with the ideology of MODx, you will not have problems with the installation: go to the "manage resources" create a new snippet, call it, say, Gismeteo and there copypaste the contents of the file gismeteo.php. As and with templates for each of them create a chunk (I tried to adhere to the ideology of MODx not to use connection files from a hard disk without special needs), and it would be nice one of these chunks called "informer" is the value of the parameter tpl is the default... well, more on this will be written in detail.

Before I turn to the detailed analysis of the parameters in the snippet call (luckily only two of them) and logic of his work — a little digression. Why, in fact, XSLT even though it's not the fastest, it would seem that technology? Yes, because Gismeteo offers a very convenient feature — getting weather reports in XML format. And, with the help of the XSLT templates we can provide that information somehow, minimizing the "stitching" of information presentation in php-code and significantly save the number of lines of actual code. And, moreover, because we have no limits — for one the snippet call, we may request information in several cities, put it then on this information the overall pattern... which is what we needed, right?

Therefore, the snippet call is simple as the Siberian boots. Something like:

the [[Gismeteo? &tpl=`informer_full`&source=`http://informer.gismeteo.ru/xml/32150_1.xml,http://informer.gismeteo.ru/xml/32158_1.xml`]]

Yes, as I said, the option only two. The parameter tpl is the name of the chunk with the XSLT template (by default — "informer"). The source parameter specifies a comma — separated list of URLS, which are XML-widgets for your cities (to get those URLS by the link above). If the source parameter is not specified, by default it will be equal to the informer for the capital of our glorious country :)

How it works. The snippet displays a table with a header. Inside it runs an iterator over the list of values for the source parameter. In other words, for each city we get the XML source, perekodirovat it in normal human UTF-8 (and in the original it is given in Galskom cp1251, and even the method urlencode prektirovaniya) imposed on him an XSL style (convertible to normal <tr> s on the table, to put it simply) — and then model the resulting HTML-e numerical values for "cloud" and "precipitation" in normal verbal counterparts. Yes, not very convenient that as many as three stages of parsing, but still have "hard" to sew into pattern classes cloudiness and precipitation — but, alas, nothing can be done, because the owner — a gentleman who wanted to give XML so it gave. Well, okay. Details of all of this you can see in the code — since he is very small, so even with the review. In the end we get a neat sign and on it can be subverted in every possible way with the help of CSS, "sharpening" its the design of everything else.
In General enjoy! I think there are still those to whom this little but useful thing useful...

PS No, Gismeteo doesn't pay me for advertising, don't think :)
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Automatically create Liquibase migrations for PostgreSQL

Vkontakte sync with address book for iPhone. How it was done

What part of the archived web