Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll: liquid tag inside javascript

Suppose I have two links: "all posts" and "personal." When the user clicks the "personal" link, he should only see the posts that have the category "personal." Right now, the liquid tag is {% for post in site.posts %}. I want to learn if there is a way to access the variable site.posts from javascript, so that I can listen to the click event and dynamically filter the post. If not, what should I do?

like image 341
Maximus S Avatar asked Jan 25 '15 22:01

Maximus S


1 Answers

You can make Jekyll parse any file by adding an empty front matter 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 site.posts objects collection in a json form and attribute them to you javascript posts var.

like image 173
David Jacquel Avatar answered Nov 17 '22 12:11

David Jacquel