Latest Post

Improving my Grav templates

Taking DRY to heart, a little

Sep 18, 2018

A couple of back-burner projects are simmering nicely. Mapping I wrote about recently.1 Today I turned back to working on micropub to add posts to this site. I have now worked out how to send two different kinds of post to two different places in the file system. Both are what you might call notes (indeed I call them Notes) but some are little status update notes and others are bookmarks.2 The way Grav works is that each file lives in a folder and Grav uses a specific template to display the contents of the file. I wanted a way to visually distinguish the two kinds of note; I chose different icons in the title.

To get it all working, I started off with different templates for the notes and the bookmarks, with the icon for each specified. That's very wasteful though, because literally the only difference between them is the icon. I was repeating myself. Bad. Worse, the page that displays a list of all the recent Notes had the same icon for each of them, because it uses a single template, and that specified a single icon.

I started casting around for a solution that would depend on discovering which template a child note was using and then calling the appropriate template. I made some progres with that, and then realised that if I could find out which template a page was using (because I tell it which template to use) then I can also tell it which icon to use, and have one single template.

And it works, although you can't see it yet because this is all still taking place only in my local sandpit.

While I was about it, I noticed that the listing page of all recent notes wasn't showing any content beyond the title. I did not, however, want all the content. The first paragraph will do. StackOverflow to the rescue. This worked perfectly for my purposes:

{% set paragraphs = page.content | split('</p>') %}
{% set firstParagraph = paragraphs[0] ~ '</p>' %}
{{  firstParagraph }}

That's all very heartening.

Photo by Zoltan Tasi on Unsplash

  1. And I will get back to it next, although there's a problem that is currently still a bit beyond me, even though I know in theory how to solve it. 

  2. There will be others to come, slowly.  

Eat This Podcast

Using food as a vehicle to explore the byways of taste, economics and trade, culture, science, history, archaeology, geography and just about anything else.

Nominated for a James Beard Award in 2015 -- and again in 2016 -- and going from strength to strength.

Subscribe to the podcast in iTunes or by email and get a new episode every two weeks. And a newsletter to stave off the pangs between podcasts.

Older Posts


Finding my way, slowly

The puns are too easy

Sep 17, 2018

Write every day? With constraints?


Sep 12, 2018

Profile picture

Jeremy Cherfas

I work as a freelance communicator. In essence, I see myself as a translator. I speak Science, and I speak English, and I work hard to make the two understand one another. Mostly, I like to help people tell their stories. I'm a biologist by training and by inclination, and my main joy is applying that to food and the agriculture and industries that supply it. I also have side interests in economics and many other things.