W3cubDocs

/Jekyll

Templates

Jekyll uses the Liquid templating language to process templates. All of the standard Liquid tags and filters are supported. To make common tasks easier, Jekyll even adds a few handy filters and tags of its own, all of which you can find on this page. Jekyll even lets you come up with your own tags via plugins.

Filters

Description Filter and Output

Relative URL

Prepend the baseurl value to the input. Useful if your site is hosted at a subpath rather than the root of the domain.

{{ "/assets/style.css" | relative_url }}

/my-baseurl/assets/style.css

Absolute URL

Prepend the url and baseurl value to the input.

{{ "/assets/style.css" | absolute_url }}

http://example.com/my-baseurl/assets/style.css

Date to XML Schema

Convert a Date into XML Schema (ISO 8601) format.

{{ site.time | date_to_xmlschema }}

2008-11-07T13:07:54-08:00

Date to RFC-822 Format

Convert a Date into the RFC-822 format used for RSS feeds.

{{ site.time | date_to_rfc822 }}

Mon, 07 Nov 2008 13:07:54 -0800

Date to String

Convert a date to short format.

{{ site.time | date_to_string }}

07 Nov 2008

Date to Long String

Format a date to long format.

{{ site.time | date_to_long_string }}

07 November 2008

Where

Select all the objects in an array where the key has the given value.

{{ site.members | where:"graduation_year","2014" }}

Where Expression

Select all the objects in an array where the expression is true. Jekyll v3.2.0 & later.

{{ site.members | where_exp:"item", "item.graduation_year == 2014" }} {{ site.members | where_exp:"item", "item.graduation_year < 2014" }} {{ site.members | where_exp:"item", "item.projects contains 'foo'" }}

Group By

Group an array's items by a given property.

{{ site.members | group_by:"graduation_year" }}

[{"name"=>"2013", "items"=>[...]}, {"name"=>"2014", "items"=>[...]}]

Group By Expression

Group an array's items using a Liquid expression.

{{ site.members | group_by_exp:"item", "item.graduation_year | truncate: 3, \"\"" }}

[{"name"=>"201...", "items"=>[...]}, {"name"=>"200...", "items"=>[...]}]

XML Escape

Escape some text for use in XML.

{{ page.content | xml_escape }}

CGI Escape

CGI escape a string for use in a URL. Replaces any special characters with appropriate %XX replacements. CGI escape normally replaces a space with a plus + sign.

{{ "foo, bar; baz?" | cgi_escape }}

foo%2C+bar%3B+baz%3F

URI Escape

Percent encodes any special characters in a URI. URI escape normally replaces a space with %20. Reserved characters will not be escaped.

{{ "http://foo.com/?q=foo, \bar?" | uri_escape }}

http://foo.com/?q=foo,%20%5Cbar?

Number of Words

Count the number of words in some text.

{{ page.content | number_of_words }}

1337

Array to Sentence

Convert an array into a sentence. Useful for listing tags. Optional argument for connector.

{{ page.tags | array_to_sentence_string }}

foo, bar, and baz

{{ page.tags | array_to_sentence_string: 'or' }}

foo, bar, or baz

Markdownify

Convert a Markdown-formatted string into HTML.

{{ page.excerpt | markdownify }}

Smartify

Convert "quotes" into “smart quotes.”

{{ page.title | smartify }}

Converting Sass/SCSS

Convert a Sass- or SCSS-formatted string into CSS.

{{ some_scss | scssify }} {{ some_sass | sassify }}

Slugify

Convert a string into a lowercase URL "slug". See below for options.

{{ "The _config.yml file" | slugify }}

the-config-yml-file

{{ "The _config.yml file" | slugify: 'pretty' }}

the-_config.yml-file

{{ "The _cönfig.yml file" | slugify: 'ascii' }}

the-c-nfig-yml-file

{{ "The cönfig.yml file" | slugify: 'latin' }}

the-config-yml-file

Data To JSON

Convert Hash or Array to JSON.

{{ site.data.projects | jsonify }}

Normalize Whitespace

Replace any occurrence of whitespace with a single space.

{{ "a \n b" | normalize_whitespace }}

Sort

Sort an array. Optional arguments for hashes: 1. property name 2. nils order (first or last).

{{ page.tags | sort }}

{{ site.posts | sort: 'author' }}

{{ site.pages | sort: 'title', 'last' }}

Sample

Pick a random value from an array. Optional: pick multiple values.

{{ site.pages | sample }}

{{ site.pages | sample:2 }}

To Integer

Convert a string or boolean to integer.

{{ some_var | to_integer }}

Array Filters

Push, pop, shift, and unshift elements from an Array.

These are NON-DESTRUCTIVE, i.e. they do not mutate the array, but rather make a copy and mutate that.

{{ page.tags | push: 'Spokane' }}

['Seattle', 'Tacoma', 'Spokane']

{{ page.tags | pop }}

['Seattle']

{{ page.tags | shift }}

