Question marks in Liquid variable names
February 24, 2014 navigateright
Liquid is a very forgiving templating language, which is part of why I love it. I sometimes find myself pushing Liquid to its limits to see how much it will allow before throwing an error. With a history of Ruby under my belt, I’ve become accustomed to using boolean methods that end with a question mark (ex: new?
, blank?
, etc).
I felt rather adventureous one day, so I tried following this same approach with Liquid. I created a metadata value named inverted?
in Siteleaf to indicate that the page should use white text instead of black. In my template, I added the conditional {% if meta.inverted? %}
to insert a CSS class into body.
It worked!
From there, I discovered that Liquid has no objections with hyphens in variable names, either. Instead of background_color
, I can use the CSS syntax background-color
and access it with {{ meta.background-color }}
.
Liquid, I love you.