Get all page templates in code

I need to programmatically get hold of a page Template (CMS_Template) by name. What is the best way to do this? Is there any way of getting all existing page Templates through code?

Litium version: 6.2.1

You can get template from its pagetype if that is known.

var pageType = ModuleCMS.Instance.PageTypes.GetPageType("MyPageType");
var template = pageType.Templates.GetTemplate("TemplateName");

The templates I need to handle aren’t connected to any page types (due to some unfortunate naming choices).

Templates in CMS are always connected to pagetypes? How can you have a disconnected template?

Legacy data. We have a lot of page types that were created long before MVC was a thing in Litium (created from BO and not from code). I’ve created PageTypeDefinitions and MVC controllers for them, and most page types are getting the correct template automatically through name matching. But we have some special cases where the old page template names don’t match the new PageTypeDefinition + Controller names. For example, I have 3 page types (with 3 different names obviously) that I need to connect to one MVC template with a completely different name.

Your old templates should still be connected to pagetypes, you can always check which one in the database.

A template is connected to a pagetype by decorating the controller as PageTemplate so Controller name should not be an issue: [PageTemplate(typeof(Article), "~/Site/Images/Article.png", Name = "Article1")].

And if you need to connect multiple pagetypes to one controller you can do that in Litium backoffice, or create three controller actions that return the same view.

I’ll try the multiple-controller-actions approach.

For future reference - is there any way of getting a list of all templates/getting a specific template by name without going through the page type?

Not that I know of since templates are only the display for pagetypes, but normally you know which pagetype you are working with, not really sure what your scenario is.

With Litium 7 this will be different and the templates will be as they are in Customers/PIM.