3

CONTEXT:

  • Eleventy and Nunjucks (and Markdown)
  • A lot of long-form text (easier to create/edit using markdown).
  • Complex layouts.
  • Still new to SSGs

GOAL:

  • Manage chunks of text content using markdown.
  • Use these markdown files with template partials.
  • Assemble partials into a page.

EXPECTED RESULT

Processed html page:

    <html>
      <body>
        <div>
          <p>Some content originating from a markdown file.</p>
        </div>
        <div>
          <p>Some content originating from another markdown file.</p>
        </div>
      </body>
    </html>

ATTEMPTED ACTIONS

Here is what I've tried so far...

(Note: I've excluded my base.njk (html doctype shell) for readability.)

1. NJK MAIN with NJK PARTIAL INCLUDES

INPUT

Directory Structure

src/
    /_includes
        base.njk
        _layout-A.njk
        _layout-B.njk
    main-layout.njk
    content-1.md
    content-2.md

main-layout.njk

    {% extends "base.njk" %}

    {% block content %}

        {% include '_layout-A.njk' %}

        {% include '_layout-B.njk' %}

    {% endblock %}

content-1.md

    ---
    layout: _layout-A.njk
    --- 
    Some content.

_layout-A.njk

    <div>{{ content | safe }}</div>

content-2.md

    ---
    layout: _layout-B.njk
    --- 

    Some more content.

_layout-B.njk

    <div>{{ content | safe }}</div>

RESULT

  • Directory structure 'splits'.
dist/
    /content-1
        index.html
    /content-2
        index.html
    /main-layout
        index.html

  • Markdown content not passed through to parent page. Empty child tags in parent.

main-layout/index.html

    <html>
      <body>
        <div></div>
        <div></div> 
      </body>
    </html>


I'm at a loss for how the files are processed and what I can do to do what I set out to.

gabrimo
  • 53
  • 6
  • I'm honeslty confused as to what you are trying to do. Includes definitely work, but front matter will not be run with them, so when you included a child and it had a layout defined, it would not work like that. Can you simplify a bit? – Raymond Camden Aug 17 '20 at 19:28
  • @RaymondCamden My goal was to keep my content and layout as separate as possible. For example, I want to manage 'chunks' of textual content in markdown files and be able to mix and match those with different template partials for a given page. Lesha Ogonkov (below) confirmed what I had suspected; 11ty can't do it out of box - but can be with a little retooling. I'll clarify my post for any future visitors. – gabrimo Aug 17 '20 at 22:30

1 Answers1

4

That's not how 11ty works. Each MD file is a single page.

If you want to include multiple MD files to page, you should add custom filter for 11ty, to render it to html.

See examples in this issue https://github.com/11ty/eleventy/issues/658

Lesha Ogonkov
  • 1,218
  • 8
  • 20