1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
---
title: Templates
permalink: /docs/templates/
---
Jekyll uses the [Liquid](https://shopify.github.io/liquid/) templating language to
process templates. All of the standard Liquid [tags](https://shopify.github.io/liquid/tags/control-flow/) and
[filters](https://shopify.github.io/liquid/filters/abs/) 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
<div class="mobile-side-scroller">
<table>
<thead>
<tr>
<th>Description</th>
<th><span class="filter">Filter</span> and <span class="output">Output</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p class="name"><strong>Relative URL</strong></p>
<p>Prepend the <code>baseurl</code> value to the input. Useful if your site is hosted at a subpath rather than the root of the domain.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "/assets/style.css" | relative_url }}{% endraw %}</code>
</p>
<p>
<code class="output">/my-baseurl/assets/style.css</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Absolute URL</strong></p>
<p>Prepend the <code>url</code> and <code>baseurl</code> value to the input.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "/assets/style.css" | absolute_url }}{% endraw %}</code>
</p>
<p>
<code class="output">http://example.com/my-baseurl/assets/style.css</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to XML Schema</strong></p>
<p>Convert a Date into XML Schema (ISO 8601) format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_xmlschema }}{% endraw %}</code>
</p>
<p>
<code class="output">2008-11-07T13:07:54-08:00</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to RFC-822 Format</strong></p>
<p>Convert a Date into the RFC-822 format used for RSS feeds.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_rfc822 }}{% endraw %}</code>
</p>
<p>
<code class="output">Mon, 07 Nov 2008 13:07:54 -0800</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to String</strong></p>
<p>Convert a date to short format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_string }}{% endraw %}</code>
</p>
<p>
<code class="output">07 Nov 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to String in ordinal US style</strong></p>
<p>Format a date to ordinal, US, short format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_string: "ordinal", "US" }}{% endraw %}</code>
</p>
<p>
<code class="output">Nov 7th, 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to Long String</strong></p>
<p>Format a date to long format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_long_string }}{% endraw %}</code>
</p>
<p>
<code class="output">07 November 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to Long String in ordinal UK style</strong></p>
<p>Format a date to ordinal, UK, long format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_long_string: "ordinal" }}{% endraw %}</code>
</p>
<p>
<code class="output">7th November 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Where</strong></p>
<p>Select all the objects in an array where the key has the given value.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | where:"graduation_year","2014" }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Where Expression</strong></p>
<p>Select all the objects in an array where the expression is true. Jekyll v3.2.0 & later.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | where_exp:"item",
"item.graduation_year == 2014" }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.members | where_exp:"item",
"item.graduation_year < 2014" }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.members | where_exp:"item",
"item.projects contains 'foo'" }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Group By</strong></p>
<p>Group an array's items by a given property.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | group_by:"graduation_year" }}{% endraw %}</code>
</p>
<p>
<code class="output">[{"name"=>"2013", "items"=>[...]},
{"name"=>"2014", "items"=>[...]}]</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Group By Expression</strong></p>
<p>Group an array's items using a Liquid expression.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | group_by_exp:"item",
"item.graduation_year | truncate: 3, \"\"" }}{% endraw %}</code>
</p>
<p>
<code class="output">[{"name"=>"201...", "items"=>[...]},
{"name"=>"200...", "items"=>[...]}]</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>XML Escape</strong></p>
<p>Escape some text for use in XML.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.content | xml_escape }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>CGI Escape</strong></p>
<p>
CGI escape a string for use in a URL. Replaces any special characters
with appropriate <code>%XX</code> replacements. CGI escape normally replaces a space with a plus <code>+</code> sign.
</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "foo, bar; baz?" | cgi_escape }}{% endraw %}</code>
</p>
<p>
<code class="output">foo%2C+bar%3B+baz%3F</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>URI Escape</strong></p>
<p>
Percent encodes any special characters in a URI. URI escape normally replaces a space with <code>%20</code>. <a href="https://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters">Reserved characters</a> will not be escaped.
</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "http://foo.com/?q=foo, \bar?" | uri_escape }}{% endraw %}</code>
</p>
<p>
<code class="output">http://foo.com/?q=foo,%20%5Cbar?</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Number of Words</strong></p>
<p>Count the number of words in some text.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.content | number_of_words }}{% endraw %}</code>
</p>
<p>
<code class="output">1337</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Array to Sentence</strong></p>
<p>Convert an array into a sentence. Useful for listing tags. Optional argument for connector.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | array_to_sentence_string }}{% endraw %}</code>
</p>
<p>
<code class="output">foo, bar, and baz</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | array_to_sentence_string: 'or' }}{% endraw %}</code>
</p>
<p>
<code class="output">foo, bar, or baz</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Markdownify</strong></p>
<p>Convert a Markdown-formatted string into HTML.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.excerpt | markdownify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Smartify</strong></p>
<p>Convert "quotes" into “smart quotes.”</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.title | smartify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Converting Sass/SCSS</strong></p>
<p>Convert a Sass- or SCSS-formatted string into CSS.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ some_scss | scssify }}{% endraw %}</code>
<code class="filter">{% raw %}{{ some_sass | sassify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Slugify</strong></p>
<p>Convert a string into a lowercase URL "slug". See below for options.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "The _config.yml file" | slugify }}{% endraw %}</code>
</p>
<p>
<code class="output">the-config-yml-file</code>
</p>
<p>
<code class="filter">{% raw %}{{ "The _config.yml file" | slugify: 'pretty' }}{% endraw %}</code>
</p>
<p>
<code class="output">the-_config.yml-file</code>
</p>
<p>
<code class="filter">{% raw %}{{ "The _cönfig.yml file" | slugify: 'ascii' }}{% endraw %}</code>
</p>
<p>
<code class="output">the-c-nfig-yml-file</code>
</p>
<p>
<code class="filter">{% raw %}{{ "The cönfig.yml file" | slugify: 'latin' }}{% endraw %}</code>
</p>
<p>
<code class="output">the-config-yml-file</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Data To JSON</strong></p>
<p>Convert Hash or Array to JSON.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.data.projects | jsonify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Normalize Whitespace</strong></p>
<p>Replace any occurrence of whitespace with a single space.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "a \n b" | normalize_whitespace }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Sort</strong></p>
<p>Sort an array. Optional arguments for hashes: 1. property name 2. nils order (<em>first</em> or <em>last</em>).</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | sort }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.posts | sort: 'author' }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.pages | sort: 'title', 'last' }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Sample</strong></p>
<p>Pick a random value from an array. Optional: pick multiple values.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.pages | sample }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.pages | sample:2 }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>To Integer</strong></p>
<p>Convert a string or boolean to integer.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ some_var | to_integer }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Array Filters</strong></p>
<p>Push, pop, shift, and unshift elements from an Array.</p>
<p>These are <strong>NON-DESTRUCTIVE</strong>, i.e. they do not mutate the array, but rather make a copy and mutate that.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | push: 'Spokane' }}{% endraw %}</code>
</p>
<p>
<code class="output">['Seattle', 'Tacoma', 'Spokane']</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | pop }}{% endraw %}</code>
</p>
<p>
<code class="output">['Seattle']</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | shift }}{% endraw %}</code>
</p>
<p>
<code class="output">['Tacoma']</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | unshift: "Olympia" }}{% endraw %}</code>
</p>
<p>
<code class="output">['Olympia', 'Seattle', 'Tacoma']</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Inspect</strong></p>
<p>Convert an object into its String representation for debugging.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ some_var | inspect }}{% endraw %}</code>
</p>
</td>
</tr>
</tbody>
</table>
</div>
### 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):
- `none`: no characters
- `raw`: spaces
- `default`: spaces and non-alphanumeric characters
- `pretty`: spaces and non-alphanumeric characters except for `._~!$&'()+,;=@`
- `ascii`: spaces, non-alphanumeric, and non-ASCII characters
- `latin`: like `default`, except Latin characters are first transliterated (e.g. `àèïòü` to `aeiou`) {%- include docs_version_badge.html version="3.7.0" -%}
## Tags
* [Includes](#includes)
* [Code snippet highlighting](#code-snippet-highlighting)
* [Linking to pages, collections and posts (the new and improved way)](#links)
* [Linking to posts (the old way)](#linking-to-posts)
### 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:
{% raw %}
```liquid
{% include footer.html %}
```
{% endraw %}
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](../includes).
### Code snippet highlighting
Jekyll has built in support for syntax highlighting of over 60 languages
thanks to [Rouge](http://rouge.jneen.net). 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](http://pygments.org) 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](http://pygments.org/languages/)
To render a code block with syntax highlighting, surround your code as follows:
{% raw %}
```liquid
{% highlight ruby %}
def foo
puts 'foo'
end
{% endhighlight %}
```
{% endraw %}
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](https://github.com/jayferd/rouge/wiki/List-of-supported-languages-and-lexers)
or the [Pygments' Lexers page](http://pygments.org/docs/lexers/).
<div class="note info">
<h5>Jekyll processes all Liquid filters in code blocks</h5>
<p>If you are using a language that contains curly braces, you
will likely need to place <code>{% raw %}</code> and
<code>{% endraw %}</code> tags around your code.</p>
</div>
#### 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:
{% raw %}
```liquid
{% highlight ruby linenos %}
def foo
puts 'foo'
end
{% endhighlight %}
```
{% endraw %}
#### 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](https://github.com/mojombo/tpw/tree/master/css/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.
## Links
### Linking to pages {#link}
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:
{% raw %}
```liquid
{{ 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 %}
```
{% endraw %}
You can also use the `link` tag to create a link in Markdown as follows:
{% raw %}
```liquid
[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 %})
```
{% endraw %}
(Including `{% raw %}{{ site.baseurl }}{% endraw %}` 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 `{% raw %}{{ page.path }}{% endraw %}` 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 `{% raw %}{% link mypage.html | append: "#section1" %} {% endraw %}`. 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.
{% raw %}
```liquid
{{ site.baseurl }}{% post_url 2010-07-21-name-of-post %}
```
{% endraw %}
If you organize your posts in subdirectories, you need to include subdirectory path to the post:
{% raw %}
```liquid
{{ site.baseurl }}{% post_url /subdir/2010-07-21-name-of-post %}
```
{% endraw %}
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:
{% raw %}
```liquid
[Name of Link]({{ site.baseurl }}{% post_url 2010-07-21-name-of-post %})
```
{% endraw %}
|