Overview
A .nds-weather element shows the current weather for one place. You write its markup, the place’s coordinates and, in .nds-weather-city, its name. The script fills three slots: the icon, the conditions and the temperature. It changes nothing else, so the layout, the order and the words around the slots are yours. A page can show any number of places. The Top Bar can show one as an item.
An element with no slots gets the auto line: the icon, the name, the conditions and the temperature with its unit, in the page language. The canon uses a Meta line, and the tile structure a Card.
Pick another component when:
Markup
Variants
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Custom line (default) | — | — | The weather for one place, with the slots in your own line |
| Structure | Auto line | canon #weather-auto |
— | No slots: the script writes the line in the page language, with its own punctuation and unit. Pick it when one markup serves every language |
| Structure | Several places | canon #weather-places |
— | The weather for each place a service covers, such as its branch cities: one auto line per place, in a Meta column (.nds-col). Each item has its own coordinates and name |
| Structure | Tile | canon #weather-tile |
— | The weather as a card of its own, such as on a service’s home page. The same slots in a Card, as wide as its content: change the card freely |
Built-in Features
-
Open-Meteo
The weather comes from the public Open-Meteo service, with no key and no account. It renders again every 15 minutes while the tab shows.
-
Day and Night
The icon shows day or night at the place itself, as Open-Meteo reports it, not at the visitor's clock.
-
One Request per Place
Elements on the same place and unit share one request. The browser keeps each result for 14 minutes, so a page load or a language switch in that time sends no request.
Best Practices
- Set
data-latitudeanddata-longitudeto the place your service is for. The Riyadh default is wrong for a service in another region. - Write the place’s name in
.nds-weather-city, so people know which place the weather is for. Without it, the element shows only the conditions and the temperature. - Write a placeholder, such as
--, in each text slot. It holds the line’s width until the weather arrives. - Write the unit after
.nds-weather-temp, to matchdata-weather-unit: the script writes the number only. - Your own words around the slots stay in one language. Where one markup serves every language, use the auto line, or mark your words with
data-i18n. - Where one markup serves every language, mark the name with
data-i18n. Give its other languages inwindow.NDS_I18N_LANGS, as for any word of your own. See Internationalization. A page per language needs neither: write the name in that page’s language. - Show a few places at most. Each place is one request from each visitor, at most once every 14 minutes.
- In the top bar, the weather is one of the two items DGA allows.
API
Classes
| Class | Element | Effect |
|---|---|---|
.nds-weather |
Any element, such as a span.nds-meta-item or a .nds-card |
The item. The script fills its slots and changes nothing else. With no slot, it writes the auto line |
.nds-weather-icon |
An i.nds-icon in .nds-weather |
Slot. The script adds the icon class for the conditions, by day or night |
.nds-weather-condition |
A span in .nds-weather |
Slot. The script writes the conditions, such as “Clear”, in the page language |
.nds-weather-temp |
A span in .nds-weather |
Slot. The script writes the temperature as a whole number, in the unit data-weather-unit picks, with no unit sign |
.nds-weather-city |
A span in .nds-weather |
Optional. The place’s name, as the page’s own text. The script never writes it |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-latitude, data-longitude |
.nds-weather |
The place’s coordinates. The default is Riyadh (24.7136, 46.6753) |
data-weather-unit |
.nds-weather |
c (the default) for Celsius, f for Fahrenheit. The weather service sends the temperature in that unit, and the auto line writes its sign |
hidden |
.nds-weather |
The script sets it when the request fails, and removes it when a request works |
The words come from the weather section of the string pack: the conditions, and the auto line with or without the place’s name. A change of lang on <html> writes every item again, from the cache.
JavaScript
NDS.Weather loads after the page shows. Until it loads, a call to it loads it and returns a promise.
| Method | Effect |
|---|---|
NDS.Weather.init() |
Writes every item on the page, from the cache or a new request. Renders them again every 15 minutes. The loader runs it when an item is on the page. NDS.init.mount() runs it on markup added later |
The full API is in the banner of _js/nds-weather.js.