Infinite Wishes 🏳️‍⚧️🚀

Is a weblog by Emma Humphries

12 Jan 2023 » 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 %}
  {% 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 %}
  {% 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.

Replies to this post are welcome. You can send an email, or a WebMention. Be kind, not clever.

Send a WebMention