jekyll/site/docs/templates.md

8.4 KiB
Raw Blame History

layout title prev_section next_section permalink
docs Templates migrations permalinks /docs/templates/

Jekyll uses the Liquid templating language to process templates. All of the standard Liquid tags and filters are supported. Jekyll even adds a few handy filters and tags of its own to make common tasks easier.

Filters

Description Filter and Output

Date to XML Schema

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

{% raw %}{{ site.time | date_to_xmlschema }}{% endraw %}

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

Date to RFC-822 Format

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

{% raw %}{{ site.time | date_to_rfc822 }}{% endraw %}

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

Date to String

Convert a date to short format.

{% raw %}{{ site.time | date_to_string }}{% endraw %}

17 Nov 2008

Date to Long String

Format a date to long format.

{% raw %}{{ site.time | date_to_long_string }}{% endraw %}

17 November 2008

XML Escape

Escape some text for use in XML.

{% raw %}{{ page.content | xml_escape }}{% endraw %}

CGI Escape

CGI escape a string for use in a URL. Replaces any special characters with appropriate %XX replacements.

{% raw %}{{ “foo,bar;baz?” | cgi_escape }}{% endraw %}

foo%2Cbar%3Bbaz%3F

URI Escape

URI escape a string.

{% raw %}{{ “'foo, bar \\baz?'” | uri_escape }}{% endraw %}

foo,%20bar%20%5Cbaz?

Number of Words

Count the number of words in some text.

{% raw %}{{ page.content | number_of_words }}{% endraw %}

1337

Array to Sentence

Convert an array into a sentence. Useful for listing tags.

{% raw %}{{ page.tags | array_to_sentence_string }}{% endraw %}

foo, bar, and baz

Textilize

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

{% raw %}{{ page.excerpt | textilize }}{% endraw %}

Markdownify

Convert a Markdown-formatted string into HTML.

{% raw %}{{ page.excerpt | markdownify }}{% endraw %}

Tags

Includes

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

{% highlight ruby %} {% raw %}{% include footer.html %}{% endraw %} {% endhighlight %}

Jekyll expects all include files to be placed in an _includes directory at the root of your source directory. This will embed the contents of <source>/_includes/footer.html into the calling file.

You can also pass parameters to an include:

{% highlight ruby %} {% raw %}{% include footer.html param="value" %}{% endraw %} {% endhighlight %}

These parameters are available via Liquid in the include:

{% highlight ruby %} {% raw %}{{ include.param }}{% endraw %} {% endhighlight %}

Code snippet highlighting

Jekyll has built in support for syntax highlighting of over 100 languages thanks to Pygments. To use Pygments, you must have Python installed on your system and set pygments to true in your site's configuration file.

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

{% highlight text %} {% raw %} {% highlight ruby %} def foo puts 'foo' end {% endhighlight %} {% endraw %} {% 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 Lexers page.

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 text %} {% raw %} {% highlight ruby linenos %} def foo puts 'foo' end {% endhighlight %} {% endraw %} {% endhighlight %}

Stylesheets for syntax highlighting

In order for the highlighting to show up, youll 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.

Post URL

If you would like 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.

{% highlight text %} {% raw %} {% post_url 2010-07-21-name-of-post %} {% endraw %} {% endhighlight %}

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:

{% highlight text %} {% raw %} [Name of Link]({% post_url 2010-07-21-name-of-post %}) {% endraw %} {% endhighlight %}

Gist

Use the gist tag to easily embed a GitHub Gist onto your site:

{% highlight text %} {% raw %} {% gist 5555251 %} {% endraw %} {% endhighlight %}

You may also optionally specify the filename in the gist to display:

{% highlight text %} {% raw %} {% gist 5555251 result.md %} {% endraw %} {% endhighlight %}

The gist tag also works with private gists, which require the gist owner's github username:

{% highlight text %} {% raw %} {% gist parkr/931c1c8d465a04042403 %} {% endraw %} {% endhighlight %}

The private gist syntax also supports filenames.