Jekyll: liquid tag inside javascript

后端 未结 1 461
走了就别回头了
走了就别回头了 2021-02-14 08:32

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

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 08:58

    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.

    0 讨论(0)
提交回复
热议问题