Jekyll:JavaScript中的液体标记 [英] Jekyll: liquid tag inside javascript

查看:66
本文介绍了Jekyll:JavaScript中的液体标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个链接:所有帖子"和个人".当用户单击个人"链接时,他应该只看到具有个人"类别的帖子.现在,液体标签为{% for post in site.posts %}.我想了解是否有一种方法可以从javascript访问变量site.posts,以便我可以监听click事件并动态过滤帖子.如果没有,我该怎么办?

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?

推荐答案

您可以使Jekyll通过向其添加空的开头来解析任何文件.

You can make Jekyll parse any file by adding an empty front matter to it.

示例:assets/js/script.js

编辑16/07/28 :您可以使用jsonify 过滤器用于任何哈希或数组

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

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

旧答案

---
---
{% 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}}

这会将site.posts对象集合放置为json形式,并将其归因于您的JavaScript posts变量.

This will put the site.posts objects collection in a json form and attribute them to you javascript posts var.

这篇关于Jekyll:JavaScript中的液体标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