twitter-bootstrapjekyllbootstrap-modaljekyll-bootstrap

How can I place multiple images in a modal on my Jekyll site?


I am finishing up building a website using a modified Bootstrap theme (specifically this theme) and Jekyll. This is my first time using Jekyll and things have been going well thus far up until this point which has me a stumped.

Just like the bootstrap theme I linked above I have a portfolio section that opens up a modal where I want to include a few photos and a paragraph's worth of text. So far I have the other elements of the modal post working the way I want them to, but can't figure out a way to add more than one image.

Here's the html for my portfolio modal

{% for post in site.posts %}
<div class="portfolio-modal modal fade" id="portfolioModal-{{     post.modal-id }}" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="close-modal" data-dismiss="modal">
            <div class="lr">
                <div class="rl">
                </div>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2">
                    <div class="modal-body">
                        <!-- Project Details Go Here -->
                        <h2>{{ post.title }}</h2>
                        <p class="item-intro text-muted"> {{   post.subheading }} </p>
                        <p>{{ post.description }}</p>
                        <img class="img-responsive" src="img/portfolio/{{ post.img }}">
                        <ul class="list-inline">
                            <li>Date: July 2014</li>
                            <li>Location: {{ post.location }} </li>
                            <li>Category: {{ post.category }} </li>
                        </ul>
                        <button type="button" class="btn btn-primary"  data-dismiss="modal"><i class="fa fa-times"></i> Close Project</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
{% endfor %}

Here's the front matter for my modal post

---
layout: post
modal-id: 1
title:  Post title
date:   2017-01-06
location: Project location
img: img.jpg
category: Post category
alt: image-alt
description: Post description
---

So, I'm not sure how to add more than one image per post. Any help is much appreciated!


Solution

  • Build a sequence/array/list of images and alt tags to replace img: & alt: in your Front Matter then loop over them. Nothing else to it.

    Example Sequence:

    images-array:
     - image: image-1.jpg
       alt: This is some alt text
     - image: image-2.jpg
       alt: This is some alt text
     - image: image-3.jpg
       alt: This is some alt text
    

    Updated Front Matter:

    ---
    layout: post
    modal-id: 1
    title: Post title
    date: 2017-01-06T00:00:00.000Z
    location: Project location
    category: Post category
    description: Post description
    images-array:
     - image: image-1.jpg
       alt: This is some alt text
     - image: image-2.jpg
       alt: This is some alt text
     - image: image-3.jpg
       alt: This is some alt text
    ---
    

    For Loop:

    {% for item in post.images-array %}
      <img class="img-responsive" src="img/portfolio/{{ item.image }}" alt="{{ item.alt }}">
    {% endfor %}
    

    For Loop using Bootstrap Columns:

    {% for post in site.posts %}
        <div class="portfolio-modal modal fade" id="portfolioModal-{{ post.modal-id }}" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog">
    
                <div class="modal-content">
                    <div class="close-modal" data-dismiss="modal">
                        <div class="lr">
                            <div class="rl"></div>
                        </div>
                    </div>
    
                    <div class="modal-body">
    
                        <h2>{{ post.title }}</h2>
    
                        <p class="item-intro text-muted"> {{ post.subheading }} </p>
    
                        <p>{{ post.description }}</p>
    
                        {% for item in post.images-array %}
                        <div class="col-sm-4">
                            <img class="img-responsive" src="img/portfolio/{{ item.image }}" alt="{{ item.alt }}">
                        </div>
                        {% endfor %}
    
                        <ul class="list-inline">
                            <li>Date: July 2014</li>
                            <li>Location: {{ post.location }} </li>
                            <li>Category: {{ post.category }} </li>
                        </ul>
    
                        <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-times"></i> Close Project</button>
                    </div>
    
                </div>
            </div>
        </div>
    {% endfor %}