7

More of a question that problem, I see this often - templates that start with an underscore. For example, _entry.html.

Reading the Craft docs:

Note that if any of the URI segments begin with an underscore ('_'), Craft will return a 404, since template path segments that begin with an underscore are considered "hidden" from direct access

I guess I'm confused as to what I am hiding from. Is it so I can't directly browse right to the full path of that template?

mysite.com/something/something/_template.html

If you move the craft folder outside of the site root, I can't really get to that file can I? It just isn't obvious to me (yet) what the benefit is and when/where/why I need to start a template with and underscore.

Thank you!

7

Yes, prefixing a template's filename with an underscore does what it says on the box – any underscored template will be excluded from route mapping.

A typical use case is to use underscores for partials or wrappers, e.g. "_mainmenu.html" or "_baseTemplate.html" – essentially any template that doesn't make any sense to load up or access directly in the browser via that template's path.

This isn't a mandatory thing, but a good practice.

  • 1
    It actually is required if you want to prevent URI to tempate routing for that URI (→ routing case 6). – carlcs Apr 1 '15 at 15:11
  • Good point, @carlcs. – Mats Mikkel Rummelhoff Apr 1 '15 at 15:20
  • It's an old topic, but I found it since I was looking for the meaning of prefixing underscores for folder names and I could only find this topic. So I might be asking a dumb question, if so, my sincere apologies :-), but I'm wondering: what does prefixing underscores for folder names do? Does that mean that all templates in that folder are being excluded from route mapping so you don't need a prefixed underscore for all templates inside that folder? I now do both: prefixing my folder and prefixing my template name, but I'm wondering if that's too much... – Jan Jun 29 '18 at 6:45
  • 1
    @Jan Right, if you prefix a folder name w/ an underscore, everything inside that folder (both templates and subfolders) will be excluded from routing. Hence, it’s not necessary to underscore a template sitting in an underscored folder. – Mats Mikkel Rummelhoff Jun 29 '18 at 22:32
  • Thanks for the answer, now I know for once and for all :-) ! – Jan Jul 3 '18 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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