Jekyll - Get all folders in a directory and generate pages - jekyll

I have a folder structure like so:
/gallery/images/category1/
/gallery/images/category2/
/gallery/images/category3/
/gallery/images/category4/
/gallery/images/category5/
and so on..
Inside each of these folders, there are a bunch of images. However, these category folders are always changing, the names as well.
My goal is to have jekyll auto generate a seperate page for each on of these categories. Then in this page, cycle through each image in that folder and display it on the page.
What I need help on:
How do I look in the /gallery/images folder and grab all the folders
Once I know all the folders, how do you generate a page such as /gallery/[FOLDER_NAME].html for each one of them
Once I am able to do that, I know I can have the follow as the content of the page and be fine.
{% for image in site.static_files %}
{% if image.path contains 'gallery/{{ FOLDER_NAME }}' %}
<img src="{{ site.baseurl }}{{ image.path }}" />
{% endif %}
{% endfor %}
Any help or guidance would be greatly appreciated.

You can do this with javascript and without an extension.
Step 1. Put them on the screen using a page or layout.
<div id="cats"></div>
<div class="imagecontainer">
{% for image in site.static_files %}
{% if image.path contains 'gallery/' %}
{% assign imagepath = image.path | split: "/" %}
<img src="{{ site.baseurl }}{{ image.path }}" class="
{% for subpath in imagepath %}
{% if forloop.index0 == 3 %}{{ subpath }}{% endif %}
{% endfor %}
" />
{% endif %}
{% endfor %}
</div>
Step 2. Have javascript show/hide them one by one.
/* hide all images */
$('.imagecontainer img').hide();
/* define an array for the categories */
var cats = new Array();
/* fill the array with the categories */
$('.imagecontainer img').each(function() {
cats[$(this).attr('class')] = 1;
});
/* create a link for each category */
$.each(cats, function(cat, value) {
$('#cats').append(''+cat+'');
});
/* make the links toggle the right images */
$('.showcat').click(function() {
/* hide all images */
$('.imagecontainer img').hide();
/* show images in the clicked category */
$('.imagecontainer img.'+$(this).attr('cat')).show();
});
/* make images rotate */
$('.imagecontainer img').click(function() {
/* append the clicked image to its container */
$('.imagecontainer').append($(this));
});
Use this CSS:
div#cats {}
div.imagecontainer {}
img {position: absolute}
You can prevent them from loading in the first place by using data-src instead of src for the images and swap these attributes using javascript/jQuery.
Documentation on Liquid split: https://shopify.github.io/liquid/filters/split/

Related

How to show collections page list in second div, when click on collection name in first div ? (JEKYLL)

I am new to jekyll and have beginner experience in js. [Jekyll version: 4.2.0]
I have left navigation pane, center content, and right navigation pane.
on left nav I want to show the title of each collection, and when user clicks on it, right nav should show title of each page of that collection.
I'm able to show each collection's name in left nav with following code:
<div class="left_sidebar">
{% for coll in site.collections %}
{% if coll.label != "posts" %}
<a href="{{ coll.docs[0] | relative_url }}" class="col_link" >{{ coll.title }}</a>
{% endif %}
{% endfor %}
</div>
<div class="main">{{ content }} </div>
<div class="right_sidebar"></div>
but I'm not able to find a way to populate right navigation. Page should look something like this:
site_mockup
I tried scripting a bit but no luck so far:
<script>
$(function() {
$( '.col_link' ).on( 'click', function() {
$('.right_sidebar').load('{% include data.html %} #items');
});
});
</script>
_includes/data.html:
{% assign curr_coll = page.collection %}
{% for page in curr_coll.docs %}
{{ page.title }}
{% endfor %}

About generating responsive image gallery from any folder on Jekyll

