Liquid Includes

GitHub Pages content is processed by Jekyll, which allows both parameterized file inclusion and the use of the liquid templating language. By combining these two features, macro-like functionality is possible.

Each section on this page illustrates liquid syntax that can be used to invoke macros provided by the programming pages theme.

collapsible_example.liquid

embeds a collapsed example that the user can expand if interested.

parameter description
file path to example file for embedding (must be rooted in _includes)
syntax (optional) hint for syntax hilighter; defaults to file extension
id_only (optional) when 'true' (string value), macro only evaluates to id of example label, e.g. to use in an href; defaults to empty string and normal behavior of embedding code example

markdown

{% include collapsible_example.liquid file='snippets/hello_world.cpp' %}


result

toggle example
hello_world.cpp

icon.liquid

inserts SVG code for an icon. SVG icons are defined in _data/icons/theme.yml by the theme. See the markdown sampler for a list of icon ids.

Tip
Users may also define icons of their own, by adding additional icon data files under _data/icons/. See the source comments for more details.

parameter description
id icon name from yaml data file
class (optional) css class string to apply to the embedded svg element in addition to icon

markdown

{% include icon.liquid id='star' %}


result

log.liquid

generates a javascript function that logs information to the console.
the function is submitted to jQuery’s document ready function ($()).

parameter description
lvl <log|info|warn|error> (optional, defaults to log; message level)
msg <string> (optional; message to print to console at specific level)
table [..] (optional; object to be logged as a table)
values [..] (optional; array of values to be logged individually at message level)
keys [..] (optional; array of labels to be combined with values and logged as a table)

markdown

{% include log.liquid msg='[programming-pages] hello, console' %}
{% include log.liquid msg='[programming-pages] warning, console' lvl='warn' %}
{% assign log_k = '' | split: '' %}
{% assign log_v = '' | split: '' %}
{% assign log_k = log_k | push: 'key 1' %}{% assign log_v = log_v | push: 'value one' %}
{% assign log_k = log_k | push: 'key 2' %}{% assign log_v = log_v | push: 'value two' %}
{% assign log_k = log_k | push: 'key 3' %}{% assign log_v = log_v | push: 'value three' %}
{% include log.liquid msg='[programming-pages] key/value pairs to table' keys=log_k values=log_v %}
(open the browser's javascript console to see the examples)


result

(open the browser’s javascript console to see the examples)

ordered_child_list.liquid

generates an html list of child pages, respecting their page.order value.
only first-level children are listed, grandchildren and beyond are ignored. order: 0 is first, -1 is last, undefined orders go in the middle alphabetically

parameter description
docs reference to list of Jekyll collection docs
page (optional) reference to scoping page, restricting the list to children of the page

markdown

{% include ordered_child_list.liquid docs=site.examples %}


result

package_shortener.liquid

abbreviates intermediary package names to their first letter:
foo.bar.bat.Bazfoo.b.b.Baz

parameter description
package fully qualified package name
threshold (optional) number of components to leave unabbreviated. defaults to 2

markdown

{% assign packages = "foo.bar.bat, foo.bar.bat.Baz, foo.Qix" | split: ", " %}
{% for pkg in packages %}
- {% include package_shortener.liquid package=pkg %}
{% endfor %}


result

  • foo.b.bat

  • foo.b.b.Baz

  • foo.Qix

page_root.liquid

extracts the first component of a page url:
/examples/includes//examples

parameter description
page reference to a Jekyll page object to extract the url root from

markdown

{% include page_root.liquid page=page %}


result

/examples

render_indices.liquid

creates the individual links in the sidebar.
links are indented when parented, unless their page layout has been excluded with no_indent

Note
This include is made available for override, so that a site can customize how (or if) it renders the items in the side nav.

parameter description
doc_list reference to list of Jekyll collection docs to generate indices for
collection_label name of indices tab, to ensure links keep proper tab open
page_title title of the current page, to render matching indices as ‘active’
no_indent pipe (|) delimited list of layouts that should not be indented

markdown

<div class="ui link list">
{% assign collection = site.collections | where:"title","Examples" | first %}
{% include render_indices.liquid doc_list=collection.docs collection_label=collection.label page_title=page.title %}
</div>


result

programming pages 2020 pixeldroid
https://github.com/pixeldroid/programming-pages
programming pages theme v0.5.22 (https://github.com/pixeldroid/programming-pages)