如何将{%captured%}变量从视图传递到Jekyll/Liquid中的布局?

fir*_*dev 18 liquid-layout liquid jekyll jekyll-extensions

我正在尝试在Jekyll重建一个博客,我已经找到了一个简单的任务.

如果我有以下模板集:

default.html中:

{{ head }}

{{ content }}
Run Code Online (Sandbox Code Playgroud)

frontpage.html:

---
layout: default
---

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

{{ content }}
Run Code Online (Sandbox Code Playgroud)

index.html的:

---
layout: frontpage
---

Other stuff
Run Code Online (Sandbox Code Playgroud)

我当时希望{% capture head %}将变量传递给布局.但似乎只有Front Matter的变量实际上被传递为page.variable_name.

有没有办法将capture-d var 传递给Jekyll的布局?

猜猜我可以制作2种不同的布局frontpage,normal_page这将取代布局中的整个{{head}}{{content}}块.但这就像html的两倍,所以capture如果可能的话我宁愿解决它.

Dav*_*son 8

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

default.html中

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

{{ content }}
Run Code Online (Sandbox Code Playgroud)

frontpage.html

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

{{ content }}
Run Code Online (Sandbox Code Playgroud)

_includes/header1.html

(Frontpage header content)
Run Code Online (Sandbox Code Playgroud)


Car*_*l G 6

如果您的用例和我的一样,并且您想在模板中包含添加的内容,则可以使用 YAML 的块标量功能将前端内容中的多行内容包含到模板中。A|保留换行符,而 a>删除(“折叠”)换行符。(注意块指示符后面必须跟一个空行。)

索引.html

---
layout: default
head: |
  <link href="//cdn-images.mailchimp.com/embedcode/classic-081711.css" rel="stylesheet" type="text/css">
  <style type="text/css">
    #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
  </style>
script: |
  <script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script>
  <script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';fnames[3]='PHONE';ftypes[3]='phone';fnames[4]='ORG';ftypes[4]='text';fnames[5]='MMERGE5';ftypes[5]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
---
<!-- Content, maybe a MailChimp signup form? -->
Run Code Online (Sandbox Code Playgroud)

默认.html

<!DOCTYPE html>
<html>
<head>
  <title>
    {{page.title}}
  </title>
  <link rel="stylesheet" type="text/css" href="/css/main.css">

  <!-- here you can have add'l arbitrary head content -->
  {{ page.head }}
</head>
<body>
  {{content}}

  <script>
    // Google Analytics, perhaps?
  </script>

  <!-- here you can have add'l arbitrary content at the end of the page, good for scripts -->
  {{page.script}}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)