Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables Jekyll:基于页面变量定义一个新变量 我的问题_Variables_Jekyll - Fatal编程技术网

Variables Jekyll:基于页面变量定义一个新变量 我的问题

Variables Jekyll:基于页面变量定义一个新变量 我的问题,variables,jekyll,Variables,Jekyll,我正在用Jekyll写一个静态站点。每篇文章都应该有一个带有作者姓名的“Posted by”副标题,以及一个指向其网页的可选链接,如果没有定义作者姓名,还应该有一个指向网站标题的回退 例如,假设我的站点标题是“Acme corp” 作者和“URL” 应该给[某人](https://their_site.com) 作者 应该给某人 没有作者 应该给Acme公司 我试过什么 我可以将逻辑硬编码到\u layouts/post.html页面中,但我必须在其他页面模板中重复自己的操作: 发布人 {%if

我正在用Jekyll写一个静态站点。每篇文章都应该有一个带有作者姓名的“Posted by”副标题,以及一个指向其网页的可选链接,如果没有定义作者姓名,还应该有一个指向网站标题的回退

例如,假设我的站点标题是“Acme corp”

作者和“URL” 应该给[某人](https://their_site.com)

作者 应该给某人

没有作者 应该给Acme公司

我试过什么 我可以将逻辑硬编码到
\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 %}