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