Can I define page types in my theme (instead of building them out in the backend)?
Created 5 years ago by immersiontravis

The "Creating Your First Page" video explains how backend users can create custom page types.

However, I'd prefer to define page types and templates in my theme, for three reasons:

  1. I want to source-control my page types.
  2. It fits more naturally with my development workflow. When building a theme, I typically edit CSS, layout-level HTML, and page-level HTML all at the same time. I'd rather stay in my IDE for this, instead of jumping over to the backend to edit page-level HTML.
  3. I don't want backend users to be able to edit page types. In other words, I want page types and templates to be defined by the developer at development time, not by the backend user after launch. (I'm sure that I could prevent this via user permissions – so consider this a bonus reason, I guess.)

So: is it possible to do this? There seems to be a CMS trend toward giving backend users the ability to create arbitrary layouts – but in my experience, giving clients that freedom is a great way to end up with a trashy site.

edster  —  5 years ago

Nope, those also only sync up to the DB during development (once set as production it will no longer sync from file to db).

Permissions is the right way to remove the ability for clients to modify, as for the source control issue, add the storage path to your repo and you should be good to go.

immersiontravis  —  5 years ago

Thanks for the info. That's very annoying, though – I still have to use the Pages backend to scaffold the files for each page type, and then I have to fiddle with the environment settings whenever I deploy/update those files on production.

frednwt  —  5 years ago

Um, currently you can.

  1. a) You can use Theme Layout instead of Types.

By default the normal Theme Layout are using:

        {% block content %}{% endblock %}

And page use it like that:

{% block content %}
    {% block page %}
        {{ content|raw }}
    {% endblock %}
{% endblock %}

{{ content|raw }} is the full HTML (Types included), but you can use {{ page.title }} for example, or anything else.

  1. b) You can also just use Seeder in your theme or module that will seed the good Types.
  2. Same as 1.
  3. Just remove the user access to Types.
frednwt  —  5 years ago

If you use Theme Layout, I recommend to create new one only for Page (so it will not break other module ;))

Also remember that Theme Layout support subfolder. So you can create layouts/pages/default.twig for example.

ryanthompson  —  5 years ago

Something that will be making it to the roadmap but that is not yet a fully developed idea is generating migrations from existing data. Which would let us generate page types once and then automate migrations for them.

If yo use blocks though, this is less of an issue as your blocks can be distributed across projects. In this way we've all but eliminated the need for page types other than the home page in 90% of our use cases. Comes in handy though for design