3

I like to use .twig for my templates for easy syntax switching, but it doesn't seem to work when my template requires an extension as part of its name.

For instance:

  • robots.txt works fine
  • robots.txt.twig, doesn't. The matching request is now {siteUrl}/robots.txt.twig, and I don't get the automatic Content-Type matching that happens when it is just robots.txt.

I figure this just isn't currently supported, but I would be nice!

3
  • 1
    Curious... what's the use case for using a double extension? i.e. Why not just use robots.twig?
    – Brad Bell
    Commented Aug 28, 2014 at 20:53
  • Well, in the case of robots.txt, because it has to be accessible from /robots.txt. So to use robots.twig you'd have to set up a route. And with the .txt in the template name, you get the content-type header automatically set.
    – Tim Kelty
    Commented Aug 28, 2014 at 21:24
  • @TimKelty We made this possible in Craft 2.2. Updated my answer below! Commented Sep 2, 2014 at 18:57

1 Answer 1

2

This is because right now, if your template path includes an extension, Craft assumes that you’re explicitly providing the full template path, and won’t try appending the defaultTemplateExtensions to it. (See a full explanation here.)

robots.txt does make a good use case for why we should append those extensions anyway, though. We shall consider it :)

UPDATE

We changed this behavior in Craft 2.2, so now a request to /robots.txt could resolve to a template called /robots.txt.twig. http://buildwithcraft.com/updates#build2579

3
  • The 2.2 update is great, and allows us to name things robots.txt.twig, entries.json.twig...However, we now unfortunately miss out on the Content-type detection that we would get if we had the templates names without the .twig on the end. Too much to ask? :)
    – Tim Kelty
    Commented Sep 16, 2014 at 19:32
  • 2
    .twig files are always going to default to HTML. You can explicitly set the MIME type using the {% header %} tag though: {% header "Content-Type: text/plain; charset=utf-8" %} Commented Sep 16, 2014 at 20:50
  • If you are setting up sitemap.xml.twig {% header "Content-Type: text/xml;" %} <?xml version="1.0" encoding="UTF-8" ?> Commented Jun 26, 2015 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.