Help with an array, when split just isn't cutting it

I have an array of hashtags, which I suppose jekyll is reading as text, not as an array.

['StaySafe', 'digitalhealth', 'COVID19', 'selfsovereignidentity', 'HealthTech', 'TentoHealth']

if I use the split function, I get part the way there

  {% assign htags = row.Hashtags | split: "', '" %}
  {% for h in htags %}{{ h }} {% endfor %}

output: ['StaySafe digitalhealth COVID19 selfsovereignidentity HealthTech TentoHealth']

then I try getting fancy with variations of this type of thing and I get no output anymore.

  {% for h in htags %}{{ h | remove "['" | remove "']"}} {% endfor %}

next I might try throwing a ‘where’ clause, to only try removing where that character is found… but I’m done for the night, and figured asking here is a good step.

thanks for any suggestions.

First of all, where is this array of hastags defined?
If it is in the front matter or a data file, Jekyll will correctly read it as an array.

When in doubt, always use the inspect filter.
For example,

<pre>{{ row.Hashtags | inspect }}</pre>

If the output code is an array, use the for tag directly, else continue using split till you can define the data cleanly.

The hashtags are saved in a csv, along with other tweet data, that I’m trying to build into a page.

Thank you! I will try again with a for loop.

Pretty new to wrangling liquid, but I couldn’t get them to print individually until I used the split.

There is a Liquid syntax error. Liquid filters require a colon (:) before their arguments, so the remove filters should have a colon, i.e.: remove: "..."

By default, Jekyll only outputs warnings for Liquid syntax errors, which can easily go unnoticed. In _config.yml, you can configure Liquid to be more strict, and halt the build when there are syntax errors or filter typos:

liquid:
  error_mode: strict
  strict_filters: true
1 Like

Could you post the first four rows from the csv file so that I get an idea about the data structure?

I got it working adding a : to my remove argument, (thanks @chuckhoupt), like so:

  {% assign htags = row.Hashtags | split: "', '" %}
  {% for h in htags %}<a href="https://twitter.com/hahstag/{{ h | remove: "['" | remove: "']" }}">#{{ h | remove: "['" | remove: "']" }}</a> {% endfor %}

then, to test out your idea earlier that jekyll should recognize it’s an array without doing all this, I tried like you said:

  {% for h in row.Hashtags %}<a href="https://twitter.com/hahstag/{{ h }}">#{{ h }}</a>  {% endfor %}

but that brought me back to the blank output.

Here’s a few rows if you’re still curious.

Time,Link,Text,Hashtags,User,Following,Followers,Retweets,Urls,ImageUrls,ReplyURL
2020-10-27 08:29:56,https://twitter.com/NilimaP/status/1321006201346367488,#StaySafe and secure #digitalhealth solutions will help us all keep moving  as we navigate through #COVID19 and any other pandemics that come our way. Let’s be in control of our own data.. or should I say destiny?  #selfsovereignidentity #HealthTech #TentoHealth @tento_health https://t.co/dSH1AyS4p8,"['StaySafe', 'digitalhealth', 'COVID19', 'selfsovereignidentity', 'HealthTech', 'TentoHealth']",NilimaP,1264,998,0,['https://twitter.com/healthcareguru/status/1320651072831012864'],[],
2020-10-26 15:41:24,https://twitter.com/debimr75/status/1320752395543040000,"1st issued in 450 BC as “safe travel” letter by King Artaxerxes Persia With no standard way of issuing holding &amp; verifying digital #credentials was ""A Chasm of Chaos"" #SelfSovereignIdentity is coined for secure controllable &amp; portable #digitalidentity #SSI https://t.co/RmekEfZB1j https://t.co/P2GLSYZrmy","['credentials', 'SelfSovereignIdentity', 'digitalidentity', 'SSI']",debimr75,454,3056,0,['https://www.evernym.com/blog/inevitable-return-self-sovereign-identity/'],['https://pbs.twimg.com/media/ElRAABxXYAIBqwc.jpg'],
2020-10-25 18:29:29,https://twitter.com/dwebchap/status/1320432308508336128,"By Decentralized Identity user own their data and identity, selectively disclose only relevant information. Checkout @RubixChain project on building the identity layer for the internet  https://t.co/Ql3nRV8aTj #selfsovereignidentity #BYOI","['selfsovereignidentity', 'BYOI']",dwebchap,90,21,0,['https://github.com/rubixchain/rubixdid'],[],https://twitter.com/1119701484373823488/1320428520246550535

Thanks for posting the data sample.
The reason Jekyll is parsing the hashtags as a String instead of an Array is because CSV cell values do not have the concept of Arrays. The values are either strings or numbers:

...,"['StaySafe', 'digitalhealth', 'COVID19', 'selfsovereignidentity', 'HealthTech', 'TentoHealth']",...

If you have control over the generated CSV, I’d suggest you to format above as:

...,"StaySafe, digitalhealth, COVID19, selfsovereignidentity, HealthTech, TentoHealth",...

Then you can simplify your Liquid template to just split the string:

{% assign htags = row.Hashtags | split: ', ' %}
{% for h in htags %}{{ h }} {% endfor %}

Note how the string to split filter is simpler as well…

1 Like

yes, I see. That probably comes down to how I declared the variable in python (which I’m also new at).

for arrays:
variable = []

for strings:
variable =""

I’m not sure that convention is necessary, I could use strings and add the comma myself to make jekyll life easier.

Thank you!

It sounds like you are just getting used to using site data in Jekyll and are probably where I was at about 2 months ago :slight_smile:.

If you use an array of variables as site data, I suggest you use YAML or JSON. I do not know Python, but found this article that looks legit enough, and it says JSON is native to Python, so you might want to switch to that from CSV if at all possible:

I created a 3-part video series on everything I learned about using Jekyll data, so you might find them helpful, and I link to them below. If you are in a rush, jump to the third video, but I suggest you watch all three if you have the time.

Here are the videos and I hope you find them useful! I use the words “one-to-many” vs. array, but I think the code still achieves what you are looking to do – or at least points you in the right direction.

This slides-only video gives you a broad overview of how data works in Jekyll and provides some guidance and what to use when:

This video shows you how to use data already provided to you by Jekyll:

This is probably the video you want most because it walks you through how to create and use each data type Jekyll supports, such as CSV, YML, etc.:

Here is the GitHub repo, if you want the code:

Here is the site running on GitHub pages so you can see the code’s output:

1 Like

hey, thanks! I have a webpage where I keep track of everything for learning github pages that’s sorely in need of an update!

I’ll be sure to add your videos there. When I first started using Jekyll the data\liquid side was beyond me… glad to be finally learning this stuff, lots of possibilities.

[edit]

your help and some other help from this forum has inspired me to sharpen up that page, since I didn’t really understand gh-pages or jekyll when I was creating it.

Your videos are a wonderful addition to this page, @BillRaymond. Thank you!

Python does just fine with Yaml btw, which is really simple to write by hand, compared to JSON where I have to squint the whole time trying to make the structures properly. Thanks for the link on python\json though, I am definitely working with JSON regularly, like it or not :smiley:

Excellent! Glad to help! Yes I went into it only knowing json and xml with YAML confusing me. Now I’m a huge fan on the simplicity YAML provides.