Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template:feeds: add extra block #2263

Merged
merged 5 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@ fn can_build_feeds() {
assert!(file_contains!(public, "posts/tutorials/programming/atom.xml", "Foo Doe"));
assert!(file_contains!(public, "posts/tutorials/programming/atom.xml", "Bar Doe"));
assert!(file_contains!(public, "posts/tutorials/programming/atom.xml", "Baz Doe"));

// Test feeds with extended block
assert!(file_contains!(public, "atom.xml", "<category term=\"Foo tag\"/"));
assert!(file_contains!(public, "atom.xml", "<category term=\"Bar tag\"/"));
assert!(file_contains!(public, "rss.xml", "<category>Foo tag</category>"));
assert!(file_contains!(public, "rss.xml", "<category>Bar tag</category>"));
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions components/templates/src/builtins/atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
{% else %}
<content type="html" xml:base="{{ page.permalink | escape_xml | safe }}">{{ page.content }}</content>
{% endif %}

{%- block atom_entry_extra %}
{% endblock atom_entry_extra -%}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand why, but these lines cause the tests to fail:

     Running tests/invalid.rs (/home/h/projects/zola/target/debug/deps/invalid-9ae7bc0e52bdd68b)

running 1 test
test errors_on_index_md_page_in_section ... FAILED

failures:

---- errors_on_index_md_page_in_section stdout ----
thread 'errors_on_index_md_page_in_section' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Msg("Failed to parse '__zola_builtins/atom.xml'"), source: Some(Error { kind: Msg("  --> 52:9\n   |\n52 |         {% block atom_entry_extra %}\n   |         ^---\n   |\n   = unexpected tag; expected an `else` tag, an endfor tag (`{% endfor %}`), or some content"), source: None }) }', components/templates/src/lib.rs:28:6
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The message is:

$ echo -e "  --> 52:9\n   |\n52 |         {%- block atom_entry_extra %}\n   |         ^---\n   |\n   = unexpected tag; expected an `else` tag, an endfor tag (`{% endfor %}`), or some content"
  --> 52:9
   |
52 |         {%- block atom_entry_extra %}
   |         ^---
   |
   = unexpected tag; expected an `else` tag, an endfor tag (`{% endfor %}`), or some content

We can't add a block to a builtin template? I am not sure what I am missing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add <!-- Optional extra content --> inside the block to have some content, but I got the same error.

I also tried adding these lines to components/templates/src/lib.rs:19:

        ("__zola_builtins/atom.xml", "{% extends \"__zola_builtins/atom_base.xml\" %}{% block atom_entry_extra %}<>{% endblock atom_entry_extra %}"),
        ("__zola_builtins/rss.xml",  "{% extends \"__zola_builtins/rss_base.xml\" %}{% block rss_item_extra %}<>{% endblock rss_item_extra %}"),

And no success.

I'm not familiar with details on Tera, but does this mean we can't have an empty block in the built-in templates?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because the block is in a for loop, which is not allowed in Tera. Maybe the extend approach doesn't work then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a bummer.

I reverted the commits and updated the docs on how to get the templates and modify. Maybe that's worth of a merge.

</entry>
{%- endfor %}
</feed>
3 changes: 3 additions & 0 deletions components/templates/src/builtins/rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<link>{{ page.permalink | escape_xml | safe }}</link>
<guid>{{ page.permalink | escape_xml | safe }}</guid>
<description xml:base="{{ page.permalink | escape_xml | safe }}">{% if page.summary %}{{ page.summary }}{% else %}{{ page.content }}{% endif %}</description>

{%- block rss_item_extra %}
{% endblock rss_item_extra -%}
</item>
{%- endfor %}
</channel>
Expand Down
7 changes: 7 additions & 0 deletions docs/content/documentation/templates/feeds/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ for `atom.xml` (in the preferred Atom 1.0 format), and `rss.xml` (in the RSS
2.0 format). If you choose a different filename (e.g. `feed.xml`), you will
need to provide a template yourself.

Alternatively, instead of providing an entire template for the feed you can
extend them by [overriding a
block](@/documentation/themes/extending-a-theme.md#overriding-a-block). The
Atom template provides a block named `atom_entry_extra`, while the rss template
has the block `rss_item_extra`. These blocks allow you to extend the content of
each item that goes into the feed.

**Only pages with a date will be available.**

The feed template gets five variables:
Expand Down
1 change: 1 addition & 0 deletions test_site/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ theme = "sample"
taxonomies = [
{name = "categories", feed = true},
{name = "podcast_authors", feed = true},
{name = "tags", feed = true},
]

ignored_content = ["*/ignored.md"]
Expand Down
10 changes: 10 additions & 0 deletions test_site/content/posts/with-tag-taxonomy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+++
title = "A post with tags"
description = ""
date = 2023-07-29

[taxonomies]
tags = ["foo tag", "bar tag"]
+++

This post has a `tags` taxonomy.
11 changes: 11 additions & 0 deletions test_site/templates/atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "atom.xml" %}

{% block atom_entry_extra %}

{%- if page.taxonomies and page.taxonomies.tags %}
{%- for tag in page.taxonomies.tags %}
<category term="{{ tag }}/">
{% endfor -%}
{% endif -%}

{% endblock atom_entry_extra %}
11 changes: 11 additions & 0 deletions test_site/templates/rss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "rss.xml" %}

{% block rss_item_extra %}

{%- if page.taxonomies and page.taxonomies.tags %}
{%- for tag in page.taxonomies.tags %}
<category>{{ tag }}</category>
{% enfor -%}
{% endif -%}

{% endblock rss_item_extra %}