如何将视图中的{%Capture%}变量传递给Jekyll/Liquid中的布局? [英] How to pass {% captured %} variable from a view to the layout in Jekyll/Liquid?

查看:110
本文介绍了如何将视图中的{%Capture%}变量传递给Jekyll/Liquid中的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Jekyll中重建博客,而我已经完成了一项简单的任务.

I am trying to rebuild a blog in Jekyll and I have stubled upon a simple task.

提供了以下模板集:

default.html:

{{ head }}

{{ content }}

frontpage.html:

---
layout: default
---

{% capture head %}
  Frontpage
{% end %}

{{ content }}

index.html:

---
layout: frontpage
---

Other stuff

我期望{% capture head %}会将变量传递给布局.但是似乎只有前项中的变量实际上是作为page.variable_name传递的.

I was expecting that {% capture head %} would pass a variable to layout. But it seems only variables from the Front Matter are actually being passed as page.variable_name.

是否可以将capture -d var传递给Jekyll中的布局?

Is there a way to pass capture-d var to the layout in Jekyll?

猜猜我可以为frontpagenormal_page制作2种不同的布局,以替换布局中的整个{{head}}{{content}}块.但这就像html的两倍,所以我宁愿使用capture来解决它.

Guess I could make 2 different layouts for frontpage and normal_page that would replace the whole {{head}}{{content}} block in the layout. But that's like twice the html, so I'd rather solve it with capture if possible.

推荐答案

您不能使用捕获来执行此操作,但是可以使用包含.页面层次结构的每个级别都可以覆盖head键,以根据需要指向其他包含文件

You can't do this with a capture, but you can using an include. Every level of the page hierarchy can override the head key to point to a different include file as required

{% include {{ page.head }} %}

{{ content }}

frontpage.html

---
layout: default
head: header1.html
---

{{ content }}

_includes/header1.html

(Frontpage header content)

这篇关于如何将视图中的{%Capture%}变量传递给Jekyll/Liquid中的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