Overview

Building on the initial steps to create Create a simple GitHub Pages blog post and Using Jinja with shell variables from the CLI we can combine Jinja with a template and some environment variables to streamline initial page creation.

Background

The initial set up of each page and its frontmatter for each post was simple but fiddly as first documented. We had already simplified the process with environment variables, now we attempt to streamline the process further using Jinja, and a template. The simplification over the previous HEREDOC is nominal but should ultimately be easier.

Now let’s walk through the slightly improved process.

How-to

  1. Set some common variables to reuse in subsequent steps.

    Set the current date & time, and the subject of the blog post:

     export BLOGDATE=$(date -I)
     export BLOGTITLE='Updated Github pages post'
     export BLOGFILE=blog-title.md
     echo $BLOGDATE $BLOGTIME $BLOGTITLE $BLOGFILE
    

    Or, in a slightly more compressed form:

     export BLOGDATE=$(date -I)
     export BLOGTITLE='Updated Github pages post'
     export BLOGFILE=blog-title.md
     echo $BLOGDATE $BLOGTIME $BLOGTITLE $BLOGFILE
    
  2. Activate the Python virtual environment (venv) containing the Jinja CLI:
     $ . ~/.venv/jinjacli/bin/activate
    
  3. Populate the template using Jinja:
     $ jinja -X 'BLOG*' ~/repos/github-pages/_templates/post_template.md > ~/repos/github-pages/_posts/${BLOGDATE}-${BLOGTIME//:/-}-${BLOGFILE}
    

    Note: the {BLOGTIME//:/-} construct uses bash substring replacement to switch from colons (required for the frontmatter in the post’s markdown) to a hyphen, for the file name.

Summary

We now have a simpler process to quickly boilerplate a new blog article with its frontmatter.

Further reading