<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Jekyll模板代码 乐知道 on 一极乐</title><link>/tags/jekyll%E6%A8%A1%E6%9D%BF%E4%BB%A3%E7%A0%81-%E4%B9%90%E7%9F%A5%E9%81%93/</link><description>Recent content in Jekyll模板代码 乐知道 on 一极乐</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><managingEditor>blog4@outlook.com (堃埜)</managingEditor><webMaster>blog4@outlook.com (堃埜)</webMaster><copyright>Copyright © [site_title]</copyright><lastBuildDate>Fri, 28 Feb 2025 20:22:05 +0000</lastBuildDate><atom:link href="/tags/jekyll%E6%A8%A1%E6%9D%BF%E4%BB%A3%E7%A0%81-%E4%B9%90%E7%9F%A5%E9%81%93/index.xml" rel="self" type="application/rss+xml"/><item><title>Jekyll Liquid 指定内容只在首页显示或在首页以外的页面显示</title><link>/zh/jekyll-liquid-specifies-if-to-display-on-the-homepage/</link><pubDate>Fri, 28 Feb 2025 20:22:05 +0000</pubDate><author>blog4@outlook.com (堃埜)</author><guid>/zh/jekyll-liquid-specifies-if-to-display-on-the-homepage/</guid><description>&lt;p>有时候我们可能会有一些奇奇怪怪的需求。比如某些内容只在站点首页显示，某些内容只在首页以外的页面显示，特别是我们用的是通用模板的时候处理起来就比较麻烦。&lt;/p>
&lt;p>这种奇怪的需求可能比较场景的场景有：&lt;/p>
&lt;ul>
&lt;li>Blog 的友情链接只在首页显示，其他的页面不显示&lt;/li>
&lt;li>某个位置的广告只在首页以外的页面显示，首页不显示&lt;/li>
&lt;/ul>
&lt;p>然而 Jekyll 使用的 Liquid 模板语言处理这类需求轻而易举。如果你在使用 Jekyll Liquid 可以通过以下方法实现。&lt;/p>
&lt;h2 id="使用-unless-设置首页以外的页面显示特定内容">使用 unless 设置首页以外的页面显示特定内容
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{% unless page.url == &amp;#34;/&amp;#34; or page.url == &amp;#34;/index.html&amp;#34; %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &amp;lt;!-- 这里是要在首页以外显示的内容 --&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% endunless %}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="使用-if-设置首页以外的页面显示特定内容">使用 if 设置首页以外的页面显示特定内容
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{% if page.url != &amp;#34;/&amp;#34; and page.url != &amp;#34;/index.html&amp;#34; %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &amp;lt;!-- 这里是要在首页以外的页面显示的内容 --&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% endif %}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>如果你想让内容只在首页显示也很简单。&lt;/p>
&lt;h2 id="使用-unless-设置仅在首页显示特定内容">使用 unless 设置仅在首页显示特定内容
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{% unless page.url != &amp;#34;/&amp;#34; or page.url != &amp;#34;/index.html&amp;#34; %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &amp;lt;!-- 这里是要在首页以外显示的内容 --&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% endunless %}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="使用-if-设置仅在首页显示特定内容">使用 if 设置仅在首页显示特定内容
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{% if page.url == &amp;#34;/&amp;#34; and page.url == &amp;#34;/index.html&amp;#34; %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &amp;lt;!-- 这里是要在首页以外的页面显示的内容 --&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% endif %}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>如果你首页使用的并非 &lt;code>html&lt;/code> 后缀，而是 &lt;code>htm&lt;/code> 请自行将 &lt;code>html&lt;/code> 改完 &lt;code>htm&lt;/code>&lt;/p>
&lt;h2 id="错误代码注明">错误代码注明
&lt;/h2>&lt;p>因为 Jekyll 并没有 &lt;code>is_home&lt;/code> 变量，所以以下代码无效。当然你也可以在首页 font matter 添加一个 is_home 变量然后调整代码实现该功能。&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{% unless page.is_home %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &amp;lt;!-- 非首页显示的内容 --&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% endunless %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% if page.is_home %}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &amp;lt;!-- 在首页显示的内容 --&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{% endif %}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div></description></item></channel></rss>