Âť Using Jekyll Variables to Promote a Cause
This week I got a burst of visits to the blog. That can make one happy, anxious, or both (depending on the source of the traffic.)
A friend suggested I take advantage of the situation to put up a banner for a bail bond fund on the page getting all the hits.
I made a quick edit and pushed the site. I hope visitors saw the call to action and followed it.
After this, itâd be good to have a way to put up a banner on one or all posts.
Since this blog uses Jekyll, a static site generator, I have to do this in the templates that generate the site.
Jekyll allows you to set variables at different levels or scopes. Page-specific variables you declare in a postâs YAML front-matter, such as date
, title
, and tags
. In a template you access them as page.<var name>
.
You can create your own variables in a scope, so I added
banner: true
to the postâs front matter.
In the template for blog posts I added*
{% if page.banner %}
<div class="page-banner">
{% include banner.html %}
</div>
{% endif %}
Thatâll include the content of _includes/banner.html
when the page renders. Thatâs where you put your banner with the text, image references, and links to what youâre promoting.
If you want to put a banner on every post then add banner: true
to your siteâs _config.yaml
which sets that for the whole site scope.
Then change the block in your blog post template to:
{% if page.layout != archive and page.banner or site.banner %}
<div class="page-banner">
{% include banner.html %}
</div>
{% endif %}
On this site, the index page with recent posts is generated using an archive.html
template so I need to detect that so that the banner doesnât appear on every post displayed on the index page.
Youâll need to update that conditional for your siteâs structure.
If you use bundle exec jekyll serve
or jekyll serve
locally while youâre working on your site, youâll have to stop and restart it to pick up changes to variables in the site scope.
I asked visitors to the page to support the Bay Area Immigration Bond Fund. California recently reformed cash bail in its criminal courts, but immigrants living here may have to post bond in Federal immigration proceedings.
Cash bail disproportionally harms Black and or poor people, even those who have not been charged with a crime.
* To display Liquid as source in a file rendered by liquid remember to put it in a raw
block.