有时候我们可能会有一些奇奇怪怪的需求。比如某些内容只在站点首页显示,某些内容只在首页以外的页面显示,特别是我们用的是通用模板的时候处理起来就比较麻烦。
这种奇怪的需求可能比较场景的场景有:
- Blog 的友情链接只在首页显示,其他的页面不显示
- 某个位置的广告只在首页以外的页面显示,首页不显示
然而 Jekyll 使用的 Liquid 模板语言处理这类需求轻而易举。如果你在使用 Jekyll Liquid 可以通过以下方法实现。
使用 unless 设置首页以外的页面显示特定内容
1 | {% unless page.url == "/" or page.url == "/index.html" %} |
使用 if 设置首页以外的页面显示特定内容
1 | {% if page.url != "/" and page.url != "/index.html" %} |
如果你想让内容只在首页显示也很简单。
使用 unless 设置仅在首页显示特定内容
1 | {% unless page.url != "/" or page.url != "/index.html" %} |
使用 if 设置仅在首页显示特定内容
1 | {% if page.url == "/" and page.url == "/index.html" %} |
如果你首页使用的并非 html
后缀,而是 htm
请自行将 html
改完 htm
错误代码注明
因为 Jekyll 并没有 is_home
变量,所以以下代码无效。当然你也可以在首页 font matter 添加一个 is_home 变量然后调整代码实现该功能。
1 | {% unless page.is_home %} |
Relay Tips: 一极乐( https://yijile.com/zh/jekyll-liquid-specifies-if-to-display-on-the-homepage/ )