Jekyll Liquid Template: Use Variable of Layout in Page - html

i have the following files:
_layouts/twoPointLayout.html
---
layout: documentationLayout
---
{% assign col = site.interfaceServer | where:"neededTopCollectionItem", page.topCollectionItem | sort: "order" %}
<ol class="breadcrumb">
<li>{{ col[0].title }}</li>
<li class="active">{{ page.title }}</li>
</ol>
<h3>{{ col[0].order }}.{{ page.order }} {{ page.title }}</h3>
{{ content }}
someFile.md
---
title: "Some title"
identifier: hints
order: 3
orderPagination: 24
layout: twoPointLayout
topCollectionItem: xyz
neededTopCollectionItem: xyz_hints
---
{% assign items = site.interfaceServer | where:"topCollectionItem", "xdt-documentation_hints" | sort: "order" %}
{{ page.col[0].order }} // This doesn`t work
{% for item in items %}
<h4 id="{{ item.identifier }}">{{ col[0].order }}.{{ page.order }}.{{ item.order }} {{ item.title }}</h4>
{% endfor %}
In the Layout twoPointLayout.html there is a variable declaration of col. Now i need to use this Variable with the same content in the page someFile.md.
I have used page.col[0].order to access the variable, but it doesn`t work.
How can I access the variable?

You can put that variable outside the layout and posts, and then include it where you need it:
Create an include in _includes/customdata.html:
{% assign col = site.interfaceServer | where:"neededTopCollectionItem", page.topCollectionItem | sort: "order" %}
Then include it in someFile.md:
{%include customdata.html %}
{{col}}
or in the layout.

Related

How to Sort Jekyll Post by Front Matter

how to sort posts by order in Jekyll liquid.
---
layout: post
title: my title
order: 1
---
From https://jekyllrb.com/docs/posts/
Sort
Sort an array. Optional arguments for hashes 1. property name 2. nils order (first or last).
Examples:
{{ page.tags | sort }}
{{ site.posts | sort: "author" }}
{{ site.pages | sort: "title", "last" }}
<ul>
{% for post in site.posts %}
<li>
{{ post.title }}
</li>
{% endfor %}
</ul>
From https://jekyllrb.com/docs/liquid/filters/:
{{ site.posts | sort: "author" }}
I think, this should do it:
<!-- or + last, see above -->
{% assign sorted_posts = site.posts | sort "order" %}
<ul>
{% for post in sorted_posts %}
<li>
{{ post.title }}
</li>
{% endfor %}
</ul>

Jekyll equivalent of Hugo's "with"

In Hugo, one can use with to avoid repeating variable names.
{{ with .Site.Params.foo }}
<p>{{ .bar }}</p>
{{ end }}
This is almost equivalent to
<p>{{ .Site.Params.foo.bar }}</p>
What would be its equivalent in Jekyll?
My try:
{%- assign tempvar = .Site.Params.foo -%}
<p>{{ tempvar.bar }}</p>
This should work:
{%- assign tempvar = site.foo -%}
<p>{{ tempvar.bar }}</p>
Based on your follow up comment to the question, it looks like you are seeking a way to check if a variable only contains a desired key:value pair. Here is one way to check if a variable contains only the desired key.
{% assign numOfKeys = page | size %}
{% assign sizeOfGold = page.gold | size %}
<!-- Print out the page object for debugging purposes -->
{{ page | inspect }}
{% if numOfKeys == 1 %}
{% if sizeOfGold > 0 %}
{% for x in page.gold %}
<!-- Logic -->
{% endfor %}
{% endif %}
{% endif %}

Listing Jekyll Collection pages by tags

I am trying to make a list of pages within a collection grouped by tags. I know this is easily possible with Jekyll's built-in site.tags feature, which I can (and have) achieve with something like:
<h3>Tags</h3>
{% for tag in site.tags %}
{% assign t = tag | first %}
{% assign posts = tag | last %}
<h4><a name="{{t | downcase | replace:" ","-" }}"></a><a class="internal" href="/tagged/#{{t | downcase | replace:" ","-" }}">{{ t | downcase }}</a></h4>
<ul>
{% for post in posts %}
{% if post.tags contains t %}
<li>
{{ post.title }}
<span class="date">{{ post.date | date: "%B %-d, %Y" }}</span>
</li>
{% endif %}
{% endfor %}
</ul>
<hr>
{% endfor %}
In order to get this:
I want to replicate the site.tags function in a Collection called note. Tags in my collections are grouped like they are for posts, using, e.g., tags: [urban growth, California, industrialization] in the YAML header. But I want to get this working with a Collection instead. I can almost achieve what I want with the following:
{% assign collection = site.note | group_by: "tags" | uniq %}
{% for group in collection %}
{% comment %} This is super hacky and I don't like it {% endcomment%}
<h3>{{ group.name | replace: '"', '' | replace: '[', '' | replace: ']', '' }}</h3>
<ul>
{% for item in group.items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{%endfor%}
But as you can probably see, this doesn't break tags out into their unique groups; instead, each set of tags in the YAML is treated as a single, large tag. Any pointers on constructing the array of unique tags and listing the Collection pages under them?
Try this :
{% assign tags = site.note | map: 'tags' | join: ',' | split: ',' | uniq %}
{% for tag in tags %}
<h3>{{ tag }}</h3>
<ul>
{% for note in site.note %}
{% if note.tags contains tag %}
<li>{{ note.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
The tags assignment method mentioned in David Jacquel's answer can be simplified as:
{% assign tags = site.note | map: 'tags' | uniq %}

Jekyll, Liquid - get posts belonging to category1 and category2

I produced an overview page for all posts from the category "Tutorials" like this:
<ul class="post-list">
{% for post in site.categories.Tutorials %}
<li>
<span class="post-meta">{{ post.date | date: "%b %-d, %Y" }}</span>
<h2>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</h2>
{{ post.excerpt }}
</li>
{% endfor %}
</ul>
But now I would like to produce an index page for posts with both category "Tutorials" and category "German". But how?
The post I would like to have in this overview page is this one:
---
layout: post
title: 'German tutorial'
categories: [Tutorials, German]
---
Posts I would not like to have in this overview page are posts with a header like this:
---
layout: "post"
title: "English totorial"
categories: [Tutorials, English]
---
I tried for example:
{% for post in site.categories.Tutorials & site.categories.German %}
but this doesn't work...
I easily would switch to tags instead of categories, if this makes it easier.
Get the first category array : site.categories.Tutorials then sort German category posts out of it :
{% assign tutorials = site.categories.Tutorials %}
{% comment %}Creates an empty array{% endcomment %}
{% assign germansTutos = "" | split: "/" %}
{% for p in tutorials %}
{% if p.categories contains "German" %}
{% assign germansTutos = germansTutos | push: p %}
{% endif %}
{% endfor %}
<ul>
{% for post in germansTutos %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
You can also simply assign a tutorial category and a lang variable to your posts and filter them by the power of the where filter.
eg:
---
layout: post
title: "Post 1"
date: 2016-01-27 00:29:55 +0100
categories: Tutorials
lang: ge
---
Post one
You can then sort your posts like this :
{% assign tutorials = site.categories.Tutorials %}
{% assign germanTutos = tutorials | where: 'lang', 'ge' %}
<ul>
{% for post in germanTutos %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>

Sorting/grouping collections for an archive

I am building my first site and am having a very difficult time grouping/sorting collections. The only success I have had is a list of files sorted alphabetically by subdirectory, then by files by their dates (seems to be the default) using:
<ul>
{% for page in site.collection_name | sort: weight %}
<h3>{{ page.title }}</h3>{{ page.category }}{{page.excerpt}}
{% endfor %}
</ul>
Weight (above) does not work. Neither did type, category, or any other variable I substituted.
My files are in subdirectories, and have permalinks as:
/collection_name/category_name/file_name/
Front matter includes:
title,
category,
layout,
type,
(tried several others)
The collection will have several file types such as:
articles,
videos,
research
What I want to accomplish is something that loops through my collection first for category, then by type. In very bad pseudo code:
<ul>
{% for page in site.{{category_name}} %}
<li><h2>{{category01}}</h2></li>
<ul>
{% for type in site.{{category_name.type}} | sort: date reverse%}
<li><h3>Articles</h3></li>
<li>{{ page.title }}{{page.excerpt}}</li>
<li><h3>Videos</h3></li>
<li>{{ page.title }}{{page.excerpt}}</li>
<li><h3>Research</h3></li>
<li>{{ page.title }}{{page.excerpt}}</li>
</uL>
<li><h2>{{category02}}</h2></li>
. . .
. . . {% endfor %}
Any help or direction will be appreciated.
Try to use group_by :
{% assign byCategory = site.collection_name | group_by: 'category' | sort: 'name' %}
{% for cat in byCategory %}
<h2>{{ cat.name | capitalize }}</h2>
{% assign byType = cat.items | group_by: 'type' %}
{% for type in byType %}
<h3>{{ type.name | capitalize }}</h3>
<ul>
{% for item in type.items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}
Note: this works with category: mycategory not with categories: [one, two]
If you want to specifically order types, you can do :
_config.yml
# this array determine types order
# a collection itm with non matching type will not be listed
types :
- articles
- videos
- research
code
{% assign byCategory = site.area | group_by: 'category' %}
{% for cat in byCategory %}
<h2>{{ cat.name }}</h2>
{% assign byType = cat.items | group_by: 'type' %}
{% for type in site.types %}
{% assign currentType = byType | where:"name", type | first %}
<h3>{{ currentType.name | capitalize }}</h3>
<ul>
{% for item in currentType.items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}