0

I would like to be able to add to the list of published articles only documents that contain the variable publish = true in the file header.

I have added to each article the following lines in the header: foo.md:

+++
title = "Foo"
date = 2022-06-03
publish = true
+++

bar.md:

+++
title = "Bar"
date = 2022-06-03
publish = false
+++

This is the code I have tried to use in my index.html for Zola:

    <h2 id="Articles">Articles</h2>
    <ul class="articles">
    {% set section = get_section(path="blog/_index.md") %}
    {% for page in section.pages %}
        {% if page.publish %}
            <li>
                <a href="{{ page.permalink | safe }}">{{ page.title }}</a>
                <p class="date">{{ page.date }}</p>
            </li>
        {% endif %}
    {% endfor %}
Jmb
  • 18,893
  • 2
  • 28
  • 55
luarpy
  • 3
  • 3
  • And what happens? – Peter Hall Apr 12 '23 at 15:46
  • I think you can't just define your own [front matter](https://www.getzola.org/documentation/content/page/#front-matter) for a page (your `publish` field); it has to be stored in the `[extra]` table. Try putting a line with `[extra]` before `publish = ...` and change the call in your template to `{% if page.extra.publish %}` – Jonas Fassbender Apr 12 '23 at 15:50

1 Answers1

0

Going through the documentation, I found the draft variable that can be used to determine if a document is ready to publish or not.

In the case of being for publishing, the variable draft = false is set inside the file header. It would look like this:

+++
title = "Foo"
date = 2022-03-23
draft = false
+++

and vice versa for those cases in which you want to indicate that the document is still in draft form:

+++
title = "Bar"
date = 2022-04-21
draft = true
+++

No other changes need to be made to the default code:

    <h2 id="Articles">Articles</h2>
    <ul class="articles">
    {% set section = get_section(path="blog/_index.md") %}
    {% for page in section.pages %}
        <li>
            <a href="{{ page.permalink | safe }}">{{ page.title }}</a>
            <p class="date">{{ page.date }}</p>
        </li>
    {% endfor %}

Output for that code in after zola serve or zola build:

Articles

<h2 id="Articles">Articles</h2>
<ul class="articles">
        <li>    
            <a href="http://127.0.0.1:1111/Foo">Foo</a>
            <p class="date">2022-03-23</p>
        </li>
</ul>

Output for that code in after zola serve --drafts or zola build --drafts:

    <h2 id="Articles">Articles</h2>
    <ul class="articles">
            <li>    
                <a href="http://127.0.0.1:1111/Foo">Foo</a>
                <p class="date">2022-03-23</p>
            </li>
            <li>    
                <a href="http://127.0.0.1:1111/Bar">Bar</a>
                <p class="date">2022-04-21</p>
            </li>
    </ul>
luarpy
  • 3
  • 3