A way to determine all content types available for a "template"?

I have added an RSS template blog.rss.twig to supplement blog.html.twig in a theme. I am porting a Wordpress site, so this is a format I want to continue supporting. I have chosen not to use the feed plugin this time.

The feed works fine under this system and I went to add an autodiscovery link tag to the HTML template’s head. For those not familiar, these takes the form:

<link rel="alternate" type="application/rss+xml" href="//example.org/feed" />

In future, I want to add both Atom and JSON feed alternatives as well, because they are much richer, but for now I just need to support the old RSS.

When I was adding the autodiscovery link to the HTML template, I added it by hand. But it occurred to me that it would be more robust to run a Twig loop through all the content types that the template is available in. That’s when I couldn’t find a way to determine that in Twig.

Does anyone know how I can retrieve an array of every extension/content type variant that the current template is available in?

For example, from blog.html.twig, I want to say:

{% for contype in page.getContentTypes() if not contype == 'html' %}
    {# add the link element etc #}

(or use get_content_types(page.template) or something like that).

I suppose I can query the filesystem in a custom function, but that seems hacky. This is not holding me back, I am just interested in the question. I think a Github issue might just get buried or de-prioritised.

@hughbris, Is it too hard to copy and alter all three templates from the Feed plugin? They are all approx. 40 lines of code.

I didn’t say it was too hard, it’s a matter of preference. I don’t care for that plugin. I don’t think the plugin addresses my question either.

@hughbris, I’m not suggesting to use the plugin, but you’ll need the templates anyway. Why not just copy all three of them form the Feeds plugin into you theme? I suspect, your RSS, Atom and JSON formats won’t differ.

By copying all three, you won’t need to find out whether a template exists or not.

I think I started out with the plugin, then realised I was editing the templates so much that I might as well start afresh, and I really didn’t like the complexity of that solution anyway.

I read a post earlier today where you talk about complex vs simple solutions. (Sorry, it’s late, else I would link it now.) The Feed plugin is an example of the former.

I think I’d better explain my original question better tomorrow.

So just to clarify, the topic question is whether I can programmatically determine or query (Twig best) which content types exist for rendering an .md file (“page”). So for a blog maybe HTML, RSS, Atom.

The purpose is to automatically provide alternate links in the HTML header, and honestly it’s not a big deal. Nice to have and I thought I’d see if anyone knows a trick. My explanation wasn’t great.

@hughbris, I like to think I did understand your question when giving my previous answer…

If you provide all three templates, there is no need to programmatically determine which content types exist, because all three exist.

I think I started out with the plugin, then realised I was editing the templates so much that I might as well start afresh, and I really didn’t like the complexity of that solution anyway.

Are we talking about the same plugin? GitHub - getgrav/grav-plugin-feed: Grav Feed Plugin
I fail to see the complexity. Apart from some extra options allowing to manipulate the collection, it is more or less like:

  • Get the (blog) collection defined in the page
  • In template feed.rss|atom|json.twig:
    • Set up the header of the feed
    • Loop the collection and turn each collection item into an rss|atom|json item

What would the link or mapping between content and the output format of that content be do you think? I’m thinking of a kind of lookup table maybe in a template or specify the formats in the page frontmatter.