# Fun Facts ```{image} funfacts.png :alt: Rendering of the funfacts example :class: example-render ``` Shows a new fun fact every day, laid out like the front page of a small newspaper. It builds on [date](date.md) in two ways: the facts live in a separate Jinja file that the screen template imports, and the screen uses custom fonts, rules and a box to make more of a designed page than a plain list of data. ## Files - `funfacts.yml` — the config. Re-renders the `funfacts` screen every midnight. - `funfacts.xml.jinja` — the screen template. - `facts.jinja` — the facts, as one Jinja list. It draws nothing on its own. - `fonts/` — [DM Serif Display](https://fonts.google.com/specimen/DM+Serif+Display), regular and italic, under the SIL Open Font License (`fonts/OFL.txt`). Text without a `font` uses the bundled FiraSans. ## Importing one Jinja file from another `facts.jinja` only sets a variable: ```jinja {% set facts = [ {"content": "Snails take the longest naps! They can sleep for up to 3 years."}, ... ] -%} ``` The screen template imports it, and every top-level variable becomes an attribute of the name given after `as`: ```jinja {% import "facts.jinja" as DB -%} {% set number = timestamp.toordinal() % (DB.facts | length) -%} {% set fact = DB.facts[number].content -%} ``` The file name is resolved like any other file the config references, relative to `CONFIG_DIR`. Keeping the data in its own file means you can add, remove or translate facts without touching the layout, and several screens can import the same file. Macros defined in an imported file can be called the same way, e.g. `DB.someMacro()`. An imported file doesn't see the importing template's variables, such as `timestamp`, unless you import it `with context`. That's why this example picks the fact in the screen template instead of in `facts.jinja`. ## Picking the fact `timestamp.toordinal()` counts days, so it grows by one every midnight. Taking it modulo the number of facts steps through the list one fact per day and starts over when all have been shown. Unlike `facts | random`, this never repeats a fact two days in a row, and rendering twice on the same day gives the same fact. ## The design - **Masthead.** The title sits above a thick and a thin rule, with the date, a "Did you know?" kicker and the fact number between the rules. Each text is placed with `x` and `align` (`left`, `center`, `right`), so the three line up with the edges and the middle of the page. - **Quote mark.** A 300 pt `“` in light gray, drawn as text. It shares a `` with the fact, so it stays next to the first line however many lines the fact wraps to. The box itself is positioned with `valign="middle"`, which centers the whole group between the header and the footer. - **Adaptive size.** The template picks a font size from the length of the fact, so one-liners are big and the longest facts still fit. - **Grays.** The panel shows 16 levels of gray. The quote mark and the small print use gray to step back and leave the fact in full black. - **Escaping.** The fact is written as `{{ fact | e }}`, so an `&`, `<` or `>` in a fact is escaped and doesn't break the XML. ## Try it From the `web/` directory: ```sh CONFIG_DIR=examples/funfacts uv run todays_paper funfacts -o funfacts.png ``` Add `-t 2027-02-22` to see how the longest fact in the list fits. ## Source ### `facts.jinja` ````xml+jinja {% set facts = [ {"content": "Snails take the longest naps! They can sleep for up to 3 years."}, {"content": "Before there were trees on the earth, our planet was covered by giant mushrooms."}, {"content": "Elephants use the skin folds on their backs to crusch mosquitos."}, {"content": "The moon has moonquakes. They happen due to tidal stresses connected to the distance between the moon and the Earth."}, {"content": "You actually lose a large percentage of your taste buds while on an airplane. This might explain a lot about those less-than-stellar in-flight meals, or why you find yourself craving the saltiest foods while in the sky."}, {"content": "You may be jealous of a bird's ability to fly, but it may soothe your envy to learn they can't live in space because they need gravity to swallow."}, {"content": "Bees can sting other bees — usually if they feel threatened or are protecting their territory. In other words, you're not the only one who's scared of getting stung."}, ... ] -%} ```` 249 lines left out. See the [full file](https://gitlab.com/todays-paper/web/-/blob/master/examples/funfacts/facts.jinja) in the repository. ### `funfacts.xml.jinja` ````xml+jinja {# The facts live in their own file. Its top-level variables become attributes of DB. -#} {% import "facts.jinja" as DB -%} {# Step one fact forward every day, starting over when all have been shown. -#} {% set number = timestamp.toordinal() % (DB.facts | length) -%} {% set fact = DB.facts[number].content -%} {# Longer facts get a smaller font so every fact fits the page. -#} {% if fact | length < 90 -%} {% set size = 66 -%} {% elif fact | length < 170 -%} {% set size = 54 -%} {% else -%} {% set size = 46 -%} {% endif -%} {% set margin = 56 -%} {% set indent = 110 -%} {% set serif = "fonts/DMSerifDisplay-Regular.ttf" -%} {% set italic = "fonts/DMSerifDisplay-Italic.ttf" -%} {# Masthead: title between newspaper-style double rules. #} The Daily Fun Fact {{ timestamp.strftime('%A %-d %B') | upper }} Did you know? FACT NO. {{ number + 1 }} {# A box keeps the quote mark next to the fact's first line, however many lines it has. The box is centered between header and footer, nudged up since the glyph's empty top margin makes the box taller than what's visible. #} “ {{ fact | e }} {# Footer #} A NEW FACT EVERY DAY · {{ DB.facts | length }} IN THE COLLECTION ```` ### `funfacts.yml` ````yaml # Shows a new fun fact every day. Builds on date by importing the facts from a second Jinja file. settings: # Decides when the day changes, and with it the fact. timezone: Europe/Stockholm screens: # Panel resolution. Use 1024x758 for an ED060XC3 panel, 1448x1072 for an ED060KC1 panel. width: 1024 height: 758 rotate: 0 funfacts: template: funfacts.xml.jinja scheduler: # Re-render every midnight to show the next fact. - days: [1, 2, 3, 4, 5, 6, 7] hour: 0 screen: funfacts ````