Liquid Extensions

Heliosmaster edited this page Mar 6, 2012 · 17 revisions

Jekyll uses Liquid to process templates. Along with the standard liquid tags and filters, Jekyll adds a few of its own:

Filters

Date to XML Schema

Convert a Time into XML Schema format.

{{ site.time | date_to_xmlschema }} => 2008-11-17T13:07:54-08:00

Date to String

Convert a date in short format, e.g. “27 Jan 2011”.

{{ site.time | date_to_string }} => 17 Nov 2008

Date to Long String

Format a date in long format e.g. “27 January 2011”.

{{ site.time | date_to_long_string }} => 17 November 2008

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.

{{ "foo,bar;baz?" | cgi_escape }} => foo%2Cbar%3Bbaz%3F

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.

{{ page.tags | array_to_sentence_string }} => foo, bar, and baz

Textilize

Convert a Textile-formatted string into HTML, formatted via RedCloth

{{ page.excerpt | textilize }}

Markdownify

Convert a Markdown-formatted string into HTML.

{{ page.excerpt | markdownify }}

Tags

Include

If you have small page fragments that you wish to include in multiple places
on your site, you can use the include tag.

{% include sig.textile %}

Jekyll expects all include files to be placed in an _includes directory at the root of your source dir. So this will embed the contents of /path/to/proto/site/_includes/sig.textile into the calling file.

Code Highlighting

Jekyll has built in support for syntax highlighting of over 100 languages via Pygments. In order to take advantage of this you’ll need to have Pygments installed, and the pygmentize binary must be in your path. When you run Jekyll, make sure you run it with Pygments support

To denote a code block that should be highlighted:

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

The argument to highlight is the language identifier. To find the appropriate identifier to use for your favorite language, look for the “short name” on the Lexers page.

Line number

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 %}

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 lineno in syntax.css to distinguish the line numbers from the highlighted code.

Post Url

If you would like to include a link to a post on your site, you can use the post_url tag.

{% post_url 2010-07-21-name-of-post %}

There is no need to include the extension.

To create a link, do:

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