Adding to site.tags from csv _data

I have a number of posts, which I’m now building from a data file.

Depending on the post, my template reads the data, and displays the content as expressed by the liquid templating.

Additionally, I have modified archive-single.html so that it displays both frontmatter tags along with the tags in my datafile.

now, for the final frontier, I want add, from my data file, to site.tags, so that on tags.html I can click the given tag and find which posts belong to it

any direction would be super, thanks!

For this I used ‘jekyll-tagging’: GitHub - pattex/jekyll-tagging: Jekyll plugin to automatically generate a tag cloud and tag pages.. It’s well explained in the readme. The result seen here: Blog (“All tags” on the right),

1 Like

this looks interesting, but I’m not sure it supports brining in tags from csv _data/

[edit] I may find some answers here:

1 Like

I am curious if you can explain why you are using csv data? While I am sure you have your use case, you could search through all available tags and provide links to posts. You can find code to do this here in the official Jekyll docs.

That said, I think what you want is to look at all the tags in a csv file and then display what posts contain those tags, correct? Below is an example

:page_facing_up: CSV data

Here is a basic data CSV data set containing the tags we care about. Since Jekyll treats any space between each tag in tags, create add a display-friendly label:
/_data/tag-data.csv

title,label
pets,Cute pets
wildanimals,Wild animals
fish,Fish and dolphins

:page_with_curl: Three sample posts

In my example, I created three posts in Jekyll, with YAML front matter, each using a variation of tags.

First post

/_posts/2023-04-03-first-post.markdown

---
layout: post
title:  "First post"
date:   2023-04-05
tags: fish wildanimals
---
Some content...

Second post

/_posts/2023-04-04-second-post.markdown

---
layout: post
title:  "Second post"
date:   2023-04-04
tags: pets fish
---
Some content...

Third post

/_posts/2023-04-05-third-post.markdown

---
layout: post
title:  "Third post"
date:   2023-04-05
tags: pets
---
Some content...

:technologist: Code

From here, we can read the list of tags from /_data/tag-data.csv and then list all the posts that contain a match.

/tags-from-csv.markdown

---
layout: default
---

## List of posts that match the tag-data.csv file
{%- assign tagData = site.data.tag-data -%}
{% for tag in tagData %}
<dl>
    <dt><strong>Tag: </strong>{{tag.label}} ({{tag.item}})</dt>
    {%- assign posts = site.posts | where_exp: "item", "item.tags contains tag.item" -%}
    {% for post in posts %}
        <dd>- <a href="{{ post.url | relative_url }}">{{ post.title }}</a></dd>
    {% endfor %}
</dl>
{% endfor %}

The output for the code will look something like this:

Hope this helps!

1 Like

my content is generated from csv data. For certain posts my template reads csv and extracts attributes [tags] (that fall under different headings) and puts them all together into post tags

eventually I will leave behind the front-matter tags, altogether.

these varying headings have different uses in different places. For example, each item listed in a post has its own attributes falling under different headings.

For now, they get lumped together into the page.tags but eventually, I want a separate page for each tag to list each item, and a link back to its main page.

to make this more visceral

archive-single from Categories.html

items from within that post

summary

so you can see here that I collect tags from various headings, whther that’s related (organizations), tech, or internet standard (as mentioned within that external link)

eventually I expect to move on from beyond jekyll as my build-times drag on and I push the limits of this SSG. For now, however, I have a working web-directory that I want to extend and have capacity for automating all manner of content creation \ archival \ broken links checking \ pulling in content previews from rss \ etcetera

thanks for your interest (live site: Decentralized-ID.com …for now only company pages are generated from csv)

Thank you. I think I understand a little better :-). It looks like you want all your content to be dynamic and easily pulled from a dataset so you can quickly build dynamic content.

Your site seems pretty complex compared to how Jekyll was designed (create the page or post with the content with some YAML). To build more dynamic and reusable content, you might want to consider Jekyll collections. That said, if you want to build most (or all) of your website content from data files, I agree that using a dynamic, database-driven approach is probably better than hacking Jekyll.

All that said did my previous response help to answer your question? If not, please be as specific as you can about what you are trying to accomplish, like the expected input and output.

1 Like

and, yes. this code does look like it can be useful for me, but not quite an answer for this particular issue.

[edit: didn’t see your reply until after I sent this]

yes, I have some experience building sites using Ruby Templating (ERB) and sqlite, which is SUPER FAST considering the amount of data stored…

I don’t know if ruby is really the best language for the task, but its sure the easiest to learn…

this site grew from a curated list (and ~0 coding knowledge) which evolved to a multitude of curated lists, and then I learned to use jekyll, and gradually I’ve extended the functionality.

The problem with collections is they don’t support tags at all, and aren’t included in rss… so… honestly jekyll collections are the worst part of jekyll to me. if they supported RSS and proper tagging, then sure it could be an option, but … its just a ton more work to try that.

IDK, rereading your code… I would have to play around with it to be sure… maybe it can help but I’m not a developer, so its difficult for me to tell just looking

You can use tags with Jekyll Collections. Here is an example from another post I worked on some time ago that lists breweries in a collection. I enhanced the code to use a tag (you can use tags, but doing this quickly). Using this sample may (or may not) inspire you.

Add a collection to the configuration

/_config.yml

collections:
  breweries:
    output: true

Create the _breweries folder and add content

/_breweries/anchor-steam.md

---
title: Anchor Steam Brewery
type: brewery
sub-type: na
location: San Francisco
tag: potrero
---
### {{page.title}}
__Famous brewery with a tasting room__
Learn the traditions of this famous brewery...

/_breweries/magnolia-brewery.md

---
title: Magnolia Brewery
type: brewery
sub-type: restaurant
location: San Francisco
tag: haight
---
### {{page.title}}
__Come for the beer stay for the fries__
Tucked away in a corner in the historic Haight Ashbury district there lies a gem of a brewery and restaurant....

Create a breweries folder and display content

/breweries/index.html

---
title: Breweries
layout: default
---
<h1>Display a single brewery based on a tag</h1>
</p>
{% assign breweries = site.breweries | where_exp: "item", "item.tag == 'haight'" %}
{% for brewery in breweries %}
    {{ brewery.content }}
{% endfor %}

</p>
<h1>Display all breweries</h1>
</p>
{% assign breweries = site.breweries %}
{% for brewery in breweries %}
    {{ brewery.content }}
{% endfor %}

Output

2 Likes