Passing a category name to template in _includes through include.content

In my _layouts folder, I have a frontpage layout that looks like this:

---
layout: default
---

{% include header.html %}

{% include timeline.html content="cat" %}

{% include footer.html %}

where timeline.html is some code that renders the timeline.

timeline.html looks like below, more or less:

{% capture catname %}site.{{ include.content }}{% endcapture %}

<h1 id="timeline">{{ include.content }}</h1>
    <ul class="timeline"> 

      {% for item in {{ catname }} %}
      
      <li>
      <h4>{{ item.title }}</h4>
            <div class="timeline-body">
              <p> {{ item.excerpt }} </p>
            </div>
       </li>

      {% endfor %}
    </ul>

Thereā€™s also a category called cat, in a folder called _cat, and thereā€™s a bunch of posts with dates and titles and all that. Itā€™s defined in _config.yml as follows:

collections:
  cat:
    collections_dir: cat
    permalink: /:collection/:title

Nothing appears on webpage. Is what I want even possible? Iā€™d rather not have separate timeline.html files in _includes for every categoryā€¦

not exactly clear what you are doing - looks like you pass the category name into the include, and then want a list of all posts that have that category?

in your capture I think you need to get site.categories.cat - I would hard code that first to see if you get what you want, then work on passing it into the include.

Keep in mind that site.categories only works on posts - and posts are collections, but I am not sure the way you are doing your defaults will work? as I think the collection name is posts not cat.

In one of my themeā€™s I pass a category (or tag) name into an include to output posts within that taxonomyā€¦ so itā€™s possible.

This is how I do it:

Use a variable in front matter that can be defined in a page. For example if Iā€™m building a category page for postsā€™ categorized under foo Iā€™d add this:

---
taxonomy: foo
---

Then that gets assigned in an include:

{% include items.html taxonomy=page.taxonomy %}

Then in the items include that variable can be accessed like this to do your usual for loop stuff:

{% for item in site.categories[include.taxonomy] %}
 ... 
{% endfor %}

To do the same thing, but with a tag youā€™d iterate over site.tags[include.taxonomy] instead.

1 Like

would that work for image galleries? post = image, blog html has a grid, images of category foo get listed. - would I reassemble the blog html everytime or should I have blogcategory1 and so on html?

I have images - all the exact same size / ratio. So I want to use the post mechanism to output a gallery. The post .md has an image link, the css presents it nicely and the grid would be 3 columns max folding down to 1 on mobile. And then assemble the images according to taxonomy or search (tags, categories ā€¦). Could that work?

and every post.md would have an image link so I could just push up a new image ā€˜blog postā€™ and it gets included. overlay with jsā€¦

If you want a gallery of images thatā€™s something else and would require you writing some custom code.

What I referenced above is assuming each ā€œimageā€ is a post or collection document.

If you want a gallery of images thereā€™s a helper in the theme. But it doesnā€™t have any sort of logic to pull in images from a certain category or anything.

well I only need the logic to pull in posts - nevermind that it is an image.
[image] plus the front matter tagging, categorizing it ā€¦ or am I misunderstanding how it works?

I would have the image name and a date in the postfilename which is not really used because they get assembled/grouped by tags (taxonomyā€¦)

thank you for the link - reading up :slight_smile:

I am assuming of course that I can ā€˜designā€™ the way posts are displayed as I want with cssā€¦

yes, that splash page you made is what I want - just needs to be responsive so that the images are single column on a phone. perfect.

like this maybe:
https://dev-notes.eu/2016/01/images-in-kramdown-jekyll/

The logic to pull in posts is above.

This is a basic for loop that iterates over ā€œall postsā€.

{% for item in site.posts %}
  ... do stuff
{% endfor %}

The following would iterate over posts categorized fooā€¦ youā€™d just change that to whatever tag or category youā€™d want.

{% for item in site.posts.categories.foo %}
  ... do stuff
{% endfor %}

or

{% for item in site.posts.tags.foo %}
  ... do stuff
{% endfor %}
1 Like

so, I would have all posts in _posts and then three links ā€˜lakeā€™ ā€˜treeā€™ ā€˜frogā€™ where each link would pull in the same blog with the appropriate for loop. that would be nice and simple :slight_smile:

I only have 4 categories so that could work - but the tag collections would maybe have 10 different ā€˜directionsā€™. But this looks good - I will report back on how it worked out. Thank you @mmistakes

ā€¦ and it seems that I can have multiple blogs in one jekyll site too. excellent.

that looks promising ā€¦
ā€¦ confidence is the feeling I have before I understand the situation