We want to add a little more structure to our templates directory by adding folders to group templates files.

For example:

templates
--_macros
--_views
--_layouts

In general this works fine but we encountered a problem with the index pages of sections. All pages are grouped in the views directory, so in views we could have a folder "articles" and within that a file "_entry.twig" and "index.twig".

For the entry to work we change the template path of the section in the backend. But there is no place to this for the index file.

Anybody an idea how best to handel this? Is there a way to tell craft to also look in de _views folder?

up vote 2 down vote accepted

It sounds like you're wanting to move templates around like this:

articles/index.html
articles/_entry.html

To this:

_layouts/articles/index.html
_layouts/articles/entry.html

There are three basic ways Craft figures out what template to use based on the URL:

  • Element routing like articles/_entry.html (matches a URI in an element's settings, e.g. section or category)
  • dynamic routes and URL Rules
  • Template path routing like articles/index.html

(Craft 3 docs on routing)

To get a URL like http//example.com/articles to use the template _layouts/articles/index.html, you have two options:

  1. Set up a dynamic route or URL rule.
  2. Create a Single with the URI set to articles.

Personally, I often create a Single for this like a Blog index page. Then it's easy to control parts of the index page like a header image or custom SEO tags.

A dynamic route would look like this:

dynamic route example screenshot

A URL rules would work something like this in config/routes.php:

'articles' => ['template' => '_layouts/articles/index'],

A third option that I don't recommend is if you have a structured section called something like "Pages" (or Static Pages or Landing Pages), you could create different Entry Types called "Page" and "Articles Index" just for this.

├── About Us (Page)
│   └── Our Team (Page)
├── Articles (Articles Index)
├── Services (Page)
└── Contact (Page)

I'm not a fan of this for a few reasons, but the main one is that you can't point entry types to different templates in Craft so you'll probably have to use Twig includes to create a "routing template" that points the Article Index entry type to a different template. Also, chances are you will never need more than one Articles index page, and you won't want to drag and drop it's location around the structure because the URLs will become out of sync with the child Article entries.

  • The problem solved itself when we realized we would need a single for each "index" page, just like your blog example. But it's good to know that the routes can be defined in the config. – dazz Sep 14 at 7:28

This is how my templates directory looks for almost every project:

Templates
--- _layout
--- includes
--- partials
--- site
--- users
--- shop

As long as your templates update their 'extend' tag to be {% extends '_layout/base' %} then Craft understands where that is. Craft is aware of everything in the templates directory, you don't need to set up much config except for things like the users login/password templates.

My main site pages structure is setup in the CMS to look at /site/_index.twig then all the entry.types within this structure are based on this template and I include the entry type's appropriate template in my site base file.

Let me know if you need more details.

You can't create a url path that goes to a _*** folder. If you rename your folder without the underscore, it should work. Folders that uses underscores are kind of protected and can only be used by other templates.

  • Unless you tell your structure/channel/whatever to look at an underscored template explicitly then it will work. – Jay Sep 12 at 7:28

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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