Display all images from a directory on a Jekyll powered website - ruby

It doesn't necessarily have to be something Jekyll uses.
Basically I'm creating a gallery that will use lightbox. I want to load all the images from a directory (_site\images\gallery) for the lightbox to display and their thumbnails (to be determined and created).
What would be the best way to go about this? I already have lightbox set up and tested (no thumbnails).
Thanks in advance.

Jekyll doesn't have a way to "list the contents of a folder".
You can do a very approximate thing by using the yaml front though.
In the "gallery page", include a section with the file names of the images:
---
images:
- a.jpg
- b.jpg
- c.jpg
<other properties, like title, etc>
---
Then, when you want to list your images, produce the links with a loop. I'm not familiar with lighbox's syntax for images, but it will probably look like similar to this:
<ul class="something">
{% for image in page.images %}
<li class="something">
<a rel="something" class="something" href="/path/to/images/dir/{{ image }}" />
</li>
{% endfor %}
</ul>
(I have put "something" on every place when I'm not sure about something. It might be possible that you will have to remove some "somethings" completely. You will have to modify /path/to/images/dir/ to where your images are)
Once the html is changed to work with lightbox, the only thing you have to do to add a new image is: a) putting it in the images directory and b) Edit the gallery page, and introduce the new image name in the list.
It's not as convenient as having the list "automatically generated", but it's very close.

Listing the jpg files in the current directory in Jekyll can be done like this:
{% for file in site.static_files %}
{% assign pageurl = page.url | replace: 'index.html', '' %}
{% if file.path contains pageurl %}
{% if file.extname == '.jpg' or file.extname == '.jpeg' or file.extname == '.JPG' or file.extname == '.JPEG' %}
<img src="{{ file.path }}" />
{% endif %}
{% endif %}
{% endfor %}
More about this solution can be found here: http://jekyllrb.com/docs/static-files/. I have created a lightbox extension for Jekyll that is listed on my page Jekyll without plugins. Check it out!

Try https://github.com/simoarpe/azores-image-gallery
DISCLAIMER: I'm the author.
I've tried some of the projects already available on Github but most of them are discontinued or partially working and in the end, I decided to implement something on my own starting from the good bits found around.
The result is Azores Image Gallery.
For a more detailed explanation check the README file.

I love Jekyll plugins.
Try one of these: Jekyll Gallery, Jekyll Gallery Generator, or Folder Gallery.
Check out the Ruby file for each plugin and modify the image tag generated to include the class for lightbox. That should do the trick. Don't forget to include the lightbox css file in your default template page.

Related

What is the syntax to create a pdf link in _config.yml

I am using Jekyll to create a personal website. I am using the Beautiful-Jekyll template that is very popular. There is a navigation bar already defined in _config.yml. I want to make Resume link to my resume pdf.
I have accomplished that with this code (in _config.yml):
# List of links in the navigation bar
navbar-links:
Moi: "aboutme"
Projects:
- Hell Game: "http://deanattali.com/beautiful-jekyll/"
- Success City: "http://www.markdowntutorial.com/"
Resume: "Tech Resume.pdf"
However, this only opens the resume in the same tab. I want it to open in a separate tab. In HTML, you achieve this by using target="_blank". How do I do this in YAML?
I tried this but my index.html actually doesn't use links. I know, weird.
beautiful-jekyll/_includes/nav.html line 26:
{{ linkparts[0] }}
Change it into:
<a href="{{ linkparts[1] | relative_url }}"
{% if linkparts[1] contains '.pdf' %}target="_blank"{% endif %}
>{{ linkparts[0] }}</a>
Or use this (java)script/solution:
https://jekyllcodex.org/without-plugin/new-window-fix/

Passing a front matter variable to a Liquid::Block parameter

I am trying to make a jekyll blog with github-pages.
In order to get some pictures from a folder, I used this gist: https://gist.github.com/jgatjens/8925165
Now, I want to make it a little bit more flexible so that it would get a front matter variable ( page.folder ) and would return the images from there.
My problem is that whenever I assign a folder variable to the front matter and then pass it to the block like this:
{% loop_directory directory:page.folder iterator:image filter:*.jpg sort:descending %}
<div class="item">
<a class="content" href="{{ site.baseurl }}/{{ image }}" title="portfolio 2015">
<img src="{{ site.baseurl }}/{{ image }}"/>
</a>
</div>
{% endloop_directory %}
In the directory attribute it passes page.folder instead of, for example "images/portfolio" which is defined on the front matter. I am rather new at ruby so I can't find something refered to that problem. Is it a bug or something that I need to write correctly to pass the variable?
Did you tried:
{% loop_directory directory: {{page.folder}} iterator:image filter:*.jpg sort:descending %}
I had figured it using the context object to get the front matter attributes,
but unfortunately github / github-pages do not accept plugins (_plugins folder), so a day was wasted for that.
I resolved to github's solution, to create a _data folder and use a text-based (yaml) format to list all my extra assets in an organized way.

Jekyll, include markdown with layout in html

