luarpy
luarpy

Reputation: 63

How can I add pages containing the ``publish`` variable in zola?

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 %}

Upvotes: 2

Views: 208

Answers (1)

luarpy
luarpy

Reputation: 63

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>

Upvotes: 2

Related Questions