Jekyll Variables and Liquid template tags cheatsheet
Summary of all the Jekyll variables and liquid tags available.
General Liquid template tags
- jekyll.version
- Jekyll version
- jekyll.environment
- Jekyll environment
Example Output:
Version: {{jekyll.version}} #3.2.0.pre.beta1
Environment: {{jekyll.environment}} # development
Global Variables
- site.time
- The current time (when you run the jekyll command).
- site.pages
- A list of all Pages.
- site.posts
- A reverse chronological list of all Posts.
- site.related_posts
- If the page being processed is a Post, this contains a list of up to ten related Posts. By default, these are the ten most recent posts. For high quality but slow to compute results, run the jekyll command with the --lsi (latent semantic indexing) option. Also note GitHub Pages does not support the lsi option when generating sites.
- site.static_files
- A list of all static files (i.e. files not processed by Jekyll's converters or the Liquid renderer). Each file has three properties: path, modified_time and extname.
- site.html_pages
- A subset of
site.pages
listing those which end in.html
. - site.html_files
- A subset of
site.static_files
listing those which end in.html
. - site.collections
- A list of all the collections.
- site.data
- A list containing the data loaded from the YAML files located in the _data directory.
- site.documents
- A list of all the documents in every collection.
- site.categories.CATEGORY
- The list of all Posts in category CATEGORY.
- site.tags.TAG
- The list of all Posts with tag TAG.
- site.[CONFIGURATION_DATA]
- All the variables set via the command line and your _config.yml are available through the site variable. For example, if you have url: http://mysite.com in your configuration file, then in your Posts and Pages it will be stored in site.url. Jekyll does not parse changes to _config.yml in watch mode, you must restart Jekyll to see changes to variables.
Post
- page.title
- The title of the Page.
- page.date
- The Date assigned to the Post. This can be overridden in a Post’s front matter by specifying a new date/time in the format YYYY-MM-DD HH:MM:SS (assuming UTC), or YYYY-MM-DD HH:MM:SS +/-TTTT (to specify a time zone using an offset from UTC. e.g. 2008-12-14 10:30:00 +0900).
- page.categories
- The list of categories to which this post belongs. Categories are derived from the directory structure above the _posts directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code']. These can also be specified in the YAML Front Matter.
- page.tags
- The list of tags to which this post belongs. These can be specified in the YAML Front Matter.
- page.content
- The content of the Page, rendered or un-rendered depending upon what Liquid is being processed and what page is.
- page.excerpt
- The un-rendered excerpt of the Page.
- page.url
- The URL of the Post without the domain, but with a leading slash, e.g. /2008/12/14/my-post.html
- page.id
- An identifier unique to the Post (useful in RSS feeds). e.g. /2008/12/14/my-post
- page.path
- The path to the raw post or page. Example usage: Linking back to the page or post’s source on GitHub. This can be overridden in the YAML Front Matter.
- page.next
- The next post relative to the position of the current post in site.posts. Returns nil for the last entry.
- page.previous
- The previous post relative to the position of the current post in site.posts. Returns nil for the first entry.
Paginator
Paginator variables are available in index files only
index.{md,htm,html}
- paginator.per_page
- Number of Posts per page.
- paginator.posts
- Posts available for that page.
- paginator.total_posts
- Total number of Posts.
- paginator.total_pages
- Total number of pages.
- paginator.page
- The number of the current page.
- paginator.previous_page
- The number of the previous page.
- paginator.previous_page_path
- The path to the previous page.
- paginator.next_page
- The number of the next page.
- paginator.next_page_path
- The path to the next page.
{{site.config.safe}} {{site.config.exclude}}
Collections
Variables that belong to collections and also the ones defined in _config.yml
- label
- The name of your collection, e.g. my_collection.
- docs
- An array of documents.
- files
- An array of static files in the collection.
- relative_directory
- The path to the collection's source directory, relative to the site source.
- directory
- The full path to the collections's source directory.
- output
- Whether the collection's documents will be output as individual files.
Collection Document
Variables in a single collection Document
- content
- The (unrendered) content of the document. If no YAML Front Matter is provided, Jekyll will not generate the file in your collection. If YAML Front Matter is used, then this is all the contents of the file after the terminating ```---``` of the front matter.
- output
- The rendered output of the document, based on the content.
- path
- The full path to the document's source file.
- relative_path
- The path to the document's source file relative to the site source.
- url
- The URL of the rendered collection. The file is only written to the destination when the collection to which it belongs has output: true in the site's configuration.
- collection
- The name of the document's collection.
- date
- The date of the document's collection.
Common Code Snippets
List of posts
<ul>
{% for post in site.posts %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
List of collections
{% for collection in site.collections %}
<h4>Collection {{forloop.index}}</h4>
<a href="{{site.baseurl}}/{{collection.label}}">{{collection.label | camelcase}}</a>
<dl>
<dt>Docs</dt>
<dd>{% comment %}{{ collection.docs }}{% endcomment %}</dd>
<dt>Files</dt>
<dd>{{ collection.files }}</dd>
<dt>Relative directory</dt>
<dd>{{ collection.relative_directory }}</dd>
<dt>Directory</dt>
<dd>{{ collection.directory}}</dd>
<dt>Output</dt>
<dd>{{ collection.output }}</dd>
</dl>
<hr>
{% endfor %}
Example Output
Collection 1
books
Docs
Files
Relative directory
_books
Directory
/home/marcanuy/Development/simpleit.rocks/_books
Output
true
Collection 2
posts
Docs
Files
Relative directory
_posts
Directory
/home/marcanuy/Development/simpleit.rocks/_posts
Output
true
List of document attributes of a Collection
{% for collection in site.collections %}
<h4>Collection {{forloop.index}}</h4>
<dl>
{% for doc in collection.docs %}
<dt>Path</dt><dd>{{doc.path}}</dd>
<dt>Relative_path</dt><dd> {{doc.relative_path}}</dd>
<dt>Collection</dt><dd> {{doc.collection}}</dd>
<dt>Date</dt><dd> {{doc.date}}</dd>
{% endfor %}
</dl>
<hr>
{% endfor %}
Example Output
Path
docs/ruby/jekyll/templates/_posts/2016-07-13-jekyll-variables-and-liquid-template-tags-cheatsheet.md
Relative_path
docs/ruby/jekyll/templates/_posts/2016-07-13-jekyll-variables-and-liquid-template-tags-cheatsheet.md
Collection
posts
Date
2016-07-13 00:00:00 -0300
comments powered by Disqus
- Jekyll Variables and Liquid template tags cheatsheet
Articles
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·