I have a website that consists out of a few 'slides'. Each has a fixed structure, used by some scripts, but variable content. I'm hosting it on github and am now trying to use Jekyll to make it easier to add new slides.
I already have each slide in a different html file, which I include in the main page: {% include_relative _slides/about.html %}. Now I'm trying to make it a markdown file, and I wanted to use front matter to make a layout that each slide's file could use. I can include a markdown file, and get it to render by doing:
{% capture myInclude %}{% include_relative _slides/test.md %}{% endcapture %}
{{ myInclude | markdownify }}
However, when I add a front-matter block to it with a layout defined in it, the layout doesn't get applied. It just gets rendered as a horizontal line (for the first ---) and then "layout: slide title: Test Slide —" in plain text.
Is there any way to fix this? Or perhaps a better way to break up my index.html and the slides in it?
Thanks a lot!
Note: Sorry if this was asked before, I Googled everything I could imagine it would be called.
I found something that works for me. I divided my template in two parts, the part above the content, and the part under it. Then in the file I include, there are two includes as well, one at the top and one at the bottom.
So my 'slide' files look like this:
{% include slide_start.html title="About" image="images/about.jpg" %}
... the content of the slide ...
{% include slide_end.html %}
As you can see, in the first include I give some parameters, these will be filled in and can be accessed with the liquid tags {{ include.something }}.
My slide_start.html looks like:
<div class="slide">
<div class="header">
<span>{{ include.title }}</span><!-- no whitespace
--><img src="{{ include.image }}" alt="{{ include.title }}"/>
</div>
<div class="content" markdown="1">
the slide_end.html is just two closing div tags.
You're trying to mix the page/post and the include strategies.
Page/post have a front matter and are decorated with a template, which can itself be decorated. `mypage.html -> layout: page -> layout: default.
Includes are included in page/post but they are only code parts. They cannot be decorated with a template.
You will have to choose.
Take a lool at https://github.com/shower/jekyller this can be helpfull.

Jekyll recursive dynamic include via parameter

On my blog that is powered by GitHub Pages, I'm trying to make it easy to include code snippets in my blog posts. For this, I have created code-snippet.html in the _includes-directory:
<pre><code>{% include {{include.file}} %}</code></pre>
To include a code snippet in a blog post, I use the following code:
Code snippet: {% include code-snippet.html file="snippets/MyPost/MySnippet.cs" %}
However, the content of the file _includes/snippets/MyPost/MySnippet.cs does not get included. If I output {{include.file}} in code-snippet.html I can see that the parameter is passed in correctly, but if I try to include it, I get nothing.
That's a good question. Here is my two cents.
Here, I took the example of your _posts/2012-11-16-idisposable.html post.
Storing code snippets
As code is part of the post's content I logically move it to the _posts folder. Storing it in the _includes folder is not so good as this is a template folder.
Let's copy the idisposable/TryFinallyDispose.cs.html from _includes/code-snippets to _posts/_code or anything you want.
As the code is stored in an underscored folder (_code), it will not be processed as is by Jekyll. We will just 'include' it in our posts.
Note : code snippet can be saved as code.cs
As we will use highligting from Jekyll dependancies (see below), we just rework code snippet by, removing <pre> and <code>.
It becomes :
var x = new X(); // X is a class that implements IDisposable
try
{
// do something with x
}
finally
{
x.Dispose();
}
Displaying code
Jekyll offers various ways to highlight code (markdown, pygments, rouge, ..). Here I demonstrate the pygments way, but it's up to you to explore other solutions.
The base template will be (_includes/code-snippet-csharp.html):
{% capture filePath %}_code/{{include.file}}{% endcapture %}
{% highlight csharp %}
{% include_relative {{filePath}} %}
{% endhighlight %}
note: one drawback here, in the {% highlight csharp %}, code name is hardcoded. I found no way to pass the language to the highlight tag as a variable. If you want to use multiple languages, you'll need to duplicate the include template as code-snippet-csharp.html, code-snippet-js.html, ...
Styling code
In order to beautify our code, we need an extra css file. The basic one is here, but there is many more.
Style is saved as assets/syntax.css and included in _layouts/default.hmtl :
<link rel="stylesheet" href="{{site.baseUrl}}assets/syntax.css">
And it can also be included in your sass process.
Including code in posts
We now just have to call our code in _posts/2012-11-16-idisposable.html.
{% include code-snippets/idisposable/TryFinallyDispose.cs.html %}
Is replaced by :
{% include code-snippet-csharp.html file="idisposable/TryFinallyDispose.cs.html" %}
Result
Better content structure and a nicely highlighted code !
You can include code like this
~~~
{% include snippets/MyPost/MySnippet.cs %}
~~~

Jekyll is ignoring Liquid tags within posts

I'm new to Jekyll, Liquid and Ruby as a whole, so sorry if I'm being really stupid. It seems that Jekyll is ignoring any liquid tags with posts (under the _posts directory), which results in output like the following. These posts are all formatted in markdown, and they all have YAML markup within them.
{% highlight scss %} .noisy { #include noise(#00f); } {% endhighlight %}
I've tested that it isn't just Pygments failing by adding {{ post.title }} which is also left unparsed. Check out the code on Github
I honestly don't know what I'm doing wrong.
Thanks in advance.
It seems I have figured out my problem. I was using {{ page.content }} in
my post.html instead of just {{ content }}.
Highlight in html issue with Jekyll/Liquid and pygments
It's page.title
Template Data · mojombo/jekyll Wiki
I'm not too sure why that isn't working. Does you're for loop work correctly in index.html?
This might be helpful: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers

Resources