Jekyll: liquid tag inside javascript

Suppose I have two links: "all messages" and "personal". When a user clicks on a “personal” link, he should see only messages that have the category “personal”. The fluid tag is currently {% for post in site.posts %} . I want to find out if there is a way to access the site.posts variable from javascript so that I can listen to the click event and dynamically filter the message. If not, what should I do?

+8
javascript jekyll liquid
source share
1 answer

You can get Jekyll to parse any file by adding an empty foreground element to it.

example: assets / js / script.js

Edit 16/07/28 : you can use jsonify filter for any hash or array

 --- --- {{ site.posts | jsonify }} 

Old answer

 --- --- {% capture posts %} [ {% for post in site.posts %} { "title" : "{{ post.title }}", "url" : "{{ post.url }}", "date" : "{{ post.date | date: "%B %d, %Y" }}", "content" : "{{ post.content | escape }}" } {% if forloop.last %}{% else %},{% endif %} {% endfor %} ] {% endcapture %} var posts = {{posts | strip_newlines}} 

This will put the collection of site.posts objects in the json form and assign you javascript posts var.

+14
source share

All Articles