Mike Slinn

jekyll_begin_end

Published 2020-10-03. Last modified 2023-05-23.
Time to read: 1 minutes.

This page is part of the jekyll_plugins collection.

This Jekyll plugin defines the following filters:

  • begins_with – returns true if a string starts with a given substring.
  • does_not_begin_with – returns false if a string starts with a given substring.
  • ends_with – returns true if a string ends with a given substring.
  • does_not_end_with – returns false if a string ends with a given substring.
  • append_suffix_if_does_not_start_with – appends a suffix to the string if the string does not start with a substring.

Installation

Add the following highlighted line to your Jekyll project's Gemfile, within the jekyll_plugins group:

Gemfile
group :jekyll_plugins do 
  gem 'jekyll_begin_end'
end 

And then execute:

Shell
$ bundle

Syntax

Important: the name of each of these filters must be followed by a colon (:). If you fail to do that an error will be generated and the Jekyll site building process will halt. The error message looks something like this:

Liquid Warning: Liquid syntax error (line 285): Expected end_of_string but found string in "{{ lines | begins_with 'blah' | xml_escape }}" in /some_directory/some_files.html Liquid Exception: Liquid error (line 285): wrong number of arguments (given 1, expected 2) in /some_directory/some_file.html Error: Liquid error (line 285): wrong number of arguments (given 1, expected 2).

Examples

begins_with

First example
{% assign url = "https:/asdf.com" %}
{% assign isAbsolute = url | begins_with: 'http' %}
Second example
{% assign url = "https:/asdf.com" %}
{% assign isHttp = url | begins_with: 'http' %}
{% if isHttp %}
  <p>Absolute</p>
{% else %}
  <p>Relative</p>
{% endif %}

does_not_begin_with

First example
{% assign url = "https:/asdf.com" %}
{% assign isRelative = url | does_not_begin_with: 'http' %}
Second example
{% assign url = "https:/asdf.com" %}
{% assign isRelative = url | does_not_begin_with: 'http' %}
{% if isRelative %}
  <p>Relative</p>
{% else %}
  <p>Absolute</p>
{% endif %}

ends_with

HTML or markdown
{% assign url = "https:/asdf.com" %}
{% assign isDotCom = url | ends_with: '.com' %}
{% if isDotCom %}
  <p>.com found</p>
{% else %}
  <p>Not a .com</p>
{% endif %}

does_not_end_with

HTML or markdown
{% assign url = "https:/asdf.com" %}
{% assign isNotDotCom = url | does_not_end_with: '.com' %}
{% if isNotDotCom %}
  <p>Not a .com</p>
{% else %}
  <p>.com found</p>
{% endif %}

append_suffix_if_does_not_start_with

This filter was created to make asset reloading work better.

Given a portion of _layouts/default.html that looks like this:

_layouts/default.html fragment
{% assign csses = page.css | default: layout.css %}
{% assign nowMillis = site.time | date: '%s' %}
{% assign suffix = '?v=' | append: nowMillis %}
{% for css in csses %}
  <link rel="stylesheet" href="{{ css | append_suffix_if_does_not_start_with: 'http', suffix }}" type="text/css">
{% endfor %}

And given index.html with front matter that looks like this:

Jekyll Front Matter
---
css: [
  https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css,
  /order/order.css
]
---

The following is generated. Note that the suffix s?v=1612879301 in only applied to the relative URL for order.css.

Generated HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" type="text/css">
<link rel="stylesheet" href="/order/order.css?v=1612879301" type="text/css">
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.