Variables Jekyll:基于页面变量定义一个新变量 我的问题
我正在用Jekyll写一个静态站点。每篇文章都应该有一个带有作者姓名的“Posted by”副标题,以及一个指向其网页的可选链接,如果没有定义作者姓名,还应该有一个指向网站标题的回退 例如,假设我的站点标题是“Acme corp” 作者和“URL” 应该给[某人](https://their_site.com) 作者 应该给某人 没有作者 应该给Acme公司 我试过什么 我可以将逻辑硬编码到Variables Jekyll:基于页面变量定义一个新变量 我的问题,variables,jekyll,Variables,Jekyll,我正在用Jekyll写一个静态站点。每篇文章都应该有一个带有作者姓名的“Posted by”副标题,以及一个指向其网页的可选链接,如果没有定义作者姓名,还应该有一个指向网站标题的回退 例如,假设我的站点标题是“Acme corp” 作者和“URL” 应该给[某人](https://their_site.com) 作者 应该给某人 没有作者 应该给Acme公司 我试过什么 我可以将逻辑硬编码到\u layouts/post.html页面中,但我必须在其他页面模板中重复自己的操作: 发布人 {%if
\u layouts/post.html
页面中,但我必须在其他页面模板中重复自己的操作:
发布人
{%if page.author_url%}
{%endif%}
在{page.date | date:“%B%-d,%Y”}
我的问题
是否有一种方法可以根据
页面.author
和页面.author\u url
的值定义全局变量(例如author\u html
),该变量可以在所有模板页面上访问?为什么不创建一个author.html
include,并与当前作者一起调用include(或返回默认值)在参数中,例如:
{% capture author_link %}
{% if page.author_url %}
<a href="{{ page.author_url }}">
{% endif %}
{% if page.author %}{{ page.author }}{% else %}{{ site.title }}{% endif %}
{% if page.author_url %}
</a>
{% endif %}
{% endcapture %}
{% include author.html author=author_link %}
{%capture author\u link%}
{%if page.author_url%}
{%endif%}
{%endcapture%}
{%include author.html author=author\u link%}
在包含中,您可以通过调用include.author
获取作者。有关更多信息,请参阅包含中的。
我希望这能解决你的问题
author: Someone
{% capture author_link %}
{% if page.author_url %}
<a href="{{ page.author_url }}">
{% endif %}
{% if page.author %}{{ page.author }}{% else %}{{ site.title }}{% endif %}
{% if page.author_url %}
</a>
{% endif %}
{% endcapture %}
{% include author.html author=author_link %}