I created collection for portfolio contents in my Jekyll and I am currently able to list all images from any folder with this guide. But there are image files named with #2x for displaying sharp images on high density displays like image1.png, image1#2x.png etc.
So how do I exclude #2x image files in gallery and put in srcset="" property in <img> tag for generating responsive image gallery.
This is my sample .md file:
---
layout: work-detail
---
{% include screenshot.html folder="/images-folder/" %}
This is include file for generating gallery:
<div class="screenshot screenshot--grid">
{% for file in site.static_files %}
{% if file.path contains include.folder %}
{% if file.extname == '.jpg' or
file.extname == '.jpeg' or
file.extname == '.JPG' or
file.extname == '.png' or
file.extname == '.PNG' or
file.extname == '.JPEG' %}
{% assign filenameparts = file.path | split: "/" %}
{% assign filename = filenameparts | last | replace: file.extname,"" %}
<img src="{{ file.path }}" alt="">
{% endif %}
{% endif %}
{% endfor %}
</div>
If they are all named properly/consistently you could do:
{% unless file.path contains "#2x" %}
{% assign filepath2x = file.path | replace: file.extname,"#2x" | append: file.extname %}
<img src="{{ file.path }}" srcset="{{ filepath2x }} 1000w" />
{% endunless %}
Note that this code should replace (just) the output of the image tag, so this part:
<img src="{{ file.path }}" alt="">
What this code does is:
It prevents outputting images with "#2x" in the filename (it skips them).
It creates a new variable called 'filepath2x'. This variable is a copy of the file.path, but with ".extension" (.jpg) replaced by "#2x.extension" (#2x.jpg)
It outputs the image tag with an extra srcset attribute and fills this attribute with this new variable.
I have used a random srcset setting (1000w). Please adjust this to fit your project.

Add class depending on image dimensions (Jekyll)

I'm trying to figure out if it's possible to add a specific class to an image in Jekyll depending on the image's dimensions. Basically I want to loop through a couple of images in a folder and add the correct class to the image depending on if the image is in landscape or portrait mode (i.e. if the width is bigger than the height and vice versa).
I've tried to do it with JavaScript, but I would prefer a method that would add the classes during the build process of the site instead of on page load.
This is the image loop:
<div class="model-gallery">
{% capture gallery_path %}pics/models/gallery/{{ page.title | slugify | replace: '-','' | replace: 'å','a' | replace: 'ä','a' | replace: 'ö','o' }}{% endcapture %}
{% for image in site.static_files %}
{% if image.path contains gallery_path %}
{{image.size}}
<img class="gallery-item" src="{{ site.baseurl }}{{ image.path }}" alt="{{page.title}}, {{site.title}}" />
{% endif %}
{% endfor %}
</div>
Any ideas?

Jekyll Liquid sub-navigation not properly selecting

I'm building a static website on gh-pages using jekyll liquid. I'm properly generating a simple, two-level navigation from a data file. My problem is that I am stuck trying to do two things:
Apply a "selected" class to the current page link item.
Apply an "open" class to the parent list item when a sublink menu is present.
Here's the format I'm using in my _data/nav.yml file
- title: Top Level Nav Item
url: level-1/
sublinks:
- title: Child Nav Item 1
url: child-1/
- title: Child Nav Item 2
url: child-2/
Here's how I'm building my navigation:
{% assign current_page = page.url | remove: 'index.html' %}
<ul class="-nav">
{% for nav in include.nav %}
{% assign current = null %}
{% if nav.url == current_page %}
{% assign current = ' _selected' %}
{% endif %}
{% if nav.url contains current_page %}
{% assign open = ' _open' %}
{% endif %}
<li class="-item{{ current }}{{ open }}">
{{ nav.title }}
{% if nav.sublinks and current_page contains nav.url %}
{% include navigation.html nav=nav.sublinks%}
{% endif %}
</li>
{% endfor %}
</ul>
Again this builds my navigation correctly, but it doesn't apply either the selected or open class.
Here's what I'm would like it to look like in the end:
What am I doing wrong?
Finally got this working. I solved it by adding an item to the front matter of my page called permalink where I specified the desired page permalink.
---
layout: default
title: Example
permalink: url/example/
---
I use this permalink to check against the current page.url to add the desired _selected class on the <li> element.
I made one modification to my example data file, adding in the parent url_part into the child's url. I'm not sure why, but I had trouble printing the entire URL correctly otherwise.
- title: Top Level Nav Item
url: level-1/
sublinks:
- title: Child Nav Item 1
url: level-1/child-1/
- title: Child Nav Item 2
url: level-1/child-2/
Lastly for my navigation.html include, here's how I'm creating my main menu, rendering sub-menus if they exist and should be shown, and properly selecting the active link:
{% assign current_page = page.url | remove: 'index.html' %}
<ul class="-nav">
{% for nav in include.nav %}
{% assign current = null %}
{% if nav.url == page.permalink %}
{% assign current = ' _selected' %}
{% endif %}
<li class="-item{{ current }}">
{{ nav.title }}
{% if nav.sublinks and current_page contains nav.url %}
{% include navigation.html nav=nav.sublinks%}
{% endif %}
</li>
{% endfor %}
</ul>
The big difference between this and the snippet I originally posted is I dropped the {{ open }} stuff for now. One problem at a time. The other thing is that I'm checking to see if nav.url equals page.permalink. Before I was checking against page.url and this always failed for me.
It's probably not the prettiest, but I finally got a jekyll liquid menu to generate (semi-)dynamically and properly select the active link.

Dynamically add and filter images in Jekyll for github pages?

I am trying out Jekyll to help someone who's not all that technical maintain their own static site. I would like to be able to have a images directory in the app's root /images containing images following a naming convention:
post_one_1.jpg, post_one_2.jpg, post_two_1.jpg, post_two_2.jpg ... etc.
I would then like for the user to create a post (post_one) and dynamically grab all of the images pertaining to that post from the images directory.
This plugin (https://gist.github.com/jgatjens/8925165) does almost exactly what I need, but isn't compatible with github pages.
Is there a solution in which I can hand the site off to a user and they would only need to add images to the image directory following the naming convention and then create a post and have access to the images?
Given you have a post file _posts/2015-05-28-post_one.md
From inside this post you have :
page.id = /2015/05/29/post_one
page.dir = /2015/05/29
In order to extract post_one whe do :
{% assign imgNameStart = page.id | remove: page.dir | remove: "/" %}
We now generate the base path we search for :
{% assign imgBasePath = imgNameStart | prepend: "/images/" %}
in this case it will be imgBasePath = "/images/post_one"
Loop over all our static files (files that are not pages or posts).
{% for img in site.static_files %}
And print images that have /images/post_one in their path like /images/post_one-01.jpg or /images/post_one-wathever-you-want.jpg
{% if img.path contains imgBasePath %}
<img src="{{ site.baseurl }}{{ img.path }}">
{% endif %}
{% endfor %}
All together :
{% assign imgNameStart = page.id | remove: page.dir | remove: "/" %}
{% assign imgBasePath = imgNameStart | prepend: "/images/" %}
{% for img in site.static_files %}
{% if img.path contains imgBasePath %}
<img src="{{ site.baseurl }}{{ img.path }}">
{% endif %}
{% endfor %}
Beware of code indentation if your post is a markdown file, four space indentation can be transformed to code snippet.