⬆️ ⬇️

"Glam" informer with Gismeteo for MODx

Actually, on many sites one way or another it is useful to insert information about the current weather in one or another place of our planet. Since it’s at least expensive for most people to maintain their own weather service, it’s more logical to use some of the resources that provide free weather informers on fairly soft grounds (you just need to link to their website). One of the most famous and popular services is gismeteo.ru . But its standard informers are far from being suitable for everyone ... but what if you want to remake the design of the informer to fit the corporate style of the company, or to cover several cities with one informer (by default there is one city for one informer)? For example, a recent customer, a travel company, wanted an informer with a list of places in which they work. Here's a (screenshot of the final result):



what we want to achieve



Think you can not do this? As much as possible, and, moreover, I have already facilitated your work by writing a special snippet for MODx with a couple of demo templates for the evening today!

')

Actually, what a small archivist . In it, as we see, there are three files - in fact, the snippet itself (the gismeteo.php file, requires libxslt to work correctly, however, on most normal hosts this library is included) and two xsl files with templates. Actually, I think for a person familiar with the ideology of MODx, there will be no problems with the installation: go to "resource management", create a new snippet, call it, say, Gismeteo - and copy-paste the contents of the gismeteo.php file there. The same with templates - we create a chunk for each of them (I tried to adhere to the MODx ideology and not use the connection of files from the hard drive without special needs), and it would be nice to call one of these chunks " informer " - this is the default tpl value ... however, further on this will be written in more detail.



Before I proceed to a detailed analysis of the parameters for calling a snippet (there are only two of them) and the logic of its work is a small lyrical digression. Why, in fact, XSLT - although it is not the fastest, it would seem, technology? Yes, because Gismeteo offers a very handy feature - getting weather reports in XML format . And it is with the help of XSLT templates that we can present this information in any way, minimizing the stitching of the presentation of information in php code and significantly saving the number of lines of this code itself. And, moreover, nobody restricts us - for one call to the snippet, we can request information for several cities at once, then putting a general presentation template on this information ... which is what we needed, right?



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



[[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, there are only two parameters. The tpl parameter is the name of the chunk with the XSLT template (the default is “informer”). The source parameter is a comma-separated list of URLs that have XML informers for the cities you need (you can get these URLs from 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 homeland :)



How it works. The snippet itself displays a table with a title. Inside it runs an iterator over the entire list of values ​​for the source parameter. In other words, for each city we get the XML source, recode it into normal human UTF-8 (or it is given in the original in the cp1251 reptile, and also with the urlencode method, coded), impose the XSL style on it ( tr> -s for a table, in simple terms) - and then we replace in the resulting HTML-e numerical values ​​for the “cloudiness” and “precipitation” parameters with normal verbal analogues. Yes, it’s not very convenient, that there are already three stages of parsing, and you also have to “hard” stitch classes cloudiness and precipitation into the template - but, alas, there's nothing you can do, because the owner, the master, wanted to give up the XML, and gave it to him . Anyway. Details of all this can be seen in the code - it is a very small blessing, and even with comments. In the end, we get a neat label - and above it we can in every possible way be perverted with the help of CSS, “sharpening” it under the design of everything else.



In general - enjoy! I think there will be those who find this small but useful thing useful ...



PS No, the site Gismeteo does not pay me for advertising, do not think :)

Source: https://habr.com/ru/post/20607/



All Articles