Skip to content
Grav 2.0 is officially stable. Read the announcement →
Themes & Styling

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

content-types twig

Started by Hugh Barnes 3 years ago · 8 replies · 729 views
3 years ago

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:

HTML
<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:

TWIG
{% 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.

3 years ago

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

3 years ago

@pamtbaau:
is it too hard to copy and alter all three templates from the Feed plugin?

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.

3 years ago

@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.

1 reply
3 years ago

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.

3 years ago

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.

1 reply
3 years ago

@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? https://github.com/getgrav/grav-plugin-feed
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

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Sebadamus, 6 days ago
5 147 12 hours ago
Themes & Styling · by martynfoster735, 1 week ago
1 192 1 week ago
Themes & Styling · by Justin Young, 3 weeks ago
1 240 3 weeks ago
Themes & Styling · by Slebeig, 1 month ago
4 373 1 month ago
Themes & Styling · by Pedro M, 3 months ago
4 744 3 months ago