V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Jekyll
http://jekyllrb.com/
Jekyll on GitHub
https://github.com/mojombo/jekyll
Jekyll-bootstrap
http://jekyllbootstrap.com/
Octopress
http://octopress.org/
szhshp
V2EX  ›  Jekyll

Jekyll x Liquid: 文章置顶功能的实现

  •  
  •   szhshp · 2017-02-05 15:55:34 +08:00 · 4108 次点击
    这是一个创建于 2630 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天突然闲的蛋疼想把一些文章置顶, 于是就研究了一下如何实现, 还顺便把功能塞到了前段时间发布的- Jekyll 主题 里面

    两个方案:

    • Server 端实现
    • Client 端实现

    Server 端实现

    其实很简单, 一开始以为无法对 Post 进行操作, 后来发现可以从 post 的 header 里面进行参数设置

    layout: post
    title: 《 10101 》 EP0 :我太受欢迎了该怎么办
    category : Comic
    tags : [Comic, 10101]
    stickie: true
    

    然后 liquid 进行判断

    {* for post in site.posts *}
        {* if post.stickie != true *}
            {* continue *}
        {* endif *}
        <div>置顶文章细节</div>
    {* endfor *}
    
    {* for post in site.posts *}
        {* if post.stickie == true *}
            {* continue *}
        {* endif *}
        <div>普通文章细节</div>
    {* endfor *}
    

    当然这个可以进行多种扩展, 比如只输出特定类别的文章等等,这里就是个只输出带有 comic 这个 tag 的 post

    {% for post in site.posts %}
    
        {% assign isComic = 0 %}
    
        {% for tag in post.tags %}
            {% if tag == comic %}
               {% assign isComic = 1 %}
            {% endif %}
        {% endfor %}
    
        {% if isComic == 0 %}
          {% continue %}
        {% endif %}
    
    {% endfor %}
    

    Client 端实现

    我太懒了没去研究 o( ̄▽ ̄)o

    用 JQuery.sort()给 DOM 排序再渲染一下即可

    这个排序还要额外对时间进行比较 //果然懒一点是没错的 o( ̄▽ ̄)o

    演示页面

    szhshp 的第三边境研究所

    参考

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3168 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:52 · PVG 18:52 · LAX 03:52 · JFK 06:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.