['Tacoma']

{{ page.tags | unshift: "Olympia" }}

['Olympia', 'Seattle', 'Tacoma']

Inspect

Convert an object into its String representation for debugging.

{{ some_var | inspect }}

Options for the slugify filter

The slugify filter accepts an option, each specifying what to filter. The default is default. They are as follows (with what they filter):

Includes

If you have small page snippets that you want to include in multiple places on your site, save the snippets as include files and insert them where required, by using the include tag:

{% include footer.html %}

Jekyll expects all include files to be placed in an _includes directory at the root of your source directory. In the above example, this will embed the contents of _includes/footer.html into the calling file.

For more advanced information on using includes, see Includes.

Code snippet highlighting

Jekyll has built in support for syntax highlighting of over 60 languages thanks to Rouge. Rouge is the default highlighter in Jekyll 3 and above. To use it in Jekyll 2, set highlighter to rouge and ensure the rouge gem is installed properly.

Alternatively, you can use Pygments to highlight your code snippets. To use Pygments, you must have Python installed on your system, have the pygments.rb gem installed and set highlighter to pygments in your site’s configuration file. Pygments supports over 100 languages

To render a code block with syntax highlighting, surround your code as follows:

{% highlight ruby %}
def foo
  puts 'foo'
end
{% endhighlight %}

The argument to the highlight tag (ruby in the example above) is the language identifier. To find the appropriate identifier to use for the language you want to highlight, look for the “short name” on the Rouge wiki or the Pygments’ Lexers page.

Jekyll processes all Liquid filters in code blocks

If you are using a language that contains curly braces, you will likely need to place {% raw %} and {% endraw %} tags around your code.

Line numbers

There is a second argument to highlight called linenos that is optional. Including the linenos argument will force the highlighted code to include line numbers. For instance, the following code block would include line numbers next to each line:

{% highlight ruby linenos %}
def foo
  puts 'foo'
end
{% endhighlight %}

Stylesheets for syntax highlighting

In order for the highlighting to show up, you’ll need to include a highlighting stylesheet. For an example stylesheet you can look at syntax.css. These are the same styles as used by GitHub and you are free to use them for your own site. If you use linenos, you might want to include an additional CSS class definition for the .lineno class in syntax.css to distinguish the line numbers from the highlighted code.

To link to a post, a page, collection item, or file, the link tag will generate the correct permalink URL for the path you specify. For example, if you use the link tag to link to mypage.html, even if you change your permalink style to include the file extension or omit it, the URL formed by the link tag will always be valid.

You must include the file’s original extension when using the link tag. Here are some examples:

{{ site.baseurl }}{% link _collection/name-of-document.md %}
{{ site.baseurl }}{% link _posts/2016-07-26-name-of-post.md %}
{{ site.baseurl }}{% link news/index.html %}
{{ site.baseurl }}{% link /assets/files/doc.pdf %}

You can also use the link tag to create a link in Markdown as follows:

[Link to a document]({{ site.baseurl }}{% link _collection/name-of-document.md %})
[Link to a post]({{ site.baseurl }}{% link _posts/2016-07-26-name-of-post.md %})
[Link to a page]({{ site.baseurl }}{% link news/index.html %})
[Link to a file]({{ site.baseurl }}{% link /assets/files/doc.pdf %})

(Including {{ site.baseurl }} is optional — it depends on whether you want to preface the page URL with the baseurl value.)

The path to the post, page, or collection is defined as the path relative to the root directory (where your config file is) to the file, not the path from your existing page to the other page.

For example, suppose you’re creating a link in page_a.md (stored in pages/folder1/folder2) to page_b.md (stored in pages/folder1). Your path in the link would not be ../page_b.html. Instead, it would be /pages/folder1/page_b.md.

If you’re unsure of the path, add {{ page.path }} to the page and it will display the path.

One major benefit of using the link or post_url tag is link validation. If the link doesn’t exist, Jekyll won’t build your site. This is a good thing, as it will alert you to a broken link so you can fix it (rather than allowing you to build and deploy a site with broken links).

Note you cannot add filters to link tags. For example, you cannot append a string using Liquid filters, such as {% link mypage.html | append: "#section1" %} . To link to sections on a page, you will need to use regular HTML or Markdown linking techniques.

Linking to posts

If you want to include a link to a post on your site, the post_url tag will generate the correct permalink URL for the post you specify.

{{ site.baseurl }}{% post_url 2010-07-21-name-of-post %}

If you organize your posts in subdirectories, you need to include subdirectory path to the post:

{{ site.baseurl }}{% post_url /subdir/2010-07-21-name-of-post %}

There is no need to include the file extension when using the post_url tag.

You can also use this tag to create a link to a post in Markdown as follows:

[Name of Link]({{ site.baseurl }}{% post_url 2010-07-21-name-of-post %})

© 2008–2018 Tom Preston-Werner and Jekyll contributors
Licensed under the MIT license.
https://jekyllrb.com/docs/templates/