首页 文章

Jekyll:生成一次包含并将其包含在所有页面中

提问于
浏览
8

TL; DR:我可以说某种方式为 {% include %} 生成一次内容并将其标记在多个地方,而不必在每个位置重新生成它吗?

我正在与Jekyll Build 一个相当大的文档站点,现在它上面有50多篇文章 . 它有一个侧边栏,列出所有文章 . 侧边栏是在一个单独的sidebar.html中构建的,然后它被包含在网站上的每个页面中,默认情况下为 {% include sidebar.html %} .

我遇到的问题是每一篇文章都分别运行sidebar.html的生成,所以我对这段代码有50多代传递 . 我添加的每篇文章都添加了另一个传递,并使所有传递速度变慢,因为生成侧边栏必须解析项目中的每一篇文章 .

构建时间已从基本上零上升到超过100秒,如果我删除 {% include sidebar.html %} 则会下降到5秒 . 当我收到所有文章时,如果在一个文件中更改一个字母会花费一个小时来重新生成 jekyll servejekyll build 中的文件,那么我会感到惊讶 .

我想要做的是在构建过程开始时构建sidebar.html,并在生成所述页面时将其标记到每个页面 . 这可能吗?

2 回答

  • 4

    现在有了更好的方法,感谢Ben Balter .

    使用:{% include yourtemplate.html%}而不是:

    当用于需要构建一次的较大项目(例如站点层次结构)时,将缓存该项目 . 对于不同页面的其他项目,您仍然希望像往常一样使用包含 .

    这里解释得很好:https://github.com/benbalter/jekyll-include-cache

    绝对减少网站启动时间!

  • 1

    最快的方式来做到这一点 .

    _includes/sidebar.html 移至 sidebar-template.html

    添加这个前面的事情:

    ---
    layout: null
    permalink: sidebar-template.html
    ---
    

    创建 Rakefile

    TPL = "_site/sidebar-template.html"
    TST = "_includes/sidebar.html"
    
    task :default => :nav
    
    desc "Generates sidebar then copy it to be used as an include"
    task :nav do
    
      if !File.exist?(TST)
        puts "Creating dummy #{TST} file"
        open(TST, 'w') do |f|
          f.puts warning
        end
      end
    
      puts "Building Jekyll 1st run"
      system "jekyll build --trace"
    
      # delete target file (TST) if exist
      if File.exist?(TST)
          puts "#{TST} exists deleting it"
          rm TST
      end
    
      # copy generated file as an include
      cp(TPL, TST)
    
      puts "Building Jekyll AGAIN"
      system "jekyll build --trace"
    
      puts "task END"
    end
    

    只需运行 rake ,您就可以生成侧边栏了 .

相关问题