Jen*_*son 8 html include liquid jekyll
TL; DR:我可以说某种方式为{% include %}一次生成内容而只是在多个地方标记它而不必在每个位置重新生成它吗?
我正在与Jekyll建立一个相当大的文档站点,现在它上面有50多篇文章.它有一个侧边栏,列出所有文章.侧边栏是在单独的sidebar.html中构建的,然后它{% include sidebar.html %}在default.html中包含在网站的每个页面中.
我遇到的问题是每一篇文章都分别运行sidebar.html的生成,所以我对这段代码有50多代传递.我添加的每篇文章都添加了另一个传递,并使所有传递速度变慢,因为生成侧边栏必须解析项目中的每一篇文章.
建造时间已从基本上零上升到超过100秒,如果我移除它{% include sidebar.html %}然后下降到5秒.当我得到所有文章时,我估计大约有100-200个文章.那么我将来会对所有文章进行版本控制,这意味着从长远来看,很容易就会有1000多篇文章.在这一点上,我不会感到惊讶,如果在一个文件中改变一个字母将采取类似一个小时再生文件jekyll serve和jekyll build.
我想要做的是在构建过程开始时构建sidebar.html,并在生成所述页面时将其标记到每个页面.这可能吗?
最快的方法。
移动_includes/sidebar.html到sidebar-template.html
添加这件事:
---
layout: null
permalink: sidebar-template.html
---
Run Code Online (Sandbox Code Playgroud)
创建一个 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
Run Code Online (Sandbox Code Playgroud)
只需运行rake,即可生成侧边栏包含。
小智 2
感谢 Ben Balter,现在有了更好的方法来做到这一点。
而不是: {% include yourtemplate.html %} 使用: {% include_cached yourtemplate.html %}
当用于需要构建一次的较大项目(例如站点层次结构)时,该项目将被缓存。对于跨页面不同的其他项目,您仍然需要像往常一样使用包含。
这里解释得很好: https ://github.com/benbalter/jekyll-include-cache
绝对减少了网站启动时间!