1

我在布局中使用前端问题时遇到了麻烦。它只是在页面上显示最前面的内容,而无需创建我的布局所需的数据。

代码如下:

/src/_includes/layouts/base.html

---
rightLinks : [
        {
            "icon": "mail_outline",
            "href": "https://google.com"
        }
    ]
---
<!DOCTYPE html>
<html lang="es-AR">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{{ title }}</title>
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet">
  <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,300;0,400;0,600;0,700;0,900;1,300;1,400;1,600;1,700;1,900&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="/css/global.css">
</head>
<body>
  <!-- top-nav try to use rightLinks data from this layout -->
  {% include "partials/top-nav.html" %}
  {% include "partials/main-nav.html" %}
  <main tabindex="-1" id="main-content">{% block content %}{% endblock %}</main>
</body>
</html>

/src/_includes/layouts/home.html

{% extends "layouts/base.html" %} 
{% block content %}

<section class="hero is-black is-fullheight-with-navbar hero-home">
  <div class="hero-body">
    <div class="container">
      <div>
        <h1 class="title text-black text-huge">
          Nexus Materiales Eléctricos
        </h1>
        <h2 class="subtitle is-4">
          Somos mayoristas en insúmos eléctricos de calidad.
        </h2>
      </div>
      <div class="mt-6">
        <a href="/marcas" class="button is-rounded is-large is-dark is-uppercase text-semi-black">
          ver marcas
        </a>
      </div>
    </div>
  </div>
</section>

<section class="section has-background-white">
  <h2 class="title">Nuestras marcas destacadas</h2>
  <h3 class="subtitle">Calidad y seguridad</h3>
</section>

{% endblock %}

/src/index.md

---
title: 'Hello World!'
layout: 'layouts/home'
---

作为视觉结果,我得到了顶部的实际字符串: 在此处输入图像描述

在我看来,十一,出于某种原因,正在跳过前面的问题。

这是我的十一配置文件:

module.exports = function(config) {
    config.addWatchTarget("./src/sass/");
    config.addPassthroughCopy('./src/images')

    // Return your Object options:
    return {
        dir: {
            input: "src",
            output: "dist"
        },
        markdownTemplateEngine: 'njk',
        dataTemplateEngine: 'njk',
        htmlTemplateEngine: 'njk',
    }
  };

有人可以向我解释为什么会发生这种情况,我该如何解决?谢谢

4

1 回答 1

1

Front-matter 数据仅在布局文件中可用,这些布局文件由带有layout:front matter 键的页面直接引用,而不是Nunjucks {% extends %}。作为11ty 数据级联的一部分,直接引用的布局文件中的前沿数据与其他数据合并。(110 个文档:布局中的前沿问题

例如:

<!-- index.md -->
---
layout: layouts/content
---
# some content here
{# layouts/base.njk #}
---
hello: "world"
---
{# ^ the above will be printed as plain text and isn't available #}
{# layouts/content.njk #}
---
someKey: "something"
---
{% extends "layouts/base.njk" %}

{{ someKey }}
{# ^ this will render "something" as expected #}

这可能是因为这是{% extends %}Nunjucks 的一个功能,而 Eleventy 不知道引用了哪些其他 Nunjucks 文件。

对于您的情况,您有几个选择。首先,您可以将您的前事数据放在 中layouts/home,因为这就是您在index.md. 另一种选择是只使用Nunjuck 的settag

{% set links = [
    {
        "icon": "mail_outline",
        "href": "https://google.com"
    }
] %}

{% for link in links %}
    {{ link.icon }}
    {{ link.href }}
{% endfor %}
于 2021-09-10T23:59:01.257 回答